Update dataset

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
dafaro
Posts: 5
Joined: Mon Dec 12, 2016 10:58 pm
antibot: No, of course not.

Update dataset

Post by dafaro » Mon Dec 12, 2016 11:02 pm

Maybe I just missed something, but I have not been able to find (including looking in the developer guide) any method for updating the data in a series of a chart/plot.

I would like to essentially reload the y-data after a listener is triggered. Currently I am just plotting a whole new window, but this is obviously kinda clumsy.

Any pointers on documentation is appreciated.

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Update dataset

Post by John Matthews » Tue Dec 13, 2016 9:28 am

In this example, the setValue() method of DefaultCategoryDataset "updates a value in the table and sends a DatasetChangeEvent to all registered listeners." The listening view updates itself in response.

dafaro
Posts: 5
Joined: Mon Dec 12, 2016 10:58 pm
antibot: No, of course not.

Re: Update dataset

Post by dafaro » Tue Dec 13, 2016 5:09 pm

Thanks John,

So is DefaultCategoryDataset the only dataset type that will update in this way? ...trying to avoid rewriting all the XYdatasets I'm using

dafaro
Posts: 5
Joined: Mon Dec 12, 2016 10:58 pm
antibot: No, of course not.

Re: Update dataset

Post by dafaro » Tue Dec 13, 2016 5:58 pm

Also this dataset type does not seem to support double values for the x-axis

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Update dataset

Post by paradoxoff » Tue Dec 13, 2016 6:14 pm

All datasets that are part of the JFreeChart package will start an event chain that will finally lead to an updated "window" (if the window is a ChartWindow or if the window contains a ChartPanel).
You only need to keep a reference to the dataset an update it with new data. Note that some datasets do miss methods to update them. For example, DefaultOHLCDataset and DefaultHighLowDataset appear to be effectively "read-only", but the mroe "conventional" ones like DefaultCategoryDataset, DefaultXYDataset and XYSeriesCollection can be updated after construction.
You could also keep a reference to the XYPlot, and simply replace an outdated dataset with one containing new data. Though this approach creates a bit more overhead that simply updating the dataset, it is simpler to implement, and is certainly far more effective than recreating the entire window.

Locked