2012-04-03 31 views
2

我正在使用exemple這個工具將rondom動態數據放入TimeSeriesCollection圖表中。從XML保存/加載jFreechart TimeSeriesCollection圖表

the data when first running the application

after a while i can not have the old data like those of the first picture

我的問題是,我無法找到如何:

1-讓當他們經過(在最後一小時)的舊數據的軌道視圖區域的左邊界(因爲數據點從右向左移動),只需實現一個水平滾動條即可。

2- XML是一個很好的選擇,可以將我的數據保存到我想要的時候全部數據的歷史記錄?

public class DynamicDataDemo extends ApplicationFrame { 
    /** The time series data. */ 
    private TimeSeries series; 

    /** The most recent value added. */ 
    private double lastValue = 100.0; 


public DynamicDataDemo(final String title) { 

    super(title); 
    this.series = new TimeSeries("Random Data", Millisecond.class); 
    final TimeSeriesCollection dataset = new TimeSeriesCollection(this.series); 
    final JFreeChart chart = createChart(dataset); 

    final ChartPanel chartPanel = new ChartPanel(chart); 

    final JPanel content = new JPanel(new BorderLayout()); 
    content.add(chartPanel); 
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); 
    setContentPane(content); 

} 


private JFreeChart createChart(final XYDataset dataset) { 
    final JFreeChart result = ChartFactory.createTimeSeriesChart(
     "Dynamic Data Demo", 
     "Time", 
     "Value", 
     dataset, 
     true, 
     true, 
     false 
    ); 
    final XYPlot plot = result.getXYPlot(); 
    ValueAxis axis = plot.getDomainAxis(); 
    axis.setAutoRange(true); 
    axis.setFixedAutoRange(60000.0); // 60 seconds 
    axis = plot.getRangeAxis(); 
    axis.setRange(0.0, 200.0); 
    return result; 
} 


public void go() { 

     final double factor = 0.90 + 0.2 * Math.random(); 
     this.lastValue = this.lastValue * factor; 
     final Millisecond now = new Millisecond(); 
     System.out.println("Now = " + now.toString()); 
     this.series.add(new Millisecond(), this.lastValue); 

} 


public static void main(final String[] args) throws InterruptedException { 

    final DynamicDataDemo demo = new DynamicDataDemo("Dynamic Data Demo"); 
    demo.pack(); 
    RefineryUtilities.centerFrameOnScreen(demo); 
    demo.setVisible(true); 

    while(true){ 

     demo.go(); 
     Thread.currentThread().sleep(1000); 
    } 

} 



} 

回答

2
  1. example使用在TimeSeries指定的最大項目年齡default values和計數。你會想要改變它們以適應你的要求。

  2. XML很好,但是對於高速率來說它很大;計劃相應。

另見本example使用javax.swing.Timer避免阻塞事件調度線程。

+0

非常感謝,但對於第一部分hemm,這不完全是我想要的,通過檢查TimeSeries.setMaximumItemAge(long),我發現它壓縮圖形以保留舊數據在視圖區域,而不是添加一個水平滾動條。因爲我想保留原來的圖形,並且在同一時間追蹤舊數據。 – 2012-04-04 12:00:24

+0

您需要進一步分離模型和視圖。有一個分頁方法建議[這裏](http://stackoverflow.com/a/6849654/230513)。 – trashgod 2012-04-04 15:28:57