001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube 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     * SonarQube 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 License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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.Color;
030    import java.awt.image.BufferedImage;
031    
032    /**
033     * Base implementation to generate charts with JFreechart
034     * 
035     * @since 1.10
036     * @deprecated in 4.5.1, replaced by Javascript charts
037     */
038    @Deprecated
039    public abstract class AbstractChart implements Chart {
040    
041      public static final int FONT_SIZE = 13;
042      public static final Color OUTLINE_COLOR = new Color(51, 51, 51);
043      public static final Color GRID_COLOR = new Color(204, 204, 204);
044      public static final Color[] COLORS = new Color[] { Color.decode("#4192D9"), Color.decode("#800000"), Color.decode("#A7B307"),
045          Color.decode("#913C9F"), Color.decode("#329F4D") };
046    
047      protected abstract Plot getPlot(ChartParameters params);
048    
049      protected boolean hasLegend() {
050        return false;
051      }
052    
053      /**
054       * Generates a JFreeChart chart using a set of parameters
055       * 
056       * @param params the chart parameters
057       * @return the generated chart
058       */
059      @Override
060      public BufferedImage generateImage(ChartParameters params) {
061        JFreeChart chart = new JFreeChart(null, TextTitle.DEFAULT_FONT, getPlot(params), hasLegend());
062        improveChart(chart, params);
063        return chart.createBufferedImage(params.getWidth(), params.getHeight());
064      }
065    
066      private void improveChart(JFreeChart jfrechart, ChartParameters params) {
067        Color background = Color.decode("#" + params.getValue(ChartParameters.PARAM_BACKGROUND_COLOR, "FFFFFF", false));
068        jfrechart.setBackgroundPaint(background);
069    
070        jfrechart.setBorderVisible(false);
071        jfrechart.setAntiAlias(true);
072        jfrechart.setTextAntiAlias(true);
073        jfrechart.removeLegend();
074      }
075    
076      @Override
077      public String toString() {
078        return getKey();
079      }
080    
081      /**
082       * Helper to set color of series. If the parameter colorsHex is null, then default Sonar colors are used.
083       */
084      protected void configureColors(Values2D dataset, CategoryPlot plot, String[] colorsHex) {
085        Color[] colors = COLORS;
086        if (colorsHex != null && colorsHex.length > 0) {
087          colors = new Color[colorsHex.length];
088          for (int i = 0; i < colorsHex.length; i++) {
089            colors[i] = Color.decode("#" + colorsHex[i]);
090          }
091        }
092    
093        dataset.getColumnCount();
094        AbstractRenderer renderer = (AbstractRenderer) plot.getRenderer();
095        for (int i = 0; i < dataset.getColumnCount(); i++) {
096          renderer.setSeriesPaint(i, colors[i % colors.length]);
097    
098        }
099      }
100    }