最近要用到實時曲線圖,在網(wǎng)上大概找了一下,有兩種實現(xiàn)方式,一種就是jfreechart的官方實例memoryusagedemo.java.通過一個實現(xiàn)java.swing.timer的內(nèi)部類,在其監(jiān)聽器中將實時數(shù)據(jù)添加進timeseries,由于timer是會實時執(zhí)行的,所以這個方法倒是沒有什么問題,可以參考代碼。
另一種方式就是將實時類實現(xiàn)runnable接口,在其run()方法中,通過無限循環(huán)將實時數(shù)據(jù)添加進timeseries,下面是較簡單的實現(xiàn)代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
//realtimechart .java import org.jfree.chart.chartfactory; import org.jfree.chart.chartpanel; import org.jfree.chart.jfreechart; import org.jfree.chart.axis.valueaxis; import org.jfree.chart.plot.xyplot; import org.jfree.data.time.millisecond; import org.jfree.data.time.timeseries; import org.jfree.data.time.timeseriescollection; public class realtimechart extends chartpanel implements runnable { private static timeseries timeseries; private long value= 0 ; public realtimechart(string chartcontent,string title,string yaxisname) { super (createchart(chartcontent,title,yaxisname)); } private static jfreechart createchart(string chartcontent,string title,string yaxisname){ //創(chuàng)建時序圖對象 timeseries = new timeseries(chartcontent,millisecond. class ); timeseriescollection timeseriescollection = new timeseriescollection(timeseries); jfreechart jfreechart = chartfactory.createtimeserieschart(title, "時間(秒)" ,yaxisname,timeseriescollection, true , true , false ); xyplot xyplot = jfreechart.getxyplot(); //縱坐標設(shè)定 valueaxis valueaxis = xyplot.getdomainaxis(); //自動設(shè)置數(shù)據(jù)軸數(shù)據(jù)范圍 valueaxis.setautorange( true ); //數(shù)據(jù)軸固定數(shù)據(jù)范圍 30s valueaxis.setfixedautorange(30000d); valueaxis = xyplot.getrangeaxis(); //valueaxis.setrange(0.0d,200d); return jfreechart; } public void run() { while ( true ) { try { timeseries.add( new millisecond(), randomnum()); thread.sleep( 300 ); } catch (interruptedexception e) { } } } private long randomnum() { system.out.println((math.random()* 20 + 80 )); return ( long )(math.random()* 20 + 80 ); } } //test.java import java.awt.borderlayout; import java.awt.event.windowadapter; import java.awt.event.windowevent; import javax.swing.jframe; public class test { /** * @param args */ public static void main(string[] args) { jframe frame= new jframe( "test chart" ); realtimechart rtcp= new realtimechart( "random data" , "隨機數(shù)" , "數(shù)值" ); frame.getcontentpane().add(rtcp, new borderlayout().center); frame.pack(); frame.setvisible( true ); ( new thread(rtcp)).start(); frame.addwindowlistener( new windowadapter() { public void windowclosing(windowevent windowevent) { system.exit( 0 ); } }); } } |
這兩中方法都有一個問題,就是每實現(xiàn)一個圖就要重新寫一次,因為實時數(shù)據(jù)無法通過參數(shù)傳進來,在想有沒有可能通過setxxx()方式傳進實時數(shù)據(jù),那樣的話就可以將實時曲線繪制類封裝起來,而只需傳遞些參數(shù)即可,或者誰有更好的辦法?
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/finethere/article/details/80722746