KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CyclicXYPlot.java
24  * ------------------
25  * (C) Copyright 2002, 2003, by Object Refinery Limited and Contributors.
26  *
27  * Original Author: Nicolas Brodu
28  * Contributor(s): -;
29  *
30  * $Id: CyclicXYPlotDemo.java,v 1.1 2003/11/20 08:54:13 brodu Exp $
31  *
32  * Changes
33  * -------
34  * 20-Nov-2003 : Creation Date (NB)
35  *
36  */

37
38 package org.jfree.chart.demo;
39
40 import java.awt.BorderLayout JavaDoc;
41 import java.awt.FlowLayout JavaDoc;
42 import java.awt.event.ActionEvent JavaDoc;
43 import java.awt.event.ActionListener JavaDoc;
44
45 import javax.swing.JButton JavaDoc;
46 import javax.swing.JPanel JavaDoc;
47 import javax.swing.Timer 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.axis.CyclicNumberAxis;
53 import org.jfree.chart.axis.NumberAxis;
54 import org.jfree.chart.plot.PlotOrientation;
55 import org.jfree.chart.plot.XYPlot;
56 import org.jfree.chart.renderer.CyclicXYItemRenderer;
57 import org.jfree.data.XYSeries;
58 import org.jfree.data.XYSeriesCollection;
59 import org.jfree.ui.ApplicationFrame;
60 import org.jfree.ui.RefineryUtilities;
61
62 /**
63  * Demo for an XY plot, with a cyclic axis and renderer
64  *
65  * @author Nicolas Brodu
66  */

67 public class CyclicXYPlotDemo extends ApplicationFrame implements ActionListener JavaDoc {
68
69     XYSeries series;
70     long x = 0;
71     double y = 50;
72     Timer JavaDoc timer;
73     
74     /**
75      * A demonstration application showing an XY plot, with a cyclic axis and renderer
76      *
77      * @param title the frame title.
78      */

79     public CyclicXYPlotDemo(String JavaDoc title) {
80
81         super(title);
82
83         series = new XYSeries("Random Data");
84         series.setMaximumItemCount(50); // Only 50 items are visible at the same time. Keep more as a mean to test this.
85
XYSeriesCollection data = new XYSeriesCollection(series);
86         
87         JFreeChart chart = ChartFactory.createXYLineChart(
88             "Cyclic XY Plot Demo",
89             "X",
90             "Y",
91             data,
92             PlotOrientation.VERTICAL,
93             true,
94             true,
95             false
96         );
97
98         XYPlot plot = chart.getXYPlot();
99         plot.setDomainAxis(new CyclicNumberAxis(10,0));
100         plot.setRenderer(new CyclicXYItemRenderer());
101
102         NumberAxis axis = (NumberAxis) plot.getRangeAxis();
103         axis.setAutoRangeIncludesZero(false);
104         axis.setAutoRangeMinimumSize(1.0);
105
106         ChartPanel chartPanel = new ChartPanel(chart);
107         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(400, 300));
108         JPanel JavaDoc content = new JPanel JavaDoc(new BorderLayout JavaDoc());
109         content.add(chartPanel,BorderLayout.CENTER);
110
111         JButton JavaDoc button1 = new JButton JavaDoc("Start");
112         button1.addActionListener(new ActionListener JavaDoc() {
113             public void actionPerformed(ActionEvent JavaDoc e) {
114                 timer.start();
115             }
116         });
117         
118         JButton JavaDoc button2 = new JButton JavaDoc("Stop");
119         button2.addActionListener(new ActionListener JavaDoc() {
120             public void actionPerformed(ActionEvent JavaDoc e) {
121                 timer.stop();
122             }
123         });
124
125         JButton JavaDoc button3 = new JButton JavaDoc("Step by step");
126         button3.addActionListener(new ActionListener JavaDoc() {
127             public void actionPerformed(ActionEvent JavaDoc e) {
128                 CyclicXYPlotDemo.this.actionPerformed(null);
129             }
130         });
131
132         JPanel JavaDoc buttonPanel = new JPanel JavaDoc(new FlowLayout JavaDoc());
133         buttonPanel.add(button1);
134         buttonPanel.add(button2);
135         buttonPanel.add(button3);
136         
137         content.add(buttonPanel, BorderLayout.SOUTH);
138         setContentPane(content);
139
140         timer = new Timer JavaDoc(200, this);
141     }
142
143     /**
144      * Starting point for the demonstration application.
145      *
146      * @param args ignored.
147      */

148     public static void main(String JavaDoc[] args) {
149
150         CyclicXYPlotDemo demo = new CyclicXYPlotDemo("Cyclic XY Plot Demo");
151         demo.pack();
152         RefineryUtilities.centerFrameOnScreen(demo);
153         demo.setVisible(true);
154
155         //.start(); // Calls ourselves each half of a second
156
}
157
158     /* (non-Javadoc)
159      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
160      */

161     public void actionPerformed(ActionEvent JavaDoc e) {
162         double delta = Math.random()*10 - 5;
163         if (delta==-5.0) delta = 0; // balance chances
164
y += delta;
165         series.add(x++ / 4.0, y);
166     }
167
168 }
169
Popular Tags