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
Dynamic XY plot
Re: Dynamic XY plot
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);
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);
Re: Dynamic XY plot
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.
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.
Re: Dynamic XY plot
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
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
Re: Dynamic XY plot
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.
Regards,
DG.
Re: Dynamic XY plot
David,
Thanks for the tip.
You are right, it updates without the overt call to fireDatasetChanged()
Great piece of work!
Ted Hill
Thanks for the tip.
You are right, it updates without the overt call to fireDatasetChanged()
Great piece of work!
Ted Hill