XYBarDataset is a wrapper class, it contains your real dataset (an XYSeriesCollection) and extends the set of methods that it implements to cover all the additional methods in the IntervalXYDataset interface (as compared to the standard methods in the XYDataset interface).
JFreeChart doesn't care about the structure of your underlying dataset implementation, only that it implements the methods in the XYDataset interface (which provides the minimal set of methods required to *read* data for the purposes of plotting it). Certain renderers require the additional methods of IntervalXYDataset, but these are the exception rather than the general case.
You, however, do care about the structure of your dataset implementation, because you want to update the data. XYBarDataset lets you access the underlying dataset that you have wrapped, see the XYBarDataset.getUnderlyingDataset() method. This returns a dataset of type XYDataset (the interface) but you can safely cast this to an XYSeriesCollection because you know that's what you created the XYBarDataset with. So:
Code: Select all
XYSeriesCollection underlying = (XYSeriesCollection) data.getUnderlyingDataset();
From the XYSeriesCollection, you can retrieve any particular series either by its index or its key. In your code, you only have one series so it is easy:
Code: Select all
XYSeries s = underlying.getSeries("Y Values");
At this point, you can add or update data in the series using the API for the XYSeries class, for example:
The when you modify the series, it will notify the series collection which will, in turn, fire a DatasetChangeEvent. This event will automatically trigger a chart repaint.
One thing to bear in mind...the XYSeries class does not (by default) allow duplicate x-values. The addOrUpdate() method allows for this, because if there is already an item in the series for the given x value, it will just update the existing y-value rather than adding a new item to the series. If you use the regular add(x, y) methods, an exception will be thrown for duplicate x-values. It is possible to pass a flag to the series constructor to allow for duplicate x-values in the series. That's fine for plotting scatter plots, but if you have a line chart then duplicate x-values are not so normal.