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.plugins.core.charts;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.jfree.chart.plot.Plot;
024    import org.jfree.chart.plot.SpiderWebPlot;
025    import org.jfree.data.category.CategoryDataset;
026    import org.jfree.data.category.DefaultCategoryDataset;
027    import org.sonar.api.charts.AbstractChart;
028    import org.sonar.api.charts.ChartParameters;
029    
030    import java.awt.*;
031    
032    public class XradarChart extends AbstractChart {
033    
034      /**
035       * see an example of complete URL in XradarChartTest
036       */
037    
038      public static final String PARAM_COLOR = "c";
039      public static final String PARAM_MAX_VALUE = "m";
040      public static final String PARAM_INTERIOR_GAP = "g";
041      public static final String PARAM_LABELS = "l";
042      public static final String PARAM_VALUES = "v";
043    
044      public String getKey() {
045        return "xradar";
046      }
047    
048      @Override
049      protected Plot getPlot(ChartParameters params) {
050        SpiderWebPlot plot = new SpiderWebPlot(createDataset(params));
051        plot.setStartAngle(0D);
052        plot.setOutlineVisible(false);
053        plot.setAxisLinePaint(Color.decode("0xCCCCCC"));
054        plot.setSeriesOutlineStroke(new BasicStroke(2f));
055    
056        if (params.getValue(PARAM_INTERIOR_GAP) != null) {
057          plot.setInteriorGap(Double.parseDouble(params.getValue(PARAM_INTERIOR_GAP, "0.4", false)));
058        }
059        if (params.getValue(PARAM_MAX_VALUE) != null) {
060          plot.setMaxValue(Double.parseDouble(params.getValue(PARAM_MAX_VALUE, "100", false)));
061        }
062        configureColors(plot, params);
063        return plot;
064      }
065    
066      private void configureColors(SpiderWebPlot plot, ChartParameters params) {
067        String[] colors = params.getValues(PARAM_COLOR, "|");
068        for (int i = 0; i < colors.length; i++) {
069          plot.setSeriesPaint(i, Color.decode("0x" + colors[i]));
070        }
071      }
072    
073      private CategoryDataset createDataset(ChartParameters params) {
074        String[] labels = params.getValues(PARAM_LABELS, ",");
075        String[] values = params.getValues(PARAM_VALUES, "|");
076    
077        DefaultCategoryDataset set = new DefaultCategoryDataset();
078        for (int indexValues = 0; indexValues < values.length; indexValues++) {
079          String[] fields = StringUtils.split(values[indexValues], ",");
080          for (int i = 0; i < fields.length; i++) {
081            set.addValue(Double.parseDouble(fields[i]), "" + indexValues, labels[i]);
082          }
083        }
084        return set;
085      }
086    }