溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

JFreeChart實(shí)現(xiàn)實(shí)時(shí)曲線圖

發(fā)布時(shí)間:2020-10-10 05:20:27 來(lái)源:腳本之家 閱讀:153 作者:finethere 欄目:編程語(yǔ)言

最近要用到實(shí)時(shí)曲線圖,在網(wǎng)上大概找了一下,有兩種實(shí)現(xiàn)方式,一種就是JFreeChart的官方實(shí)例MemoryUsageDemo.java.通過(guò)一個(gè)實(shí)現(xiàn)java.Swing.Timer的內(nèi)部類,在其監(jiān)聽(tīng)器中將實(shí)時(shí)數(shù)據(jù)添加進(jìn)TimeSeries,由于Timer是會(huì)實(shí)時(shí)執(zhí)行的,所以這個(gè)方法倒是沒(méi)有什么問(wèn)題,可以參考代碼。

另一種方式就是將實(shí)時(shí)類實(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ù),那樣的話就可以將實(shí)時(shí)曲線繪制類封裝起來(lái),而只需傳遞些參數(shù)即可,或者誰(shuí)有更好的辦法?

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI