001/*
002 * Copyright 2006 - 2013
003 *     Stefan Balev     <stefan.balev@graphstream-project.org>
004 *     Julien Baudry    <julien.baudry@graphstream-project.org>
005 *     Antoine Dutot    <antoine.dutot@graphstream-project.org>
006 *     Yoann Pigné      <yoann.pigne@graphstream-project.org>
007 *     Guilhelm Savin   <guilhelm.savin@graphstream-project.org>
008 * 
009 * This file is part of GraphStream <http://graphstream-project.org>.
010 * 
011 * GraphStream is a library whose purpose is to handle static or dynamic
012 * graph, create them from scratch, file or any source and display them.
013 * 
014 * This program is free software distributed under the terms of two licenses, the
015 * CeCILL-C license that fits European law, and the GNU Lesser General Public
016 * License. You can  use, modify and/ or redistribute the software under the terms
017 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
018 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
019 * the Free Software Foundation, either version 3 of the License, or (at your
020 * option) any later version.
021 * 
022 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
024 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
025 * 
026 * You should have received a copy of the GNU Lesser General Public License
027 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
028 * 
029 * The fact that you are presently reading this means that you have had
030 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
031 */
032package org.graphstream.algorithm.measure;
033
034import org.jfree.chart.ChartFactory;
035import org.jfree.chart.JFreeChart;
036import org.jfree.chart.axis.AxisLocation;
037import org.jfree.chart.axis.NumberAxis;
038import org.jfree.chart.plot.XYPlot;
039import org.jfree.chart.renderer.xy.StandardXYBarPainter;
040import org.jfree.chart.renderer.xy.XYBarRenderer;
041import org.jfree.data.xy.XYSeries;
042import org.jfree.data.xy.XYSeriesCollection;
043
044/**
045 * A measure to plot special entries composed of a minimum, average and maximum
046 * value.
047 */
048public class ChartMinMaxAverageSeriesMeasure extends ChartSeries2DMeasure {
049        /**
050         * Title of the plot. It is used as title in default plot parameters.
051         */
052        protected String title;
053        /**
054         * Series modeling min and max.
055         */
056        protected XYSeries min, max;
057        /**
058         * Flag used to define if min and max are plotted to a different axis than
059         * average. If true, a new axis is created on the right/bottom of the plot.
060         */
061        protected boolean separateMinMaxAxis;
062
063        public ChartMinMaxAverageSeriesMeasure(String name) {
064                super("avg");
065
066                title = name;
067
068                min = new XYSeries("min");
069                max = new XYSeries("max");
070                min.setMaximumItemCount(DEFAULT_WINDOW_SIZE);
071                max.setMaximumItemCount(DEFAULT_WINDOW_SIZE);
072
073                separateMinMaxAxis = true;
074        }
075
076        /**
077         * Flag used to define if min and max are plotted to a different axis than
078         * average.
079         * 
080         * @param on
081         *            true if a new axis should be created on the right/bottom for
082         *            min/max series
083         */
084        public void setSeparateMinMaxAxis(boolean on) {
085                this.separateMinMaxAxis = on;
086        }
087
088        /**
089         * Flag used to define if min and max are plotted to a different axis than
090         * average.
091         * 
092         * @return true if a new axis is created on the right/bottom for min/max
093         *         series
094         */
095        public boolean isSeparateMinMaxAxis() {
096                return separateMinMaxAxis;
097        }
098
099        /*
100         * (non-Javadoc)
101         * 
102         * @see org.graphstream.algorithm.measure.ChartMeasure#setWindowSize(int)
103         */
104        public void setWindowSize(int size) {
105                super.setWindowSize(size);
106
107                min.setMaximumItemCount(size);
108                max.setMaximumItemCount(size);
109        }
110
111        /**
112         * Add a new entry to series.
113         * 
114         * @param x
115         * @param min
116         * @param avg
117         * @param max
118         */
119        public void addValue(double x, double min, double avg, double max) {
120                addValue(x, avg);
121
122                this.min.add(x, min);
123                this.max.add(x, max);
124        }
125
126        /*
127         * (non-Javadoc)
128         * 
129         * @see
130         * org.graphstream.algorithm.measure.ChartSeriesMeasure#getChart(org.graphstream
131         * .algorithm.measure.ChartMeasure.PlotParameters)
132         */
133        public JFreeChart createChart(PlotParameters params) throws PlotException {
134                XYSeriesCollection minMax = new XYSeriesCollection();
135                XYSeriesCollection avgCol = new XYSeriesCollection();
136                XYPlot plot;
137                XYBarRenderer r = new XYBarRenderer();
138                r.setBarPainter(new StandardXYBarPainter());
139                r.setMargin(0.35);
140
141                minMax.addSeries(min);
142                avgCol.addSeries(series);
143                minMax.addSeries(max);
144
145                JFreeChart chart = ChartFactory.createXYLineChart(params.title,
146                                params.xAxisLabel, params.yAxisLabel, avgCol,
147                                params.orientation, params.showLegend, true, false);
148
149                plot = ((XYPlot) chart.getPlot());
150                plot.setDataset(1, minMax);
151                plot.setRenderer(1, r);
152
153                if (separateMinMaxAxis) {
154                        NumberAxis minMaxAxis = new NumberAxis("min/max");
155
156                        plot.setRangeAxis(1, minMaxAxis);
157                        plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
158                        plot.mapDatasetToRangeAxis(1, 1);
159                }
160
161                return chart;
162        }
163
164        /*
165         * (non-Javadoc)
166         * 
167         * @see
168         * org.graphstream.algorithm.measure.ChartSeriesMeasure#getDefaultPlotParameters
169         * ()
170         */
171        public PlotParameters getDefaultPlotParameters() {
172                PlotParameters params = new PlotParameters();
173                params.title = title;
174                params.xAxisLabel = "x";
175                params.yAxisLabel = "average";
176
177                return params;
178        }
179}