001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 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     */
020    package org.sonar.server.charts.deprecated;
021    
022    import org.jfree.chart.plot.CategoryPlot;
023    import org.jfree.data.category.DefaultCategoryDataset;
024    import org.jfree.ui.RectangleInsets;
025    
026    import java.awt.*;
027    import java.awt.image.BufferedImage;
028    import java.io.IOException;
029    import java.util.Map;
030    import java.util.StringTokenizer;
031    
032    public 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          boolean categoriesPresent = categoriesParam != null && categoriesParam.length() > 0;
112          String[] categoriesSplit = null;
113          if (categoriesPresent) {
114            categoriesSplit = categoriesParam.split(",");
115          } else {
116            categoriesSplit = new String[nbValues];
117            for (int i = 0; i < nbValues; i++) {
118              categoriesSplit[i] = DEFAULT_NAME_CATEGORY + i;
119            }
120          }
121          int nbCategories = categoriesSplit.length;
122    
123          // Series
124          String[] seriesSplit = {DEFAULT_NAME_SERIE};
125          int nbSeries = 1;
126    
127          //
128          for (int iCategories = 0; iCategories < nbCategories; iCategories++) {
129            String currentCategory = categoriesSplit[iCategories];
130            for (int iSeries = 0; iSeries < nbSeries; iSeries++) {
131              String currentSerie = seriesSplit[iSeries];
132              double currentValue = 0.0;
133              if (stValues.hasMoreTokens()) {
134                try {
135                  currentValue = Double.parseDouble(stValues.nextToken());
136                } catch (NumberFormatException e) {
137                }
138              }
139              dataset.addValue(currentValue, currentSerie, currentCategory);
140            }
141          }
142        }
143      }
144    
145    }