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 */
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 * 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
039public 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  public BufferedImage generateImage(ChartParameters params) {
060    JFreeChart chart = new JFreeChart(null, TextTitle.DEFAULT_FONT, getPlot(params), hasLegend());
061    improveChart(chart, params);
062    return chart.createBufferedImage(params.getWidth(), params.getHeight());
063  }
064
065  private void improveChart(JFreeChart jfrechart, ChartParameters params) {
066    Color background = Color.decode("#" + params.getValue(ChartParameters.PARAM_BACKGROUND_COLOR, "FFFFFF", false));
067    jfrechart.setBackgroundPaint(background);
068
069    jfrechart.setBorderVisible(false);
070    jfrechart.setAntiAlias(true);
071    jfrechart.setTextAntiAlias(true);
072    jfrechart.removeLegend();
073  }
074
075  @Override
076  public String toString() {
077    return getKey();
078  }
079
080  /**
081   * Helper to set color of series. If the parameter colorsHex is null, then default Sonar colors are used.
082   */
083  protected void configureColors(Values2D dataset, CategoryPlot plot, String[] colorsHex) {
084    Color[] colors = COLORS;
085    if (colorsHex != null && colorsHex.length > 0) {
086      colors = new Color[colorsHex.length];
087      for (int i = 0; i < colorsHex.length; i++) {
088        colors[i] = Color.decode("#" + colorsHex[i]);
089      }
090    }
091
092    dataset.getColumnCount();
093    AbstractRenderer renderer = (AbstractRenderer) plot.getRenderer();
094    for (int i = 0; i < dataset.getColumnCount(); i++) {
095      renderer.setSeriesPaint(i, colors[i % colors.length]);
096
097    }
098  }
099}