Dynamic XY plot

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Ted Hill

Dynamic XY plot

Post by Ted Hill » Wed Aug 21, 2002 2:17 pm

Hello,

I want to make a fairly simple XY plot having about 25 x,y pairs.

Then I want to be able to periodically change some of the y values and have the chart redraw itself.

I am new to JFreeChart, please suggest for me:

1. Which dataset class to use
2. How to update a y component of an existing x,y pair.
3. How to tell the chart to redraw after updating the dataset.

Thank you very much,

Ted Hill

josh

Re: Dynamic XY plot

Post by josh » Wed Aug 21, 2002 2:33 pm

private XYSeries series1=new XYSeries("");
private XYSeriesCollection dataset = new XYSeriesCollection();

//update your y Value by doing this
//note, x is not the x value, it is the xth point (1st, 2nd, 22nd point, etc)
series1.update(x,Number);//Number is a Double or something

//to get your graph to redraw
dataset=new XYSeriesCollection;
dataset.addSeries(series1);
chart.setDataset(dataset);

David Gilbert

Re: Dynamic XY plot

Post by David Gilbert » Wed Aug 21, 2002 2:40 pm

Josh has set you off in the right direction, except I'll add a couple of comments:

1) In recent versions of JFreeChart, the dataset is managed by the plot, so instead of:

chart.setDataset(dataset);

you should use:

XYPlot plot = chart.getXYPlot();
plot.setDataset(dataset);

2) Once you have set the dataset, any changes you make to it should automatically be reflected in the chart (it redraws itself any time the data changes).

Regards,

DG.

Ted Hill

Re: Dynamic XY plot

Post by Ted Hill » Wed Aug 21, 2002 4:11 pm

Thanks,

I followed your advice on choice of Dataset.
However instead of calling setDataset(modifiedDataset), I have subclassed XYSeriesCollection to expose fireDatasetChanged() as follows

class DynamicXYSeriesCollection extends XYSeriesCollection
{
public void fireDatasetChanged()
{
super.fireDatasetChanged();
}
}

So after I do
series1.update(x,Number);

I call myDataset.fireDatasetChanged(); and the chart redraws.

Do you see any problem with doing it this way?

Thank you,

Ted Hill

David Gilbert

Re: Dynamic XY plot

Post by David Gilbert » Wed Aug 21, 2002 4:17 pm

You shouldn't need to do this. When you call the update method in XYSeries, it will do a fireSeriesChanged() call. This should trigger a notification to XYSeriesCollection (via the seriesChanged(...) method in AbstractSeriesDataset) which should then call fireDatasetChanged() for you. If that is not happening, it is a bug...

Regards,

DG.

Ted Hill

Re: Dynamic XY plot

Post by Ted Hill » Wed Aug 21, 2002 4:45 pm

David,

Thanks for the tip.

You are right, it updates without the overt call to fireDatasetChanged()


Great piece of work!

Ted Hill

Locked