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     */
020    package org.sonar.plugins.core.charts;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.jfree.chart.axis.CategoryAxis;
024    import org.jfree.chart.axis.NumberAxis;
025    import org.jfree.chart.plot.CategoryPlot;
026    import org.jfree.chart.plot.Plot;
027    import org.jfree.chart.renderer.category.BarRenderer;
028    import org.jfree.data.category.DefaultCategoryDataset;
029    import org.sonar.api.charts.AbstractChart;
030    import org.sonar.api.charts.ChartParameters;
031    
032    import java.awt.*;
033    import java.text.DecimalFormat;
034    
035    public class DistributionBarChart extends AbstractChart {
036    
037    
038      public static final String PARAM_VALUES = "v";
039      public static final String PARAM_COLORS = "c";
040      public static final String PARAM_Y_SUFFIX = "ysuf";
041      public static final String PARAM_X_SUFFIX = "xsuf";
042      public static final String PARAM_FONT_SIZE = "fs";
043    
044      public String getKey() {
045        return "distbar";
046      }
047    
048      @Override
049      public Plot getPlot(ChartParameters params) {
050        CategoryPlot plot = generateJFreeChart(params);
051        plot.setOutlinePaint(OUTLINE_COLOR);
052        plot.setDomainGridlinePaint(GRID_COLOR);
053        plot.setRangeGridlinePaint(GRID_COLOR);
054        return plot;
055      }
056    
057      private CategoryPlot generateJFreeChart(ChartParameters params) {
058        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
059        CategoryPlot plot = new CategoryPlot();
060    
061        Font font = getFont(params.getValue(PARAM_FONT_SIZE));
062        configureDomainAxis(plot, font);
063        configureRangeAxis(plot, params.getValue(PARAM_Y_SUFFIX, "", true), font);
064        configureRenderer(plot);
065        configureValues(dataset, params.getValues(PARAM_VALUES, "|", true), params.getValue(PARAM_X_SUFFIX, "", true));
066        configureColors(dataset, plot, params.getValues(PARAM_COLORS, ","));
067    
068        plot.setDataset(dataset);
069        return plot;
070      }
071    
072      static void configureValues(DefaultCategoryDataset dataset, String[] series, String xSuffix) {
073        int index = 0;
074        while (index < series.length) {
075          String[] pairs = StringUtils.split(series[index], ";");
076          if (pairs.length == 0) {
077            dataset.addValue((Number) 0.0, index, "0");
078    
079          } else {
080            for (String pair : pairs) {
081              String[] keyValue = StringUtils.split(pair, "=");
082              double val = Double.parseDouble(keyValue[1]);
083              dataset.addValue((Number) val, index, keyValue[0] + xSuffix);
084            }
085          }
086          index++;
087        }
088    
089      }
090    
091      private void configureRenderer(CategoryPlot plot) {
092        BarRenderer renderer = new BarRenderer();
093        renderer.setDrawBarOutline(true);
094        renderer.setSeriesItemLabelsVisible(0, true);
095        renderer.setItemMargin(0);
096        plot.setRenderer(renderer);
097      }
098    
099      private void configureDomainAxis(CategoryPlot plot, Font font) {
100        CategoryAxis categoryAxis = new CategoryAxis();
101        categoryAxis.setTickMarksVisible(true);
102        categoryAxis.setTickLabelFont(font);
103        categoryAxis.setTickLabelPaint(OUTLINE_COLOR);
104        plot.setDomainAxis(categoryAxis);
105        plot.setDomainGridlinesVisible(false);
106      }
107    
108      private Font getFont(String fontSize) {
109        int size = FONT_SIZE;
110        if (!StringUtils.isBlank(fontSize)) {
111          size = Integer.parseInt(fontSize);
112        }
113        return new Font("SansSerif", Font.PLAIN, size);
114      }
115    
116      private void configureRangeAxis(CategoryPlot plot, String valueLabelSuffix, Font font) {
117        NumberAxis numberAxis = new NumberAxis();
118        numberAxis.setUpperMargin(0.3);
119        numberAxis.setTickLabelFont(font);
120        numberAxis.setTickLabelPaint(OUTLINE_COLOR);
121        String suffix = "";
122        if (valueLabelSuffix != null && !"".equals(valueLabelSuffix)) {
123          suffix = new StringBuilder().append("'").append(valueLabelSuffix).append("'").toString();
124        }
125        numberAxis.setNumberFormatOverride(new DecimalFormat("0" + suffix));
126        numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
127        plot.setRangeAxis(numberAxis);
128      }
129    }