KickJava   Java API By Example, From Geeks To Geeks.

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


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  * TimeSeriesDemo3.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: TimeSeriesDemo3.java,v 1.6 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 06-Aug-2002 : Version 1 (DG);
35  * 11-Oct-2002 : Fixes issues reported by Checkstyle (DG);
36  *
37  */

38
39 package org.jfree.chart.demo;
40
41 import java.text.SimpleDateFormat JavaDoc;
42
43 import org.jfree.chart.ChartFactory;
44 import org.jfree.chart.ChartPanel;
45 import org.jfree.chart.JFreeChart;
46 import org.jfree.chart.axis.DateAxis;
47 import org.jfree.chart.axis.DateTickUnit;
48 import org.jfree.chart.plot.XYPlot;
49 import org.jfree.chart.renderer.StandardXYItemRenderer;
50 import org.jfree.data.XYDataset;
51 import org.jfree.data.time.Month;
52 import org.jfree.data.time.TimeSeries;
53 import org.jfree.data.time.TimeSeriesCollection;
54 import org.jfree.ui.ApplicationFrame;
55 import org.jfree.ui.RefineryUtilities;
56
57 /**
58  * A time series demo, with monthly data, where the tick unit on the axis is set to
59  * one month also (this switches off the auto tick unit selection, and *can* result in
60  * overlapping labels).
61  *
62  * @author David Gilbert
63  */

64 public class TimeSeriesDemo3 extends ApplicationFrame {
65
66     /**
67      * A demonstration application showing a quarterly time series containing a null value.
68      *
69      * @param title the frame title.
70      */

71     public TimeSeriesDemo3(String JavaDoc title) {
72
73         super(title);
74
75         TimeSeries series1 = new TimeSeries("Series 1", Month.class);
76         series1.add(new Month(1, 2002), 500.2);
77         series1.add(new Month(2, 2002), 694.1);
78         series1.add(new Month(3, 2002), 734.4);
79         series1.add(new Month(4, 2002), 453.2);
80         series1.add(new Month(5, 2002), 500.2);
81         series1.add(new Month(6, 2002), 345.6);
82         series1.add(new Month(7, 2002), 500.2);
83         series1.add(new Month(8, 2002), 694.1);
84         series1.add(new Month(9, 2002), 734.4);
85         series1.add(new Month(10, 2002), 453.2);
86         series1.add(new Month(11, 2002), 500.2);
87         series1.add(new Month(12, 2002), 345.6);
88
89         TimeSeries series2 = new TimeSeries("Series 2", Month.class);
90         series2.add(new Month(1, 2002), 234.1);
91         series2.add(new Month(2, 2002), 623.7);
92         series2.add(new Month(3, 2002), 642.5);
93         series2.add(new Month(4, 2002), 651.4);
94         series2.add(new Month(5, 2002), 643.5);
95         series2.add(new Month(6, 2002), 785.6);
96         series2.add(new Month(7, 2002), 234.1);
97         series2.add(new Month(8, 2002), 623.7);
98         series2.add(new Month(9, 2002), 642.5);
99         series2.add(new Month(10, 2002), 651.4);
100         series2.add(new Month(11, 2002), 643.5);
101         series2.add(new Month(12, 2002), 785.6);
102
103         TimeSeriesCollection dataset = new TimeSeriesCollection();
104         dataset.addSeries(series1);
105         dataset.addSeries(series2);
106
107         JFreeChart chart = createChart(dataset);
108         
109         ChartPanel chartPanel = new ChartPanel(chart);
110         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
111         setContentPane(chartPanel);
112
113     }
114
115     // ****************************************************************************
116
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
117
// * Please note that commercial support and documentation is available from: *
118
// * *
119
// * http://www.object-refinery.com/jfreechart/support.html *
120
// * *
121
// * This is not only a great service for developers, but is a VERY IMPORTANT *
122
// * source of funding for the JFreeChart project. Please support us so that *
123
// * we can continue developing free software. *
124
// ****************************************************************************
125

126     /**
127      * Creates a new chart.
128      *
129      * @param dataset the dataset.
130      *
131      * @return The dataset.
132      */

133     private JFreeChart createChart(XYDataset dataset) {
134         JFreeChart chart = ChartFactory.createTimeSeriesChart(
135             "Time Series Demo 3",
136             "Time",
137             "Value",
138             dataset,
139             true,
140             true,
141             false
142         );
143         XYPlot plot = chart.getXYPlot();
144         DateAxis axis = (DateAxis) plot.getDomainAxis();
145         axis.setTickUnit(new DateTickUnit(DateTickUnit.MONTH, 1,
146                                           new SimpleDateFormat JavaDoc("MMM-yyyy")));
147         axis.setVerticalTickLabels(true);
148         
149         StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer();
150         renderer.setPlotShapes(true);
151         renderer.setSeriesShapesFilled(0, Boolean.TRUE);
152         renderer.setSeriesShapesFilled(1, Boolean.FALSE);
153
154         return chart;
155     }
156     
157     /**
158      * Starting point for the demonstration application.
159      *
160      * @param args ignored.
161      */

162     public static void main(String JavaDoc[] args) {
163
164         TimeSeriesDemo3 demo = new TimeSeriesDemo3("Time Series Demo 3");
165         demo.pack();
166         RefineryUtilities.centerFrameOnScreen(demo);
167         demo.setVisible(true);
168
169     }
170
171 }
172
Popular Tags