KickJava   Java API By Example, From Geeks To Geeks.

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


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

37
38 package org.jfree.chart.demo;
39
40 import java.awt.BorderLayout JavaDoc;
41 import java.awt.event.ActionEvent JavaDoc;
42 import java.awt.event.ActionListener JavaDoc;
43
44 import javax.swing.JButton JavaDoc;
45 import javax.swing.JPanel JavaDoc;
46
47 import org.jfree.chart.ChartFactory;
48 import org.jfree.chart.ChartPanel;
49 import org.jfree.chart.JFreeChart;
50 import org.jfree.chart.axis.ValueAxis;
51 import org.jfree.chart.plot.XYPlot;
52 import org.jfree.data.time.Millisecond;
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  * A demonstration application showing a time series chart where you can dynamically add
60  * (random) data by clicking on a button.
61  *
62  * @author David Gilbert
63  */

64 public class DynamicDataDemo extends ApplicationFrame implements ActionListener JavaDoc {
65
66     /** The time series data. */
67     private TimeSeries series;
68
69     /** The most recent value added. */
70     private double lastValue = 100.0;
71
72     /**
73      * Constructs a new demonstration application.
74      *
75      * @param title the frame title.
76      */

77     public DynamicDataDemo(String JavaDoc title) {
78
79         super(title);
80         this.series = new TimeSeries("Random Data", Millisecond.class);
81         TimeSeriesCollection dataset = new TimeSeriesCollection(series);
82         JFreeChart chart = ChartFactory.createTimeSeriesChart(
83             "Dynamic Data Demo",
84             "Time",
85             "Value",
86             dataset,
87             true,
88             true,
89             false
90         );
91         XYPlot plot = chart.getXYPlot();
92         ValueAxis axis = plot.getDomainAxis();
93         axis.setAutoRange(true);
94         axis.setFixedAutoRange(60000.0); // 60 seconds
95

96         axis = plot.getRangeAxis();
97         axis.setRange(0.0, 200.0);
98
99         JPanel JavaDoc content = new JPanel JavaDoc(new BorderLayout JavaDoc());
100
101         ChartPanel chartPanel = new ChartPanel(chart);
102         JButton JavaDoc button = new JButton JavaDoc("Add New Data Item");
103         button.setActionCommand("ADD_DATA");
104         button.addActionListener(this);
105         content.add(chartPanel);
106         content.add(button, BorderLayout.SOUTH);
107         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
108         setContentPane(content);
109
110     }
111
112     // ****************************************************************************
113
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
114
// * Please note that commercial support and documentation is available from: *
115
// * *
116
// * http://www.object-refinery.com/jfreechart/support.html *
117
// * *
118
// * This is not only a great service for developers, but is a VERY IMPORTANT *
119
// * source of funding for the JFreeChart project. Please support us so that *
120
// * we can continue developing free software. *
121
// ****************************************************************************
122

123     /**
124      * Handles a click on the button by adding new (random) data.
125      *
126      * @param e the action event.
127      */

128     public void actionPerformed(ActionEvent JavaDoc e) {
129         if (e.getActionCommand().equals("ADD_DATA")) {
130             double factor = 0.90 + 0.2 * Math.random();
131             this.lastValue = lastValue * factor;
132             Millisecond now = new Millisecond();
133             System.out.println("Now = " + now.toString());
134             this.series.add(new Millisecond(), lastValue);
135         }
136     }
137
138     /**
139      * Starting point for the demonstration application.
140      *
141      * @param args ignored.
142      */

143     public static void main(String JavaDoc[] args) {
144
145         DynamicDataDemo demo = new DynamicDataDemo("Dynamic Data Demo");
146         demo.pack();
147         RefineryUtilities.centerFrameOnScreen(demo);
148         demo.setVisible(true);
149
150     }
151
152 }
153
154
155
156
157
158
159
160
Popular Tags