001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
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 */
020package org.sonar.server.charts.deprecated;
021
022import org.jfree.chart.plot.CategoryPlot;
023import org.jfree.data.category.DefaultCategoryDataset;
024import org.jfree.ui.RectangleInsets;
025
026import java.awt.*;
027import java.awt.image.BufferedImage;
028import java.io.IOException;
029import java.util.Map;
030import java.util.StringTokenizer;
031
032public class CustomBarChart extends BarChart {
033
034  private CustomBarRenderer renderer = null;
035
036  public CustomBarChart(Map<String, String> params) {
037    super(params);
038  }
039
040  @Override
041  protected BufferedImage getChartImage() throws IOException {
042    configure();
043    return getBufferedImage(jfreechart);
044  }
045
046  @Override
047  protected void configure() {
048    configureChart(jfreechart, false);
049    configureCategoryDataset();
050    configureCategoryAxis();
051    configureRenderer();
052    configureRangeAxis();
053    configureCategoryPlot();
054    applyParams();
055  }
056
057  @Override
058  protected void configureCategoryDataset() {
059    dataset = new DefaultCategoryDataset();
060  }
061
062  @Override
063  protected void configureRenderer() {
064    renderer = new CustomBarRenderer(null);
065    renderer.setItemMargin(0.0);
066    renderer.setDrawBarOutline(false);
067  }
068
069  @Override
070  protected void configureCategoryPlot() {
071    CategoryPlot plot = jfreechart.getCategoryPlot();
072    plot.setNoDataMessage(DEFAULT_MESSAGE_NODATA);
073    plot.setInsets(RectangleInsets.ZERO_INSETS); // To remove inner space around chart
074    plot.setDataset(dataset);
075    plot.setDomainAxis(categoryAxis);
076    plot.setRenderer(renderer);
077    plot.setRangeAxis(numberAxis);
078  }
079
080  protected void applyParams() {
081    applyCommonParams();
082    applyCommomParamsBar();
083
084    configureColors(params.get(CHART_PARAM_COLORS));
085    addMeasures(params.get(CHART_PARAM_VALUES));
086  }
087
088  private void configureColors(String colorsParam) {
089    Paint[] colors = CustomBarRenderer.COLORS;
090    if (colorsParam != null && colorsParam.length() > 0) {
091      StringTokenizer stColors = new StringTokenizer(colorsParam, ",");
092      colors = new Paint[stColors.countTokens()];
093      int i = 0;
094      while (stColors.hasMoreTokens()) {
095        colors[i] = Color.decode("0x" + stColors.nextToken());
096        i++;
097      }
098    }
099
100    renderer.setColors(colors);
101  }
102
103  private void addMeasures(String values) {
104    if (values != null && values.length() > 0) {
105      // Values
106      StringTokenizer stValues = new StringTokenizer(values, ",");
107      int nbValues = stValues.countTokens();
108
109      // Categories
110      String categoriesParam = params.get(CHART_PARAM_CATEGORIES);
111      String[] categoriesSplit = null;
112      if (categoriesParam != null && categoriesParam.length() > 0) {
113        categoriesSplit = categoriesParam.split(",");
114      } else {
115        categoriesSplit = new String[nbValues];
116        for (int i = 0; i < nbValues; i++) {
117          categoriesSplit[i] = DEFAULT_NAME_CATEGORY + i;
118        }
119      }
120
121      // Series
122      String[] seriesSplit = {DEFAULT_NAME_SERIE};
123      int nbSeries = 1;
124
125      for (String currentCategory : categoriesSplit) {
126        for (int iSeries = 0; iSeries < nbSeries; iSeries++) {
127          String currentSerie = seriesSplit[iSeries];
128          double currentValue = 0.0;
129          if (stValues.hasMoreTokens()) {
130            try {
131              currentValue = Double.parseDouble(stValues.nextToken());
132            } catch (NumberFormatException e) {
133              // ignore
134            }
135          }
136          dataset.addValue(currentValue, currentSerie, currentCategory);
137        }
138      }
139    }
140  }
141
142}