从XML保存/加载jFreechart TimeSeriesCollection图表

时间:2012-04-03 23:25:44

标签: java xml jfreechart dynamic-data

我正在使用这个exemple将rondom动态数据放入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);
    }

}



}

1 个答案:

答案 0 :(得分:2)

  1. example使用default values中指定的TimeSeries作为最大项目年龄和计数。您需要根据自己的要求进行更改。

  2. XML很好,但是对于高费率来说却很庞大;相应的计划。

  3. 另请参阅使用javax.swing.Timer的{​​{3}}以避免阻止事件派发线程。