最近要用到實(shí)時(shí)曲線圖,在網(wǎng)上大概找了一下,有兩種實(shí)現(xiàn)方式,一種就是JFreeChart的官方實(shí)例MemoryUsageDemo.java.通過(guò)一個(gè)實(shí)現(xiàn)java.Swing.Timer的內(nèi)部類(lèi),在其監(jiān)聽(tīng)器中將實(shí)時(shí)數(shù)據(jù)添加進(jìn)TimeSeries,由于Timer是會(huì)實(shí)時(shí)執(zhí)行的,所以這個(gè)方法倒是沒(méi)有什么問(wèn)題,可以參考代碼。
10年積累的成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶(hù)對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶(hù)得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先做網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有金塔免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
另一種方式就是將實(shí)時(shí)類(lèi)實(shí)現(xiàn)Runnable接口,在其run()方法中,通過(guò)無(wú)限循環(huán)將實(shí)時(shí)數(shù)據(jù)添加進(jìn)TimeSeries,下面是較簡(jiǎn)單的實(shí)現(xiàn)代碼:
//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)建時(shí)序圖對(duì)象 timeSeries = new TimeSeries(chartContent,Millisecond.class); TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeSeries); JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(title,"時(shí)間(秒)",yaxisName,timeseriescollection,true,true,false); XYPlot xyplot = jfreechart.getXYPlot(); //縱坐標(biāo)設(shè)定 ValueAxis valueaxis = xyplot.getDomainAxis(); //自動(dòng)設(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","隨機(jī)數(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); } }); } }
這兩中方法都有一個(gè)問(wèn)題,就是每實(shí)現(xiàn)一個(gè)圖就要重新寫(xiě)一次,因?yàn)閷?shí)時(shí)數(shù)據(jù)無(wú)法通過(guò)參數(shù)傳進(jìn)來(lái),在想有沒(méi)有可能通過(guò)setXXX()方式傳進(jìn)實(shí)時(shí)數(shù)據(jù),那樣的話(huà)就可以將實(shí)時(shí)曲線繪制類(lèi)封裝起來(lái),而只需傳遞些參數(shù)即可,或者誰(shuí)有更好的辦法?
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
文章題目:JFreeChart實(shí)現(xiàn)實(shí)時(shí)曲線圖
網(wǎng)站路徑:http://sd-ha.com/article18/ghdedp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、ChatGPT、定制開(kāi)發(fā)、小程序開(kāi)發(fā)、域名注冊(cè)、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)