KickJava   Java API By Example, From Geeks To Geeks.

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


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  * TimeSeriesDemo11.java
24  * ---------------------
25  * (C) Copyright 2003 by Object Refinery Limited and Contributors.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: TimeSeriesDemo11.java,v 1.3 2003/11/28 10:57:35 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 12-Aug-2003 : Version 1 (DG);
35  *
36  */

37
38 package org.jfree.chart.demo;
39
40 import java.awt.Color JavaDoc;
41 import java.awt.GridLayout JavaDoc;
42
43 import javax.swing.JPanel JavaDoc;
44
45 import org.jfree.chart.ChartFactory;
46 import org.jfree.chart.ChartPanel;
47 import org.jfree.chart.JFreeChart;
48 import org.jfree.chart.Spacer;
49 import org.jfree.chart.axis.DateAxis;
50 import org.jfree.chart.plot.PlotOrientation;
51 import org.jfree.chart.plot.XYPlot;
52 import org.jfree.chart.renderer.StandardXYItemRenderer;
53 import org.jfree.data.XYDataset;
54 import org.jfree.data.time.Day;
55 import org.jfree.data.time.RegularTimePeriod;
56 import org.jfree.data.time.TimeSeries;
57 import org.jfree.data.time.TimeSeriesCollection;
58 import org.jfree.date.SerialDate;
59 import org.jfree.ui.ApplicationFrame;
60 import org.jfree.ui.RefineryUtilities;
61
62 /**
63  * An example of....
64  *
65  * @author David Gilbert
66  */

67 public class TimeSeriesDemo11 extends ApplicationFrame {
68
69     /**
70      * A demonstration application showing how to...
71      *
72      * @param title the frame title.
73      */

74     public TimeSeriesDemo11(String JavaDoc title) {
75
76         super(title);
77         JPanel JavaDoc panel = new JPanel JavaDoc(new GridLayout JavaDoc(2, 2));
78         panel.setPreferredSize(new java.awt.Dimension JavaDoc(800, 600));
79         
80         Day today = new Day();
81         XYDataset dataset = createDataset("Series 1", 100.0, today, 365);
82
83         JFreeChart chart1 = createChart("Chart 1 : 1 Year", dataset);
84         ChartPanel chartPanel1 = new ChartPanel(chart1);
85         panel.add(chartPanel1);
86         
87         JFreeChart chart2 = createChart("Chart 2 : 6 Months", dataset);
88         SerialDate t = today.getSerialDate();
89         SerialDate t6m = SerialDate.addMonths(-6, t);
90         Day sixMonthsAgo = new Day(t6m);
91         DateAxis axis2 = (DateAxis) chart2.getXYPlot().getDomainAxis();
92         axis2.setRange(sixMonthsAgo.getStart(), today.getEnd());
93         ChartPanel chartPanel2 = new ChartPanel(chart2);
94         panel.add(chartPanel2);
95         
96         JFreeChart chart3 = createChart("Chart 3 : 3 Months", dataset);
97         SerialDate t3m = SerialDate.addMonths(-3, t);
98         Day threeMonthsAgo = new Day(t3m);
99         DateAxis axis3 = (DateAxis) chart3.getXYPlot().getDomainAxis();
100         axis3.setRange(threeMonthsAgo.getStart(), today.getEnd());
101         ChartPanel chartPanel3 = new ChartPanel(chart3);
102         panel.add(chartPanel3);
103         
104         JFreeChart chart4 = createChart("Chart 4 : 1 Month", dataset);
105         SerialDate t1m = SerialDate.addMonths(-1, t);
106         Day oneMonthsAgo = new Day(t1m);
107         DateAxis axis4 = (DateAxis) chart4.getXYPlot().getDomainAxis();
108         axis4.setRange(oneMonthsAgo.getStart(), today.getEnd());
109         ChartPanel chartPanel4 = new ChartPanel(chart4);
110         panel.add(chartPanel4);
111         
112         setContentPane(panel);
113
114     }
115
116     // ****************************************************************************
117
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
118
// * Please note that commercial support and documentation is available from: *
119
// * *
120
// * http://www.object-refinery.com/jfreechart/support.html *
121
// * *
122
// * This is not only a great service for developers, but is a VERY IMPORTANT *
123
// * source of funding for the JFreeChart project. Please support us so that *
124
// * we can continue developing free software. *
125
// ****************************************************************************
126

127     /**
128      * Creates the demo chart.
129      *
130      * @param title the title.
131      * @param dataset the dataset.
132      *
133      * @return The chart.
134      */

135     private JFreeChart createChart(String JavaDoc title, XYDataset dataset) {
136
137         
138         JFreeChart chart = ChartFactory.createTimeSeriesChart(
139             title,
140             "Date",
141             "Price",
142             dataset,
143             true,
144             true,
145             false
146         );
147
148         chart.setBackgroundPaint(Color.white);
149         XYPlot plot = chart.getXYPlot();
150         plot.setOrientation(PlotOrientation.VERTICAL);
151         plot.setBackgroundPaint(Color.lightGray);
152         plot.setDomainGridlinePaint(Color.white);
153         plot.setRangeGridlinePaint(Color.white);
154         plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
155         
156         StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer();
157         renderer.setPaint(Color.blue);
158                
159         return chart;
160         
161     }
162     
163     /**
164      * Creates a sample dataset.
165      *
166      * @param name the dataset name.
167      * @param base the starting value.
168      * @param start the starting period.
169      * @param count the number of values to generate.
170      *
171      * @return The dataset.
172      */

173     private XYDataset createDataset(String JavaDoc name, double base, RegularTimePeriod start, int count) {
174
175         TimeSeries series = new TimeSeries(name, start.getClass());
176         RegularTimePeriod period = start;
177         double value = base;
178         for (int i = 0; i < count; i++) {
179             series.add(period, value);
180             period = period.previous();
181             value = value * (1 + (Math.random() - 0.495) / 10.0);
182         }
183
184         TimeSeriesCollection dataset = new TimeSeriesCollection();
185         dataset.addSeries(series);
186
187         return dataset;
188
189     }
190
191     /**
192      * Starting point for the demonstration application.
193      *
194      * @param args ignored.
195      */

196     public static void main(String JavaDoc[] args) {
197
198         TimeSeriesDemo11 demo = new TimeSeriesDemo11("Time Series Demo 11");
199         demo.pack();
200         RefineryUtilities.centerFrameOnScreen(demo);
201         demo.setVisible(true);
202
203     }
204
205 }
206
Popular Tags