001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2009, by Object Refinery Limited and Contributors.
006     *
007     * Project Info:  http://www.jfree.org/jfreechart/index.html
008     *
009     * This library is free software; you can redistribute it and/or modify it
010     * under the terms of the GNU Lesser General Public License as published by
011     * the Free Software Foundation; either version 2.1 of the License, or
012     * (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but
015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017     * License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this library; if not, write to the Free Software
021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022     * USA.
023     *
024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025     * in the United States and other countries.]
026     *
027     * -------------------------
028     * TimeSeriesChartDemo1.java
029     * -------------------------
030     * (C) Copyright 2003-2009, by Object Refinery Limited and Contributors.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   ;
034     *
035     * Changes
036     * -------
037     * 09-Mar-2005 : Version 1, copied from the demo collection that ships with
038     *               the JFreeChart Developer Guide (DG);
039     *
040     */
041    
042    package org.jfree.chart.demo;
043    
044    import java.awt.Color;
045    import java.text.SimpleDateFormat;
046    
047    import javax.swing.JPanel;
048    
049    import org.jfree.chart.ChartFactory;
050    import org.jfree.chart.ChartPanel;
051    import org.jfree.chart.JFreeChart;
052    import org.jfree.chart.axis.DateAxis;
053    import org.jfree.chart.plot.XYPlot;
054    import org.jfree.chart.renderer.xy.XYItemRenderer;
055    import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
056    import org.jfree.data.time.Month;
057    import org.jfree.data.time.TimeSeries;
058    import org.jfree.data.time.TimeSeriesCollection;
059    import org.jfree.data.xy.XYDataset;
060    import org.jfree.ui.ApplicationFrame;
061    import org.jfree.ui.RectangleInsets;
062    import org.jfree.ui.RefineryUtilities;
063    
064    /**
065     * An example of a time series chart.  For the most part, default settings are
066     * used, except that the renderer is modified to show filled shapes (as well as
067     * lines) at each data point.
068     */
069    public class TimeSeriesChartDemo1 extends ApplicationFrame {
070    
071        /**
072         * A demonstration application showing how to create a simple time series
073         * chart.  This example uses monthly data.
074         *
075         * @param title  the frame title.
076         */
077        public TimeSeriesChartDemo1(String title) {
078            super(title);
079            ChartPanel chartPanel = (ChartPanel) createDemoPanel();
080            chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
081            setContentPane(chartPanel);
082        }
083    
084        /**
085         * Creates a chart.
086         *
087         * @param dataset  a dataset.
088         *
089         * @return A chart.
090         */
091        private static JFreeChart createChart(XYDataset dataset) {
092    
093            JFreeChart chart = ChartFactory.createTimeSeriesChart(
094                "Legal & General Unit Trust Prices",  // title
095                "Date",             // x-axis label
096                "Price Per Unit",   // y-axis label
097                dataset,            // data
098                true,               // create legend?
099                true,               // generate tooltips?
100                false               // generate URLs?
101            );
102    
103            chart.setBackgroundPaint(Color.white);
104    
105            XYPlot plot = (XYPlot) chart.getPlot();
106            plot.setBackgroundPaint(Color.lightGray);
107            plot.setDomainGridlinePaint(Color.white);
108            plot.setRangeGridlinePaint(Color.white);
109            plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
110            plot.setDomainCrosshairVisible(true);
111            plot.setRangeCrosshairVisible(true);
112    
113            XYItemRenderer r = plot.getRenderer();
114            if (r instanceof XYLineAndShapeRenderer) {
115                XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
116                renderer.setBaseShapesVisible(true);
117                renderer.setBaseShapesFilled(true);
118                renderer.setDrawSeriesLineAsPath(true);
119            }
120    
121            DateAxis axis = (DateAxis) plot.getDomainAxis();
122            axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
123    
124            return chart;
125    
126        }
127    
128        /**
129         * Creates a dataset, consisting of two series of monthly data.
130         *
131         * @return The dataset.
132         */
133        private static XYDataset createDataset() {
134    
135            TimeSeries s1 = new TimeSeries("L&G European Index Trust");
136            s1.add(new Month(2, 2001), 181.8);
137            s1.add(new Month(3, 2001), 167.3);
138            s1.add(new Month(4, 2001), 153.8);
139            s1.add(new Month(5, 2001), 167.6);
140            s1.add(new Month(6, 2001), 158.8);
141            s1.add(new Month(7, 2001), 148.3);
142            s1.add(new Month(8, 2001), 153.9);
143            s1.add(new Month(9, 2001), 142.7);
144            s1.add(new Month(10, 2001), 123.2);
145            s1.add(new Month(11, 2001), 131.8);
146            s1.add(new Month(12, 2001), 139.6);
147            s1.add(new Month(1, 2002), 142.9);
148            s1.add(new Month(2, 2002), 138.7);
149            s1.add(new Month(3, 2002), 137.3);
150            s1.add(new Month(4, 2002), 143.9);
151            s1.add(new Month(5, 2002), 139.8);
152            s1.add(new Month(6, 2002), 137.0);
153            s1.add(new Month(7, 2002), 132.8);
154    
155            TimeSeries s2 = new TimeSeries("L&G UK Index Trust");
156            s2.add(new Month(2, 2001), 129.6);
157            s2.add(new Month(3, 2001), 123.2);
158            s2.add(new Month(4, 2001), 117.2);
159            s2.add(new Month(5, 2001), 124.1);
160            s2.add(new Month(6, 2001), 122.6);
161            s2.add(new Month(7, 2001), 119.2);
162            s2.add(new Month(8, 2001), 116.5);
163            s2.add(new Month(9, 2001), 112.7);
164            s2.add(new Month(10, 2001), 101.5);
165            s2.add(new Month(11, 2001), 106.1);
166            s2.add(new Month(12, 2001), 110.3);
167            s2.add(new Month(1, 2002), 111.7);
168            s2.add(new Month(2, 2002), 111.0);
169            s2.add(new Month(3, 2002), 109.6);
170            s2.add(new Month(4, 2002), 113.2);
171            s2.add(new Month(5, 2002), 111.6);
172            s2.add(new Month(6, 2002), 108.8);
173            s2.add(new Month(7, 2002), 101.6);
174    
175            // ******************************************************************
176            //  More than 150 demo applications are included with the JFreeChart
177            //  Developer Guide...for more information, see:
178            //
179            //  >   http://www.object-refinery.com/jfreechart/guide.html
180            //
181            // ******************************************************************
182    
183            TimeSeriesCollection dataset = new TimeSeriesCollection();
184            dataset.addSeries(s1);
185            dataset.addSeries(s2);
186    
187            return dataset;
188    
189        }
190    
191        /**
192         * Creates a panel for the demo (used by SuperDemo.java).
193         *
194         * @return A panel.
195         */
196        public static JPanel createDemoPanel() {
197            JFreeChart chart = createChart(createDataset());
198            ChartPanel panel = new ChartPanel(chart);
199            panel.setFillZoomRectangle(true);
200            panel.setMouseWheelEnabled(true);
201            return panel;
202        }
203    
204        /**
205         * Starting point for the demonstration application.
206         *
207         * @param args  ignored.
208         */
209        public static void main(String[] args) {
210    
211            TimeSeriesChartDemo1 demo = new TimeSeriesChartDemo1(
212                    "Time Series Chart Demo 1");
213            demo.pack();
214            RefineryUtilities.centerFrameOnScreen(demo);
215            demo.setVisible(true);
216    
217        }
218    
219    }