Save/Load jFreechart TimeSeriesCollection chart from XML

Go To


I'm working with this exemple wich put rondom dynamic data into a TimeSeriesCollection chart.

the data when first running the application

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

My problem is that i can't find how to :

1- Make a track of the old data (of the last hour) when they pass the left boundary (because the data points move from the right to the left ) of the view area just by implementing a horizontal scroll bar.

2- Is XML a good choice to save my data into when i want to have all the history of the data?

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) {

    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());
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));


private JFreeChart createChart(final XYDataset dataset) {
    final JFreeChart result = ChartFactory.createTimeSeriesChart(
        "Dynamic Data Demo", 
    final XYPlot plot = result.getXYPlot();
    ValueAxis axis = plot.getDomainAxis();
    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");




2012-04-03 23:25
by imanis_tn


  1. The example uses the default values specified in TimeSeries for the maximum item age and count. You'll want to change them to suit your requirements.

  2. XML is fine, but it's voluminous for high rates; plan accordingly.

See also this example that uses javax.swing.Timer to avoid blocking the event dispatch thread.

2012-04-04 02:00
by trashgod
Thanks a lot, but for the first part hemm that's not exactly what i want to have, by checking the TimeSeries.setMaximumItemAge(long) i found that it compress the graph to keep the old data in view area, instead of adding a horizontal scroll bar. because i want to keep the graph as he was and have track the old data in the same time - imanis_tn 2012-04-04 12:00
You'll want to further separate the model and view. There's a paging approach suggested here - trashgod 2012-04-04 15:28