KickJava   Java API By Example, From Geeks To Geeks.

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


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 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  * TimePeriodValuesDemo.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: TimePeriodValuesDemo.java,v 1.12 2003/11/28 10:57:36 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.text.SimpleDateFormat JavaDoc;
42
43 import org.jfree.chart.ChartPanel;
44 import org.jfree.chart.JFreeChart;
45 import org.jfree.chart.axis.DateAxis;
46 import org.jfree.chart.axis.DateTickUnit;
47 import org.jfree.chart.axis.NumberAxis;
48 import org.jfree.chart.axis.ValueAxis;
49 import org.jfree.chart.plot.XYPlot;
50 import org.jfree.chart.renderer.StandardXYItemRenderer;
51 import org.jfree.chart.renderer.XYBarRenderer;
52 import org.jfree.chart.renderer.XYItemRenderer;
53 import org.jfree.data.XYDataset;
54 import org.jfree.data.time.Day;
55 import org.jfree.data.time.Hour;
56 import org.jfree.data.time.Minute;
57 import org.jfree.data.time.SimpleTimePeriod;
58 import org.jfree.data.time.TimePeriodValues;
59 import org.jfree.data.time.TimePeriodValuesCollection;
60 import org.jfree.ui.ApplicationFrame;
61 import org.jfree.ui.RefineryUtilities;
62
63 /**
64  * An example of....
65  *
66  * @author David Gilbert
67  */

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

75     public TimePeriodValuesDemo(String JavaDoc title) {
76
77         super(title);
78
79         XYDataset data1 = createDataset1();
80         XYItemRenderer renderer1 = new XYBarRenderer();
81         
82         DateAxis domainAxis = new DateAxis("Date");
83         domainAxis.setVerticalTickLabels(true);
84         domainAxis.setTickUnit(new DateTickUnit(DateTickUnit.HOUR, 1));
85         domainAxis.setDateFormatOverride(new SimpleDateFormat JavaDoc("hh:mm"));
86         domainAxis.setLowerMargin(0.01);
87         domainAxis.setUpperMargin(0.01);
88         ValueAxis rangeAxis = new NumberAxis("Value");
89         
90         XYPlot plot = new XYPlot(data1, domainAxis, rangeAxis, renderer1);
91
92         XYDataset data2 = createDataset2();
93         StandardXYItemRenderer renderer2
94             = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES_AND_LINES);
95         renderer2.setShapesFilled(true);
96         
97         plot.setSecondaryDataset(0, data2);
98         plot.setSecondaryRenderer(0, renderer2);
99         //plot.mapSecondaryDatasetToRangeAxis(0, null);
100

101         JFreeChart chart = new JFreeChart("Supply and Demand", plot);
102         ChartPanel chartPanel = new ChartPanel(chart);
103         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
104         chartPanel.setMouseZoomable(true, false);
105         setContentPane(chartPanel);
106
107     }
108
109     // ****************************************************************************
110
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
111
// * Please note that commercial support and documentation is available from: *
112
// * *
113
// * http://www.object-refinery.com/jfreechart/support.html *
114
// * *
115
// * This is not only a great service for developers, but is a VERY IMPORTANT *
116
// * source of funding for the JFreeChart project. Please support us so that *
117
// * we can continue developing free software. *
118
// ****************************************************************************
119

120     /**
121      * Creates a dataset, consisting of two series of monthly data.
122      *
123      * @return the dataset.
124      */

125     public XYDataset createDataset1() {
126
127         TimePeriodValues s1 = new TimePeriodValues("Supply");
128         TimePeriodValues s2 = new TimePeriodValues("Demand");
129         Day today = new Day();
130         for (int i = 0; i < 24; i++) {
131             Minute m0 = new Minute(0, new Hour(i, today));
132             Minute m1 = new Minute(15, new Hour(i, today));
133             Minute m2 = new Minute(30, new Hour(i, today));
134             Minute m3 = new Minute(45, new Hour(i, today));
135             Minute m4 = new Minute(0, new Hour(i + 1, today));
136             s1.add(new SimpleTimePeriod(m0.getStart(), m1.getStart()), Math.random());
137             s2.add(new SimpleTimePeriod(m1.getStart(), m2.getStart()), Math.random());
138             s1.add(new SimpleTimePeriod(m2.getStart(), m3.getStart()), Math.random());
139             s2.add(new SimpleTimePeriod(m3.getStart(), m4.getStart()), Math.random());
140         }
141
142         TimePeriodValuesCollection dataset = new TimePeriodValuesCollection();
143         dataset.addSeries(s1);
144         dataset.addSeries(s2);
145
146         return dataset;
147
148     }
149
150     /**
151      * Creates a dataset, consisting of two series of monthly data.
152      *
153      * @return the dataset.
154      */

155     public XYDataset createDataset2() {
156
157         TimePeriodValues s1 = new TimePeriodValues("WebCOINS");
158         Day today = new Day();
159         for (int i = 0; i < 24; i++) {
160             Minute m0 = new Minute(0, new Hour(i, today));
161             Minute m1 = new Minute(30, new Hour(i, today));
162             Minute m2 = new Minute(0, new Hour(i + 1, today));
163             s1.add(new SimpleTimePeriod(m0.getStart(), m1.getStart()), Math.random() * 2.0);
164             s1.add(new SimpleTimePeriod(m1.getStart(), m2.getStart()), Math.random() * 2.0);
165         }
166
167         TimePeriodValuesCollection dataset = new TimePeriodValuesCollection();
168         dataset.addSeries(s1);
169
170         return dataset;
171
172     }
173
174     /**
175      * Starting point for the demonstration application.
176      *
177      * @param args ignored.
178      */

179     public static void main(String JavaDoc[] args) {
180
181         TimePeriodValuesDemo demo = new TimePeriodValuesDemo("Time Period Values Demo 1");
182         demo.pack();
183         RefineryUtilities.centerFrameOnScreen(demo);
184         demo.setVisible(true);
185
186     }
187
188 }
189
Popular Tags