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.data.xy.XYSeries;
037import org.jfree.data.xy.XYSeriesCollection;
038
039/**
040 * Base for series measure.
041 */
042public abstract class ChartSeriesMeasure extends ChartMeasure {
043
044        public static final int DEFAULT_WINDOW_SIZE = 100;
045        
046        protected XYSeries series;
047
048        /**
049         * Default constructor.
050         * 
051         * @param name
052         *            name of this measure
053         */
054        public ChartSeriesMeasure(String name) {
055                super(name);
056                series = new XYSeries(name);
057                series.setMaximumItemCount(DEFAULT_WINDOW_SIZE);
058        }
059
060        /**
061         * Create a {@link org.jfree.data.xy.XYSeries} object that can be used to
062         * create plot.
063         * 
064         * @return a XYSeries
065         */
066        public XYSeries getXYSeries() {
067                return series;
068        }
069
070        /*
071         * (non-Javadoc)
072         * 
073         * @see org.graphstream.algorithm.measure.ChartMeasure#setWindowSize(int)
074         */
075        public void setWindowSize(int size) {
076                series.setMaximumItemCount(size);
077        }
078
079        /*
080         * (non-Javadoc)
081         * 
082         * @see
083         * org.graphstream.algorithm.measure.ChartMeasure#plot(org.graphstream.algorithm
084         * .measure.ChartMeasure.PlotParameters)
085         */
086        public void plot(PlotParameters params) throws PlotException {
087                outputPlot(params, createChart(params));
088        }
089
090        /*
091         * (non-Javadoc)
092         * 
093         * @see
094         * org.graphstream.algorithm.measure.ChartMeasure#getDefaultPlotParameters()
095         */
096        public PlotParameters getDefaultPlotParameters() {
097                PlotParameters params = new PlotParameters();
098                params.title = name;
099                params.type = PlotType.LINE;
100
101                return params;
102        }
103
104        /*
105         * (non-Javadoc)
106         * 
107         * @see
108         * org.graphstream.algorithm.measure.ChartMeasure#getChart(org.graphstream
109         * .algorithm.measure.ChartMeasure.PlotParameters)
110         */
111        public JFreeChart createChart(PlotParameters params) throws PlotException {
112                JFreeChart chart;
113                XYSeriesCollection dataset = new XYSeriesCollection();
114
115                dataset.addSeries(getXYSeries());
116
117                switch (params.type) {
118                case LINE:
119                        chart = ChartFactory.createXYLineChart(params.title,
120                                        params.xAxisLabel, params.yAxisLabel, dataset,
121                                        params.orientation, params.showLegend, false, false);
122                        break;
123                case BAR:
124                        chart = ChartFactory.createXYBarChart(params.title,
125                                        params.xAxisLabel, false, params.yAxisLabel, dataset,
126                                        params.orientation, params.showLegend, false, false);
127                        break;
128                case SCATTER:
129                        chart = ChartFactory.createScatterPlot(params.title,
130                                        params.xAxisLabel, params.yAxisLabel, dataset,
131                                        params.orientation, params.showLegend, false, false);
132                        break;
133                default:
134                        throw new PlotException("unsupported plot type");
135                }
136
137                return chart;
138        }
139}