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     * An extension point to generate JFreeChart charts
034     * @since 1.10
035     */
036    public abstract class AbstractChart implements Chart {
037    
038      public static final int FONT_SIZE = 13;
039      public static final Color OUTLINE_COLOR = new Color(51, 51, 51);
040      public static final Color GRID_COLOR = new Color(204, 204, 204);
041      public static final Color[] COLORS = new Color[]{Color.decode("#4192D9"), Color.decode("#800000"), Color.decode("#A7B307"), Color.decode("#913C9F"), Color.decode("#329F4D")};
042    
043    
044      protected abstract Plot getPlot(ChartParameters params);
045    
046      protected boolean hasLegend() {
047        return false;
048      }
049    
050      /**
051       * Generates a JFreeChart chart using a set of parameters
052       *
053       * @param params the chart parameters
054       * @return the generated chart
055       */
056      public BufferedImage generateImage(ChartParameters params) {
057        JFreeChart chart = new JFreeChart(null, TextTitle.DEFAULT_FONT, getPlot(params), hasLegend());
058        improveChart(chart, params);
059        return chart.createBufferedImage(params.getWidth(), params.getHeight());
060      }
061    
062      private void improveChart(JFreeChart jfrechart, ChartParameters params) {
063        Color background = Color.decode("#" + params.getValue(ChartParameters.PARAM_BACKGROUND_COLOR, "FFFFFF", false));
064        jfrechart.setBackgroundPaint(background);
065    
066        jfrechart.setBorderVisible(false);
067        jfrechart.setAntiAlias(true);
068        jfrechart.setTextAntiAlias(true);
069        jfrechart.removeLegend();
070      }
071    
072      @Override
073      public String toString() {
074        return getKey();
075      }
076    
077    
078      /**
079       * Helper to set color of series. If the parameter colorsHex is null, then default Sonar colors are used.
080       */
081      protected void configureColors(Values2D dataset, CategoryPlot plot, String[] colorsHex) {
082        Color[] colors = COLORS;
083        if (colorsHex != null && colorsHex.length > 0) {
084          colors = new Color[colorsHex.length];
085          for (int i = 0; i < colorsHex.length; i++) {
086            colors[i] = Color.decode("#" + colorsHex[i]);
087          }
088        }
089    
090        dataset.getColumnCount();
091        AbstractRenderer renderer = (AbstractRenderer) plot.getRenderer();
092        for (int i = 0; i < dataset.getColumnCount(); i++) {
093          renderer.setSeriesPaint(i, colors[i % colors.length]);
094    
095        }
096      }
097    }