JFreeChart實現實時曲線圖

jopen 10年前發布 | 36K 次閱讀 JFreeChart 圖表/報表制作

最 近要用到實時曲線圖,在網上大概找了一下,有兩種實現方式,一種就是JFreeChart的官方實例MemoryUsageDemo.java.通過一個 實現java.Swing.Timer的內部類,在其監聽器中將實時數據添加進TimeSeries,由于Timer是會實時執行的,所以這個方法倒是沒 有什么問題,可以參考代碼。
      另一種方式就是將實時類實現Runnable接口,在其run()方法中,通過無限循環將實時數據添加進TimeSeries,下面是較簡單的實現代碼:

java 代碼
  1. //RealTimeChart .java  
  2. import org.jfree.chart.ChartFactory;  
  3. import org.jfree.chart.ChartPanel;  
  4. import org.jfree.chart.JFreeChart;  
  5. import org.jfree.chart.axis.ValueAxis;  
  6. import org.jfree.chart.plot.XYPlot;  
  7. import org.jfree.data.time.Millisecond;  
  8. import org.jfree.data.time.TimeSeries;  
  9. import org.jfree.data.time.TimeSeriesCollection;  
  10.   
  11. public class RealTimeChart extends ChartPanel implements Runnable  
  12. {  
  13.     private static TimeSeries timeSeries;  
  14.     private long value=0;  
  15.       
  16.     public RealTimeChart(String chartContent,String title,String yaxisName)  
  17.     {  
  18.         super(createChart(chartContent,title,yaxisName));  
  19.     }  
  20.       
  21.     private static JFreeChart createChart(String chartContent,String title,String yaxisName){  
  22.         //創建時序圖對象  
  23.         timeSeries = new TimeSeries(chartContent,Millisecond.class);  
  24.         TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeSeries);  
  25.         JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(title,"時間(秒)",yaxisName,timeseriescollection,true,true,false);  
  26.         XYPlot xyplot = jfreechart.getXYPlot();  
  27.         //縱坐標設定  
  28.         ValueAxis valueaxis = xyplot.getDomainAxis();  
  29.         //自動設置數據軸數據范圍  
  30.         valueaxis.setAutoRange(true);  
  31.         //數據軸固定數據范圍 30s  
  32.         valueaxis.setFixedAutoRange(30000D);  
  33.   
  34.         valueaxis = xyplot.getRangeAxis();  
  35.         //valueaxis.setRange(0.0D,200D);  
  36.   
  37.         return jfreechart;  
  38.       }  
  39.   
  40.     public void run()  
  41.     {  
  42.         while(true)  
  43.         {  
  44.         try  
  45.         {  
  46.             timeSeries.add(new Millisecond(), randomNum());  
  47.             Thread.sleep(300);  
  48.         }  
  49.         catch (InterruptedException e)  {   }  
  50.         }         
  51.     }  
  52.       
  53.     private long randomNum()  
  54.     {     
  55.         System.out.println((Math.random()*20+80));        
  56.         return (long)(Math.random()*20+80);  
  57.     }  
  58. }  
  59.   
  60. //Test.java  
  61. import java.awt.BorderLayout;  
  62. import java.awt.event.WindowAdapter;  
  63. import java.awt.event.WindowEvent;  
  64. import javax.swing.JFrame;  
  65.   
  66. public class Test  
  67. {  
  68.   
  69.     /** 
  70.      * @param args 
  71.      */  
  72.     public static void main(String[] args)  
  73.     {  
  74.     JFrame frame=new JFrame("Test Chart");  
  75.     RealTimeChart rtcp=new RealTimeChart("Random Data","隨機數","數值");  
  76.     frame.getContentPane().add(rtcp,new BorderLayout().CENTER);  
  77.     frame.pack();  
  78.     frame.setVisible(true);  
  79.     (new Thread(rtcp)).start();  
  80.     frame.addWindowListener(new WindowAdapter()   
  81.     {  
  82.         public void windowClosing(WindowEvent windowevent)  
  83.         {  
  84.             System.exit(0);  
  85.         }  
  86.   
  87.     });  
  88.     }  


          這兩中方法都有一個問題,就是每實現一個圖就要重新寫一次,因為實時數據無法通過參數傳進來,在想有沒有可能通過setXXX()方式傳進實時數據,那樣的話就可以將實時曲線繪制類封裝起來,而只需傳遞些參數即可(或者誰有更好的辦法??)。

 

Copy:

http://favey.iteye.com/blog/112244

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!