KickJava   Java API By Example, From Geeks To Geeks.

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


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

37
38 package org.jfree.chart.demo;
39
40 import java.awt.BorderLayout JavaDoc;
41 import java.awt.Color JavaDoc;
42 import java.awt.FlowLayout JavaDoc;
43 import java.awt.event.ActionEvent JavaDoc;
44 import java.awt.event.ActionListener JavaDoc;
45
46 import javax.swing.JButton JavaDoc;
47 import javax.swing.JPanel JavaDoc;
48
49 import org.jfree.chart.ChartFactory;
50 import org.jfree.chart.ChartPanel;
51 import org.jfree.chart.JFreeChart;
52 import org.jfree.chart.Spacer;
53 import org.jfree.chart.axis.NumberAxis;
54 import org.jfree.chart.axis.ValueAxis;
55 import org.jfree.chart.plot.XYPlot;
56 import org.jfree.chart.renderer.DefaultXYItemRenderer;
57 import org.jfree.data.time.Millisecond;
58 import org.jfree.data.time.TimeSeries;
59 import org.jfree.data.time.TimeSeriesCollection;
60 import org.jfree.ui.ApplicationFrame;
61 import org.jfree.ui.RefineryUtilities;
62
63 /**
64  * A demonstration application showing a time series chart where you can dynamically add
65  * (random) data by clicking on a button.
66  *
67  * @author David Gilbert
68  */

69 public class DynamicDataDemo2 extends ApplicationFrame implements ActionListener JavaDoc {
70
71     /** Series 1. */
72     private TimeSeries series1;
73     
74     /** Series 2. */
75     private TimeSeries series2;
76
77     /** The most recent value added to series 1. */
78     private double lastValue1 = 100.0;
79
80     /** The most recent value added to series 2. */
81     private double lastValue2 = 500.0;
82
83     /**
84      * Constructs a new demonstration application.
85      *
86      * @param title the frame title.
87      */

88     public DynamicDataDemo2(String JavaDoc title) {
89
90         super(title);
91         this.series1 = new TimeSeries("Random 1", Millisecond.class);
92         this.series2 = new TimeSeries("Random 2", Millisecond.class);
93         TimeSeriesCollection dataset1 = new TimeSeriesCollection(this.series1);
94         TimeSeriesCollection dataset2 = new TimeSeriesCollection(this.series2);
95         JFreeChart chart = ChartFactory.createTimeSeriesChart(
96             "Dynamic Data Demo 2", "Time", "Value", dataset1, true, true, false
97         );
98         chart.setBackgroundPaint(Color.white);
99         
100         XYPlot plot = chart.getXYPlot();
101         plot.setBackgroundPaint(Color.lightGray);
102         plot.setDomainGridlinePaint(Color.white);
103         plot.setRangeGridlinePaint(Color.white);
104         plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 4, 4, 4, 4));
105         ValueAxis axis = plot.getDomainAxis();
106         axis.setAutoRange(true);
107         axis.setFixedAutoRange(60000.0); // 60 seconds
108

109         plot.setSecondaryDataset(0, dataset2);
110         NumberAxis rangeAxis2 = new NumberAxis("Range Axis 2");
111         rangeAxis2.setAutoRangeIncludesZero(false);
112         plot.setSecondaryRenderer(0, new DefaultXYItemRenderer());
113         plot.setSecondaryRangeAxis(0, rangeAxis2);
114         plot.mapSecondaryDatasetToRangeAxis(0, new Integer JavaDoc(0));
115         
116         JPanel JavaDoc content = new JPanel JavaDoc(new BorderLayout JavaDoc());
117
118         ChartPanel chartPanel = new ChartPanel(chart);
119         content.add(chartPanel);
120         
121         JButton JavaDoc button1 = new JButton JavaDoc("Add To Series 1");
122         button1.setActionCommand("ADD_DATA_1");
123         button1.addActionListener(this);
124         
125         JButton JavaDoc button2 = new JButton JavaDoc("Add To Series 2");
126         button2.setActionCommand("ADD_DATA_2");
127         button2.addActionListener(this);
128
129         JButton JavaDoc button3 = new JButton JavaDoc("Add To Both");
130         button3.setActionCommand("ADD_BOTH");
131         button3.addActionListener(this);
132
133         JPanel JavaDoc buttonPanel = new JPanel JavaDoc(new FlowLayout JavaDoc());
134         buttonPanel.add(button1);
135         buttonPanel.add(button2);
136         buttonPanel.add(button3);
137         
138         content.add(buttonPanel, BorderLayout.SOUTH);
139         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
140         setContentPane(content);
141
142     }
143
144     // ****************************************************************************
145
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
146
// * Please note that commercial support and documentation is available from: *
147
// * *
148
// * http://www.object-refinery.com/jfreechart/support.html *
149
// * *
150
// * This is not only a great service for developers, but is a VERY IMPORTANT *
151
// * source of funding for the JFreeChart project. Please support us so that *
152
// * we can continue developing free software. *
153
// ****************************************************************************
154

155     /**
156      * Handles a click on the button by adding new (random) data.
157      *
158      * @param e the action event.
159      */

160     public void actionPerformed(ActionEvent JavaDoc e) {
161         boolean add1 = false;
162         boolean add2 = false;
163         if (e.getActionCommand().equals("ADD_DATA_1")) {
164             add1 = true;
165         }
166         else if (e.getActionCommand().equals("ADD_DATA_2")) {
167             add2 = true;
168         }
169         else if (e.getActionCommand().equals("ADD_BOTH")) {
170             add1 = true;
171             add2 = true;
172         }
173         if (add1) {
174             double factor = 0.90 + 0.2 * Math.random();
175             this.lastValue1 = lastValue1 * factor;
176             Millisecond now = new Millisecond();
177             System.out.println("Now = " + now.toString());
178             this.series1.add(new Millisecond(), lastValue1);
179         }
180         if (add2) {
181             double factor = 0.90 + 0.2 * Math.random();
182             this.lastValue2 = lastValue2 * factor;
183             Millisecond now = new Millisecond();
184             System.out.println("Now = " + now.toString());
185             this.series2.add(new Millisecond(), lastValue2);
186         }
187     }
188
189     /**
190      * Starting point for the demonstration application.
191      *
192      * @param args ignored.
193      */

194     public static void main(String JavaDoc[] args) {
195
196         DynamicDataDemo2 demo = new DynamicDataDemo2("Dynamic Data Demo 2");
197         demo.pack();
198         RefineryUtilities.centerFrameOnScreen(demo);
199         demo.setVisible(true);
200
201     }
202
203 }
204
Popular Tags