1
2 package com.cityinforport.demo;
3 /**
4 * =============================================================
5 * JFreeChart开发:利用JFreeChart开发实时曲线
6 * =============================================================
7 * Description:该例子演示了单条曲线的简单使用方法
8 * Original Author:谢莫锋 QQ:35814522 EMAIL:xmf3000@126.com created by 2005-02-28
9 *
10 * Changes:
11 * -------------------------------------------------------------
12 * 2005-03-01 增加线程调用 by xmf
13 * 2005-03-02 界面调整 by xmf
14 * -------------------------------------------------------------
15 */
16
17 //导入java2d包
18 import java.awt.BorderLayout;
19 import java.awt.Dimension;
20 import java.awt.event.ActionEvent;
21 import java.awt.event.ActionListener;
22 import javax.swing.*;
23 import java.io.PrintStream;
24 //导入jfreechart包(chart)
25 import org.jfree.chart.ChartFactory;
26 import org.jfree.chart.ChartPanel;
27 import org.jfree.chart.JFreeChart;
28 import org.jfree.chart.axis.ValueAxis;
29 import org.jfree.chart.plot.XYPlot;
30 //导入jfreechart包(data)
31 import org.jfree.data.time.*;
32 import org.jfree.data.xy.XYDataset;
33 //导入jfreechart包(ui)
34 import org.jfree.ui.ApplicationFrame;
35 import org.jfree.ui.RefineryUtilities;
36
37 public class TimeSeriesDemo1 extends JFrame implements Runnable,ActionListener{
38 //时序图数据集
39 private TimeSeries timeseries;
40 //Value坐标轴初始值
41 private double lastValue;
42 static Class class$org$jfree$data$time$Millisecond;
43 static Thread thread1;
44
45 public static void main(String[] args){
46 TimeSeriesDemo1 TimeSeriesDemo1 = new TimeSeriesDemo1();
47 TimeSeriesDemo1.pack();
48 RefineryUtilities.centerFrameOnScreen(TimeSeriesDemo1);
49 TimeSeriesDemo1.setVisible(true);
50 startThread();
51 }
52
53 public void run(){
54 while(true){
55 try{
56 //根据实际需要在此处加入需要执行的代码
57 double d = 0.9D + 0.2D * Math.random();
58 lastValue = lastValue * d;
59 Millisecond millisecond = new Millisecond();
60 System.out.println("Now=" + millisecond.toString());
61 timeseries.add(millisecond, lastValue);
62 Thread.sleep(300);
63 }catch(InterruptedException e){}
64 }
65 }
66
67 public static void startThread(){
68 thread1.start();
69 }
70
71 public void actionPerformed(ActionEvent e){
72 if(e.getActionCommand().equals("EXIT")){
73 thread1.destroy();
74 System.exit(0);
75 }
76 }
77
78
79 public TimeSeriesDemo1(){
80 //super(new BorderLayout());
81 thread1 = new Thread(this);
82 lastValue = 100D;
83 //创建时序图对象
84 timeseries = new TimeSeries("Random Data",TimeSeriesDemo1.class$org$jfree$data$time$Millisecond != null ? TimeSeriesDemo1.class$org$jfree$data$time$Millisecond : (TimeSeriesDemo1.class$org$jfree$data$time$Millisecond = TimeSeriesDemo1.getClass("org.jfree.data.time.Millisecond")));
85 TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeseries);
86 //创建图表面板
87 ChartPanel chartpanel = new ChartPanel(createChart(timeseriescollection));
88 chartpanel.setPreferredSize(new Dimension(500,270));
89
90 JPanel jpanel = new JPanel();
91 jpanel.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));//边距为4
92 JButton jbutton = new JButton("退出");
93 jbutton.setActionCommand("EXIT");
94 jbutton.addActionListener(this);
95 jpanel.add(jbutton);
96
97 getContentPane().add(chartpanel);
98 getContentPane().add(jpanel,"South");
99 }
100
101 private JFreeChart createChart(XYDataset xydataset){
102 JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("时序图例子","时间","温度值",xydataset,true,true,false);
103 XYPlot xyplot = jfreechart.getXYPlot();
104 //纵坐标设定
105 ValueAxis valueaxis = xyplot.getDomainAxis();
106 valueaxis.setAutoRange(true);
107 valueaxis.setFixedAutoRange(60000D);
108
109 valueaxis = xyplot.getRangeAxis();
110 valueaxis.setRange(0.0D,200D);
111
112 return jfreechart;
113 }
114
115 static Class getClass(String s){
116 Class cls = null;
117 try{
118 cls = Class.forName(s);
119 }catch(ClassNotFoundException cnfe){
120 throw new NoClassDefFoundError(cnfe.getMessage());
121 }
122 return cls;
123 }
124
125 }
126
127
128 ===============================================
129 package com.cityinforport.demo;
130 /**
131 * =============================================================
132 * JFreeChart开发:利用JFreeChart开发实时曲线
133 * =============================================================
134 * Description:该例子演示了多条曲线的简单使用方法
135 * Original Author:xmf created by 2005-03-03
136 *
137 * Changes:
138 * -------------------------------------------------------------
139 * 在此处注明修改日期、修改点、修改人
140 * -------------------------------------------------------------
141 */
142
143 //导入java2d包
144 import java.awt.BorderLayout;
145 import java.awt.Dimension;
146 import java.awt.Color;
147 import java.awt.event.ActionEvent;
148 import java.awt.event.ActionListener;
149 import javax.swing.*;
150 import java.io.PrintStream;
151 //导入jfreechart包(chart)
152 import org.jfree.chart.ChartFactory;
153 import org.jfree.chart.ChartPanel;
154 import org.jfree.chart.JFreeChart;
155 import org.jfree.chart.axis.ValueAxis;
156 import org.jfree.chart.plot.XYPlot;
157 import org.jfree.chart.renderer.xy.DefaultXYItemRenderer;
158 //导入jfreechart包(data)
159 import org.jfree.data.time.*;
160 import org.jfree.data.xy.XYDataset;
161 //导入jfreechart包(ui)
162 import org.jfree.ui.*;
163
164 public class TimeSeriesDemo2 extends JFrame implements Runnable,ActionListener{
165
166 //申明实时曲线对象
167 private TimeSeries timeseries1;
168 private TimeSeries timeseries2;
169
170 //Value坐标轴初始值
171 private double lastValue1,lastValue2;
172 private double originalValue1,originalValue2;
173
174 static Class class$org$jfree$data$time$Millisecond;
175 static Thread thread1;
176
177 public static void main(String[] args){
178 TimeSeriesDemo2 TimeSeriesDemo2 = new TimeSeriesDemo2();
179 TimeSeriesDemo2.pack();
180 RefineryUtilities.centerFrameOnScreen(TimeSeriesDemo2);
181 TimeSeriesDemo2.setVisible(true);
182 startThread();
183 }
184
185 public void run(){
186 while(true){
187 try{
188 //说明:在此处添加具体的业务数据
189
190 //随机产生曲线1的数据
191 double d1 = 2.0D * Math.random();
192 lastValue1 = originalValue1 * d1;
193 Millisecond millisecond1 = new Millisecond();
194 System.out.println("Series1 Now=" + millisecond1.toString());
195 timeseries1.add(millisecond1, lastValue1);
196 //随机产生曲线2的数据
197 double d2 = 2.0D * Math.random();
198 lastValue2 = originalValue2 * d2;
199 Millisecond millisecond2 = new Millisecond();
200 System.out.println("Series2 Now=" + millisecond2.toString());
201 timeseries2.add(millisecond2,lastValue2);
202
203 Thread.sleep(500);
204 }catch(InterruptedException e){}
205 }
206 }
207
208 public static void startThread(){
209 thread1.start();
210 }
211
212 public void actionPerformed(ActionEvent e){
213 if(e.getActionCommand().equals("EXIT")){
214 thread1.interrupt();
215 System.exit(0);
216 }
217 }
218
219
220 public TimeSeriesDemo2(){
221 thread1 = new Thread(this);
222 originalValue1 = 100D;
223 originalValue2 = 100D;
224 //创建时序图对象
225 timeseries1 = new TimeSeries("热风温1",TimeSeriesDemo2.class$org$jfree$data$time$Millisecond != null ? TimeSeriesDemo2.class$org$jfree$data$time$Millisecond : (TimeSeriesDemo2.class$org$jfree$data$time$Millisecond = TimeSeriesDemo2.getClass("org.jfree.data.time.Millisecond")));
226 timeseries2 = new TimeSeries("热风温2",TimeSeriesDemo2.class$org$jfree$data$time$Millisecond != null ? TimeSeriesDemo2.class$org$jfree$data$time$Millisecond : (TimeSeriesDemo2.class$org$jfree$data$time$Millisecond = TimeSeriesDemo2.getClass("org.jfree.data.time.Millisecond")));
227 TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeseries1);
228 TimeSeriesCollection timeseriescollection1 = new TimeSeriesCollection(timeseries2);
229
230 //创建jfreechart对象
231 JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("RTU温度模拟量实时曲线图","Time","Value",
232 timeseriescollection,true,true,false);
233 jfreechart.setBackgroundPaint(Color.white);
234
235 //设定显示风格
236 XYPlot xyplot = jfreechart.getXYPlot();
237 xyplot.setBackgroundPaint(Color.lightGray);
238 xyplot.setDomainGridlinePaint(Color.white);
239 xyplot.setRangeGridlinePaint(Color.white);
240 xyplot.setAxisOffset(new Spacer(1, 4D, 4D, 4D, 4D));
241 ValueAxis valueaxis = xyplot.getDomainAxis();
242 valueaxis.setAutoRange(true);
243 valueaxis.setFixedAutoRange(60000D);
244 //设定Value的范围
245 valueaxis = xyplot.getRangeAxis();
246 valueaxis.setRange(0.0D,200D);
247 xyplot.setDataset(1, timeseriescollection1);
248 xyplot.setRenderer(1,new DefaultXYItemRenderer());
249
250 //创建图表面板
251 ChartPanel chartpanel = new ChartPanel(jfreechart);
252 getContentPane().add(chartpanel);
253
254 //根据需要添加操作按钮
255 this.setTitle("RTU实时曲线");
256 JPanel jpanel = new JPanel();
257 jpanel.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));//边距为4
258 JButton jbutton = new JButton("退出");
259 jbutton.setActionCommand("EXIT");
260 jbutton.addActionListener(this);
261 jpanel.add(jbutton);
262 getContentPane().add(jpanel,"South");
263 chartpanel.setPreferredSize(new Dimension(500,270));
264 }
265
266 static Class getClass(String s){
267 Class cls = null;
268 try{
269 cls = Class.forName(s);
270 }catch(ClassNotFoundException cnfe){
271 throw new NoClassDefFoundError(cnfe.getMessage());
272 }
273 return cls;
274 }
275
276 }
277