KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > demo > TimeSeriesDemo


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * -------------------
23  * TimeSeriesDemo.java
24  * -------------------
25  * (C) Copyright 2002, 2003, by Object Refinery Limited and Contributors.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: TimeSeriesDemo.java,v 1.11 2003/11/28 10:57:34 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 08-Apr-2002 : Version 1 (DG);
35  * 25-Jun-2002 : Removed unnecessary import (DG);
36  *
37  */

38
39 package org.jfree.chart.demo;
40
41 import java.awt.Color JavaDoc;
42 import java.text.SimpleDateFormat JavaDoc;
43
44 import org.jfree.chart.ChartFactory;
45 import org.jfree.chart.ChartPanel;
46 import org.jfree.chart.JFreeChart;
47 import org.jfree.chart.Spacer;
48 import org.jfree.chart.StandardLegend;
49 import org.jfree.chart.axis.DateAxis;
50 import org.jfree.chart.plot.XYPlot;
51 import org.jfree.chart.renderer.StandardXYItemRenderer;
52 import org.jfree.chart.renderer.XYItemRenderer;
53 import org.jfree.data.XYDataset;
54 import org.jfree.data.time.Month;
55 import org.jfree.data.time.TimeSeries;
56 import org.jfree.data.time.TimeSeriesCollection;
57 import org.jfree.ui.ApplicationFrame;
58 import org.jfree.ui.RefineryUtilities;
59
60 /**
61  * An example of a time series chart. For the most part, default settings are used, except that
62  * the renderer is modified to show filled shapes (as well as lines) at each data point.
63  *
64  * @author David Gilbert
65  */

66 public class TimeSeriesDemo extends ApplicationFrame {
67
68     /**
69      * A demonstration application showing how to create a simple time series chart. This
70      * example uses monthly data.
71      *
72      * @param title the frame title.
73      */

74     public TimeSeriesDemo(String JavaDoc title) {
75
76         super(title);
77
78         XYDataset dataset = createDataset();
79
80         JFreeChart chart = createChart(dataset);
81
82         ChartPanel chartPanel = new ChartPanel(chart);
83         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
84         chartPanel.setMouseZoomable(true, false);
85         setContentPane(chartPanel);
86
87     }
88
89     /**
90      * Creates a chart.
91      *
92      * @param dataset a dataset.
93      *
94      * @return A chart.
95      */

96     private JFreeChart createChart(XYDataset dataset) {
97
98         JFreeChart chart = ChartFactory.createTimeSeriesChart(
99             "Legal & General Unit Trust Prices",
100             "Date", "Price Per Unit",
101             dataset,
102             true,
103             true,
104             false
105         );
106
107         chart.setBackgroundPaint(Color.white);
108
109         StandardLegend sl = (StandardLegend) chart.getLegend();
110         sl.setDisplaySeriesShapes(true);
111
112         XYPlot plot = chart.getXYPlot();
113         plot.setBackgroundPaint(Color.lightGray);
114         plot.setDomainGridlinePaint(Color.white);
115         plot.setRangeGridlinePaint(Color.white);
116         plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
117         plot.setDomainCrosshairVisible(true);
118         plot.setRangeCrosshairVisible(true);
119         
120         XYItemRenderer renderer = plot.getRenderer();
121         if (renderer instanceof StandardXYItemRenderer) {
122             StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
123             rr.setPlotShapes(true);
124             rr.setShapesFilled(true);
125         }
126         
127         DateAxis axis = (DateAxis) plot.getDomainAxis();
128         axis.setDateFormatOverride(new SimpleDateFormat JavaDoc("MMM-yyyy"));
129         
130         return chart;
131
132     }
133     
134     // ****************************************************************************
135
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
136
// * Please note that commercial support and documentation is available from: *
137
// * *
138
// * http://www.object-refinery.com/jfreechart/support.html *
139
// * *
140
// * This is not only a great service for developers, but is a VERY IMPORTANT *
141
// * source of funding for the JFreeChart project. Please support us so that *
142
// * we can continue developing free software. *
143
// ****************************************************************************
144

145     /**
146      * Creates a dataset, consisting of two series of monthly data.
147      *
148      * @return the dataset.
149      */

150     private XYDataset createDataset() {
151
152         TimeSeries s1 = new TimeSeries("L&G European Index Trust", Month.class);
153         s1.add(new Month(2, 2001), 181.8);
154         s1.add(new Month(3, 2001), 167.3);
155         s1.add(new Month(4, 2001), 153.8);
156         s1.add(new Month(5, 2001), 167.6);
157         s1.add(new Month(6, 2001), 158.8);
158         s1.add(new Month(7, 2001), 148.3);
159         s1.add(new Month(8, 2001), 153.9);
160         s1.add(new Month(9, 2001), 142.7);
161         s1.add(new Month(10, 2001), 123.2);
162         s1.add(new Month(11, 2001), 131.8);
163         s1.add(new Month(12, 2001), 139.6);
164         s1.add(new Month(1, 2002), 142.9);
165         s1.add(new Month(2, 2002), 138.7);
166         s1.add(new Month(3, 2002), 137.3);
167         s1.add(new Month(4, 2002), 143.9);
168         s1.add(new Month(5, 2002), 139.8);
169         s1.add(new Month(6, 2002), 137.0);
170         s1.add(new Month(7, 2002), 132.8);
171
172         TimeSeries s2 = new TimeSeries("L&G UK Index Trust", Month.class);
173         s2.add(new Month(2, 2001), 129.6);
174         s2.add(new Month(3, 2001), 123.2);
175         s2.add(new Month(4, 2001), 117.2);
176         s2.add(new Month(5, 2001), 124.1);
177         s2.add(new Month(6, 2001), 122.6);
178         s2.add(new Month(7, 2001), 119.2);
179         s2.add(new Month(8, 2001), 116.5);
180         s2.add(new Month(9, 2001), 112.7);
181         s2.add(new Month(10, 2001), 101.5);
182         s2.add(new Month(11, 2001), 106.1);
183         s2.add(new Month(12, 2001), 110.3);
184         s2.add(new Month(1, 2002), 111.7);
185         s2.add(new Month(2, 2002), 111.0);
186         s2.add(new Month(3, 2002), 109.6);
187         s2.add(new Month(4, 2002), 113.2);
188         s2.add(new Month(5, 2002), 111.6);
189         s2.add(new Month(6, 2002), 108.8);
190         s2.add(new Month(7, 2002), 101.6);
191
192         TimeSeriesCollection dataset = new TimeSeriesCollection();
193         dataset.addSeries(s1);
194         dataset.addSeries(s2);
195
196         dataset.setDomainIsPointsInTime(true);
197
198         return dataset;
199
200     }
201
202     /**
203      * Starting point for the demonstration application.
204      *
205      * @param args ignored.
206      */

207     public static void main(String JavaDoc[] args) {
208
209         TimeSeriesDemo demo = new TimeSeriesDemo("Time Series Demo 1");
210         demo.pack();
211         RefineryUtilities.centerFrameOnScreen(demo);
212         demo.setVisible(true);
213
214     }
215
216 }
217
Popular Tags