001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.api.charts;
021    
022    import org.jfree.chart.JFreeChart;
023    import org.jfree.chart.plot.CategoryPlot;
024    import org.jfree.chart.plot.Plot;
025    import org.jfree.chart.renderer.AbstractRenderer;
026    import org.jfree.chart.title.TextTitle;
027    import org.jfree.data.Values2D;
028    
029    import java.awt.*;
030    import java.awt.image.BufferedImage;
031    
032    /**
033     * @since 1.10
034     */
035    public abstract class AbstractChart implements Chart {
036    
037      public static final int FONT_SIZE = 13;
038      public static final Color OUTLINE_COLOR = new Color(51, 51, 51);
039      public static final Color GRID_COLOR = new Color(204, 204, 204);
040      public static final Color[] COLORS = new Color[]{Color.decode("#4192D9"), Color.decode("#800000"), Color.decode("#A7B307"), Color.decode("#913C9F"), Color.decode("#329F4D")};
041    
042    
043      protected abstract Plot getPlot(ChartParameters params);
044    
045      protected boolean hasLegend() {
046        return false;
047      }
048    
049      public BufferedImage generateImage(ChartParameters params) {
050        JFreeChart chart = new JFreeChart(null, TextTitle.DEFAULT_FONT, getPlot(params), hasLegend());
051        improveChart(chart, params);
052        return chart.createBufferedImage(params.getWidth(), params.getHeight());
053      }
054    
055      private void improveChart(JFreeChart jfrechart, ChartParameters params) {
056        Color background = Color.decode("#" + params.getValue(ChartParameters.PARAM_BACKGROUND_COLOR, "FFFFFF", false));
057        jfrechart.setBackgroundPaint(background);
058    
059        jfrechart.setBorderVisible(false);
060        jfrechart.setAntiAlias(true);
061        jfrechart.setTextAntiAlias(true);
062        jfrechart.removeLegend();
063      }
064    
065      @Override
066      public String toString() {
067        return getKey();
068      }
069    
070    
071      /**
072       * Helper to set color of series. If the parameter colorsHex is null, then default Sonar colors are used.
073       */
074      protected void configureColors(Values2D dataset, CategoryPlot plot, String[] colorsHex) {
075        Color[] colors = COLORS;
076        if (colorsHex != null && colorsHex.length > 0) {
077          colors = new Color[colorsHex.length];
078          for (int i = 0; i < colorsHex.length; i++) {
079            colors[i] = Color.decode("#" + colorsHex[i]);
080          }
081        }
082    
083        dataset.getColumnCount();
084        AbstractRenderer renderer = (AbstractRenderer) plot.getRenderer();
085        for (int i = 0; i < dataset.getColumnCount(); i++) {
086          renderer.setSeriesPaint(i, colors[i % colors.length]);
087    
088        }
089      }
090    }