KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > gui > chart > demos > AdvancedDynamicChart


1 /*
2  *
3  * MinimalStaticChart.java jchart2d
4  * Copyright (C) Achim Westermann, created on 10.12.2004, 13:48:55
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  * If you modify or optimize the code in a useful way please let me know.
21  * Achim.Westermann@gmx.de
22  *
23  */

24 package info.monitorenter.gui.chart.demos;
25
26 import info.monitorenter.gui.chart.AAxis;
27 import info.monitorenter.gui.chart.Chart2D;
28 import info.monitorenter.gui.chart.ITrace2D;
29 import info.monitorenter.gui.chart.axis.AxisLinear;
30 import info.monitorenter.gui.chart.io.ADataCollector;
31 import info.monitorenter.gui.chart.io.RandomDataCollectorTimeStamped;
32 import info.monitorenter.gui.chart.labelformatters.LabelFormatterDate;
33 import info.monitorenter.gui.chart.labelformatters.LabelFormatterNumber;
34 import info.monitorenter.gui.chart.layout.ChartPanel;
35 import info.monitorenter.gui.chart.rangepolicies.RangePolicyMinimumViewport;
36 import info.monitorenter.gui.chart.traces.Trace2DAxisSwap;
37 import info.monitorenter.gui.chart.traces.Trace2DLtd;
38 import info.monitorenter.util.Range;
39
40 import java.awt.BasicStroke JavaDoc;
41 import java.awt.Color JavaDoc;
42 import java.awt.Stroke JavaDoc;
43 import java.awt.event.WindowAdapter JavaDoc;
44 import java.awt.event.WindowEvent JavaDoc;
45 import java.text.DecimalFormat JavaDoc;
46 import java.text.NumberFormat JavaDoc;
47 import java.text.SimpleDateFormat JavaDoc;
48
49 import javax.swing.JFrame JavaDoc;
50
51
52 /**
53  * <p>
54  * An example that introduces some more advanced features of jchart2d.
55  * </p>
56  *
57  * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann </a>
58  *
59  * @version $Revision: 1.1 $
60  *
61  */

62 public final class AdvancedDynamicChart {
63
64   /**
65    * Creates a new JFrame and adds a chart that uses advanced features.
66    * <p>
67    *
68    * @param args
69    * command line arguments, unused.
70    */

71   public static void main(final String JavaDoc[] args) {
72     // Create a chart:
73
Chart2D chart = new Chart2D();
74
75     // We want to use a date format for the y axis.
76
// Currently works only this way:
77
AAxis yAxis = new AxisLinear();
78
79     // Number formatter does not work for AxisAutoUnit.
80
AAxis xAxis = new AxisLinear();
81
82     // Set a date formatter:
83
yAxis.setFormatter(new LabelFormatterDate(new SimpleDateFormat JavaDoc("HH:mm:ss")));
84     chart.setAxisY(yAxis);
85
86     // set a number formatter to get rid of the unnecessary ".0" prefixes for
87
// the X-Axis:
88
NumberFormat JavaDoc format = new DecimalFormat JavaDoc("#");
89     // Important!
90
// Or it will allow more than 100 integer digits and rendering will be
91
// confused.
92
// See the comment for java.text.DecimalFormat#applyPattern(String)
93
format.setMaximumIntegerDigits(3);
94     xAxis.setFormatter(new LabelFormatterNumber(format));
95
96     chart.setAxisX(xAxis);
97     // Try a range policy:
98
// This must not be invoked before the Axis is connected to a Chart2D
99
// (chart.setXAxis...)
100
xAxis.setRangePolicy(new RangePolicyMinimumViewport(new Range(-10, +10)));
101     // Create an ITrace:
102
// Note that dynamic charts need limited amount of values!!!
103

104     ITrace2D trace = new Trace2DLtd(20);
105     trace.setColor(Color.RED);
106
107     // set a stroke (pattern to render the trace)
108
Stroke JavaDoc stroke = new BasicStroke JavaDoc(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 10.0f,
109     // dash pattern, dash limit
110
new float[]{15f, 10f }, 5f);
111     trace.setStroke(stroke);
112
113     // Add the trace to the chart:
114
chart.addTrace(trace);
115
116     // Make it visible:
117
// Create a frame.
118
JFrame JavaDoc frame = new JFrame JavaDoc("AdvancedDynamicChart");
119     // add the chart to the frame:
120
frame.getContentPane().add(new ChartPanel(chart));
121     frame.setSize(400, 300);
122     // Enable the termination button [cross on the upper right edge]:
123
frame.addWindowListener(new WindowAdapter JavaDoc() {
124       public void windowClosing(final WindowEvent JavaDoc e) {
125         System.exit(0);
126       }
127     });
128     frame.setVisible(true);
129     // Every 500 milliseconds a new value is collected.
130
// The AxisSwap changes x and y data. Just a proof of concept.
131
ADataCollector collector = new RandomDataCollectorTimeStamped(
132         new Trace2DAxisSwap(trace), 500);
133     collector.start();
134   }
135
136   /** Private constructor. * */
137   private AdvancedDynamicChart() {
138     super();
139   }
140 }
141
Popular Tags