DefaultXYDataset update

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
ssaraceni
Posts: 14
Joined: Wed Apr 02, 2008 4:37 pm

DefaultXYDataset update

Post by ssaraceni » Wed Apr 09, 2008 10:08 am

I'm new to JFree, i'm using DefaultXYDataset for create a graph, with about 500+ points.
Each point is added every 100ms, but the problem is how to refresh the graph after adding a new point to one of the series of the dataset.

Code: Select all

serie=new double[2][1];
serie[0][0]=0;
serie[1][0]=0;
...
DefaultXYDataset dataset1=new DefaultXYDataset();
dataset1.addSeries("red", serie);
...
XYPlot chart= new XYPlot(dataset1,new NumberAxis("SEC"),new NumberAxis("BAR"),render);
when i add a new point i use this code:

Code: Select all

serie=new double[2][2];
serie[0][0]=0;
serie[1][0]=0;
serie[0][1]=1;
serie[1][1]=0.8;
Is it right?

Thanks
Simone

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Wed Apr 09, 2008 3:40 pm

No. You are creating a brand new array, and modifying 'serie' so that it points to this new structure. Your 'dataset1' retains its reference to the old array.

There are two ways you can update the data:

(1) First, remove the existing series, then replace it with a new series;

(2) Directly modify the 'serie' array (you can't resize it, so this only works for changing an existing value) then trigger a repaint of the chart by calling 'chart.setNotify(true)'.

Note that if you use a different dataset implementation (e.g. XYSeries/XYSeriesCollection) you'll get more flexibility for updating, at the cost of greater memory usage for the data structures.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked