001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 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 */
020package org.sonar.api.charts;
021
022import org.jfree.chart.JFreeChart;
023import org.jfree.chart.plot.CategoryPlot;
024import org.jfree.chart.plot.Plot;
025import org.jfree.chart.renderer.AbstractRenderer;
026import org.jfree.chart.title.TextTitle;
027import org.jfree.data.Values2D;
028
029import java.awt.Color;
030import java.awt.image.BufferedImage;
031
032/**
033 * An extension point to generate JFreeChart charts
034 * 
035 * @since 1.10
036 */
037public abstract class AbstractChart implements Chart {
038
039  public static final int FONT_SIZE = 13;
040  public static final Color OUTLINE_COLOR = new Color(51, 51, 51);
041  public static final Color GRID_COLOR = new Color(204, 204, 204);
042  public static final Color[] COLORS = new Color[] { Color.decode("#4192D9"), Color.decode("#800000"), Color.decode("#A7B307"),
043      Color.decode("#913C9F"), Color.decode("#329F4D") };
044
045  protected abstract Plot getPlot(ChartParameters params);
046
047  protected boolean hasLegend() {
048    return false;
049  }
050
051  /**
052   * Generates a JFreeChart chart using a set of parameters
053   * 
054   * @param params the chart parameters
055   * @return the generated chart
056   */
057  public BufferedImage generateImage(ChartParameters params) {
058    JFreeChart chart = new JFreeChart(null, TextTitle.DEFAULT_FONT, getPlot(params), hasLegend());
059    improveChart(chart, params);
060    return chart.createBufferedImage(params.getWidth(), params.getHeight());
061  }
062
063  private void improveChart(JFreeChart jfrechart, ChartParameters params) {
064    Color background = Color.decode("#" + params.getValue(ChartParameters.PARAM_BACKGROUND_COLOR, "FFFFFF", false));
065    jfrechart.setBackgroundPaint(background);
066
067    jfrechart.setBorderVisible(false);
068    jfrechart.setAntiAlias(true);
069    jfrechart.setTextAntiAlias(true);
070    jfrechart.removeLegend();
071  }
072
073  @Override
074  public String toString() {
075    return getKey();
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}