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.plugins.core.charts;
021
022import org.apache.commons.lang.StringUtils;
023import org.jfree.chart.axis.CategoryAxis;
024import org.jfree.chart.axis.NumberAxis;
025import org.jfree.chart.plot.CategoryPlot;
026import org.jfree.chart.plot.Plot;
027import org.jfree.chart.renderer.category.AreaRenderer;
028import org.jfree.data.category.DefaultCategoryDataset;
029import org.sonar.api.charts.AbstractChart;
030import org.sonar.api.charts.ChartParameters;
031
032import java.text.NumberFormat;
033
034public class DistributionAreaChart extends AbstractChart {
035  private static final String PARAM_COLORS = "c";
036
037  public String getKey() {
038    return "distarea";
039  }
040
041  @Override
042  protected Plot getPlot(ChartParameters params) {
043    DefaultCategoryDataset dataset = createDataset(params);
044
045    CategoryAxis domainAxis = new CategoryAxis();
046    domainAxis.setCategoryMargin(0.0);
047    domainAxis.setLowerMargin(0.0);
048    domainAxis.setUpperMargin(0.0);
049
050    NumberAxis rangeAxis = new NumberAxis();
051    rangeAxis.setNumberFormatOverride(NumberFormat.getIntegerInstance(params.getLocale()));
052    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
053
054    AreaRenderer renderer = new AreaRenderer();
055    CategoryPlot plot = new CategoryPlot(dataset, domainAxis, rangeAxis, renderer);
056    plot.setForegroundAlpha(0.5f);
057    plot.setDomainGridlinesVisible(true);
058    configureColors(dataset, plot, params.getValues(PARAM_COLORS, ","));
059    return plot;
060  }
061
062  private DefaultCategoryDataset createDataset(ChartParameters params) {
063    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
064
065    String[] series = params.getValues("v", "|", true);
066    int index = 0;
067    while (index < series.length) {
068      String[] pairs = StringUtils.split(series[index], ";");
069      if (pairs.length == 0) {
070        dataset.addValue((Number)0.0, index, "0");
071
072      } else {
073        for (String pair : pairs) {
074          String[] keyValue = StringUtils.split(pair, "=");
075          double val = Double.parseDouble(keyValue[1]);
076          dataset.addValue((Number) val, index, keyValue[0]);
077        }
078      }
079      index++;
080    }
081    return dataset;
082  }
083}