KickJava   Java API By Example, From Geeks To Geeks.

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


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  * TimeSeriesDemo9.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: TimeSeriesDemo9.java,v 1.6 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 11-Feb-2003 : Version 1 (DG);
35  *
36  */

37
38 package org.jfree.chart.demo;
39
40 import java.awt.geom.Ellipse2D JavaDoc;
41 import java.awt.geom.GeneralPath JavaDoc;
42 import java.awt.geom.Rectangle2D 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.StandardLegend;
48 import org.jfree.chart.plot.XYPlot;
49 import org.jfree.chart.renderer.StandardXYItemRenderer;
50 import org.jfree.chart.renderer.XYItemRenderer;
51 import org.jfree.data.XYDataset;
52 import org.jfree.data.time.Day;
53 import org.jfree.data.time.TimeSeries;
54 import org.jfree.data.time.TimeSeriesCollection;
55 import org.jfree.ui.ApplicationFrame;
56 import org.jfree.ui.RefineryUtilities;
57
58 /**
59  * An example of a time series chart.
60  *
61  * @author David Gilbert
62  */

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

71     public TimeSeriesDemo9(String JavaDoc title) {
72
73         super(title);
74
75         // create a title...
76
String JavaDoc chartTitle = "Test";
77         XYDataset dataset = createDataset();
78
79         JFreeChart chart = ChartFactory.createTimeSeriesChart(
80             chartTitle,
81             "Date",
82             "Price Per Unit",
83             dataset,
84             true,
85             true,
86             false
87         );
88
89         StandardLegend sl = (StandardLegend) chart.getLegend();
90         sl.setDisplaySeriesShapes(true);
91
92         XYPlot plot = chart.getXYPlot();
93         XYItemRenderer r = plot.getRenderer();
94         if (r instanceof StandardXYItemRenderer) {
95             StandardXYItemRenderer renderer = (StandardXYItemRenderer) r;
96             renderer.setPlotShapes(true);
97             renderer.setShapesFilled(true);
98             renderer.setSeriesShape(0, new Ellipse2D.Double JavaDoc(-3.0, -3.0, 6.0, 6.0));
99             renderer.setSeriesShape(1, new Rectangle2D.Double JavaDoc(-3.0, -3.0, 6.0, 6.0));
100             GeneralPath JavaDoc s2 = new GeneralPath JavaDoc();
101             s2.moveTo(0.0f, -3.0f);
102             s2.lineTo(3.0f, 3.0f);
103             s2.lineTo(-3.0f, 3.0f);
104             s2.closePath();
105             renderer.setSeriesShape(2, s2);
106             GeneralPath JavaDoc s3 = new GeneralPath JavaDoc();
107             s3.moveTo(-1.0f, -3.0f);
108             s3.lineTo(1.0f, -3.0f);
109             s3.lineTo(1.0f, -1.0f);
110             s3.lineTo(3.0f, -1.0f);
111             s3.lineTo(3.0f, 1.0f);
112             s3.lineTo(1.0f, 1.0f);
113             s3.lineTo(1.0f, 3.0f);
114             s3.lineTo(-1.0f, 3.0f);
115             s3.lineTo(-1.0f, 1.0f);
116             s3.lineTo(-3.0f, 1.0f);
117             s3.lineTo(-3.0f, -1.0f);
118             s3.lineTo(-1.0f, -1.0f);
119             s3.closePath();
120             renderer.setSeriesShape(3, s3);
121         }
122
123         plot.getDomainAxis().setVisible(false);
124         plot.getRangeAxis().setVisible(false);
125         ChartPanel chartPanel = new ChartPanel(chart);
126         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
127         setContentPane(chartPanel);
128
129     }
130
131     // ****************************************************************************
132
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
133
// * Please note that commercial support and documentation is available from: *
134
// * *
135
// * http://www.object-refinery.com/jfreechart/support.html *
136
// * *
137
// * This is not only a great service for developers, but is a VERY IMPORTANT *
138
// * source of funding for the JFreeChart project. Please support us so that *
139
// * we can continue developing free software. *
140
// ****************************************************************************
141

142     /**
143      * Creates a sample dataset.
144      *
145      * @return The dataset.
146      */

147     public XYDataset createDataset() {
148
149         TimeSeriesCollection dataset = new TimeSeriesCollection();
150         for (int i = 0; i < 4; i++) {
151             dataset.addSeries(createTimeSeries(i, 10));
152         }
153         return dataset;
154
155     }
156
157     /**
158      * Creates a time series containing random daily data.
159      *
160      * @param series the series index.
161      * @param count the number of items for the series.
162      *
163      * @return the dataset.
164      */

165     public TimeSeries createTimeSeries(int series, int count) {
166
167         TimeSeries result = new TimeSeries("Series " + series , Day.class);
168
169         Day start = new Day();
170         for (int i = 0; i < count; i++) {
171             result.add(start, Math.random());
172             start = (Day) start.next();
173         }
174
175         return result;
176
177     }
178
179     /**
180      * Starting point for the demonstration application.
181      *
182      * @param args ignored.
183      */

184     public static void main(String JavaDoc[] args) {
185
186         TimeSeriesDemo9 demo = new TimeSeriesDemo9("Time Series Demo 9");
187         demo.pack();
188         RefineryUtilities.centerFrameOnScreen(demo);
189         demo.setVisible(true);
190
191     }
192
193 }
194
Popular Tags