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.server.charts.deprecated;
021    
022    import org.jfree.chart.JFreeChart;
023    import org.jfree.chart.axis.DateAxis;
024    import org.jfree.chart.axis.DateTickUnit;
025    import org.jfree.chart.axis.NumberAxis;
026    import org.jfree.chart.plot.XYPlot;
027    import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
028    import org.jfree.chart.title.TextTitle;
029    import org.jfree.data.xy.XYSeries;
030    import org.jfree.data.xy.XYSeriesCollection;
031    import org.jfree.ui.RectangleInsets;
032    
033    import java.awt.image.BufferedImage;
034    import java.io.IOException;
035    import java.util.Map;
036    import java.util.StringTokenizer;
037    
038    public class SparkLinesChart extends BaseChartWeb implements DeprecatedChart {
039    
040      private XYSeriesCollection dataset = null;
041      private DateAxis x = null;
042      private NumberAxis y = null;
043      private StandardXYItemRenderer renderer = null;
044    
045      public SparkLinesChart(Map<String, String> params) {
046        super(params);
047        jfreechart = new JFreeChart(null, TextTitle.DEFAULT_FONT, new XYPlot(), false);
048      }
049    
050      @Override
051      protected BufferedImage getChartImage() throws IOException {
052        configure();
053        return getBufferedImage(jfreechart);
054      }
055    
056      private void configure() {
057        configureChart(jfreechart, false);
058        configureXAxis();
059        configureYAxis();
060        configureRenderer();
061        configureDataset();
062        configurePlot();
063        applyParams();
064      }
065    
066      private void configureRenderer() {
067        renderer = new StandardXYItemRenderer(StandardXYItemRenderer.LINES);
068      }
069    
070      private void configureYAxis() {
071        y = new NumberAxis();
072        y.setTickLabelsVisible(false);
073        y.setTickMarksVisible(false);
074        y.setAxisLineVisible(false);
075        y.setNegativeArrowVisible(false);
076        y.setPositiveArrowVisible(false);
077        y.setVisible(false);
078      }
079    
080      private void configureXAxis() {
081        x = new DateAxis();
082        x.setTickUnit(new DateTickUnit(DateTickUnit.MONTH, 1));
083        x.setTickLabelsVisible(false);
084        x.setTickMarksVisible(false);
085        x.setAxisLineVisible(false);
086        x.setNegativeArrowVisible(false);
087        x.setPositiveArrowVisible(false);
088        x.setVisible(false);
089      }
090    
091      private void configureDataset() {
092        dataset = new XYSeriesCollection();
093      }
094    
095      private void configurePlot() {
096        XYPlot plot = (XYPlot) jfreechart.getPlot();
097        plot.setInsets(RectangleInsets.ZERO_INSETS);
098        plot.setDataset(dataset);
099        plot.setDomainAxis(x);
100        plot.setDomainGridlinesVisible(false);
101        plot.setDomainCrosshairVisible(false);
102        plot.setRangeGridlinesVisible(false);
103        plot.setRangeCrosshairVisible(false);
104        plot.setRangeAxis(y);
105        plot.setRenderer(renderer);
106        plot.setBackgroundAlpha(0.0f);
107      }
108    
109      private void applyParams() {
110        applyCommonParams();
111    
112        configureColors(params.get(CHART_PARAM_COLORS), renderer);
113        addMeasures(params.get(CHART_PARAM_VALUES));
114    
115        // -- Plot
116        XYPlot plot = (XYPlot) jfreechart.getPlot();
117        plot.setOutlineVisible(isParamValueValid(params.get(CHART_PARAM_OUTLINE_VISIBLE)) && Boolean.getBoolean(params.get(CHART_PARAM_OUTLINE_VISIBLE)));
118      }
119    
120      private void addMeasures(String values) {
121        double min = Double.MAX_VALUE;
122        double max = Double.MIN_VALUE;
123        XYSeries series1 = new XYSeries("");
124        if (values != null && values.length() > 0) {
125          StringTokenizer st = new StringTokenizer(values, ",");
126          while (st.hasMoreTokens()) {
127            double vX = convertParamToDouble(st.nextToken());
128            double vY = 0.0;
129            if (st.hasMoreTokens()) {
130              vY = convertParamToDouble(st.nextToken());
131            }
132            series1.add(vX, vY);
133    
134            min = (vY < min ? vY : min);
135            max = (vY > max ? vY : max);
136          }
137          dataset.addSeries(series1);
138          y.setRange(min-1, max+1);
139        }
140      }
141    
142    }