Search code examples
javajfreechart

Dynamic XYBarChart with DefaultIntervalXYDataset()


I'm trying to combine a XYBarChart as well as a normal XYPlot in a CombinedDomainXYPlot. The domain axis is a DateAxis(), allowing me to plot time series.

For the XYPlots, I can add datapoints dynamically using:

[XYSeries].add(time, value);

However, for the XYBarChart, I'm using JFreeChart's DefaultIntervalXYDataSet. For this class, the method for adding a data-series, is as follows:

addSeries(java.lang.Comparable seriesKey, double[][] data)

Adds a series or if a series with the same key already exists replaces the data for that series, then sends a DatasetChangeEvent to all registered listeners.

Clearly, the parameter double[][] data does not allow for dynamic changes to the data, in the sense that I can't simply add the new datapoints to the series itself. Is there a way to add datapoints to the XYBarChart dynamically? Or do I have to replace the complete double[][] in every update?

Ultimately, my goal is to add bars dynamically and give these bars a dynamic color.


Solution

  • Use an instance of org.jfree.data.xy.XYIntervalSeriesCollection. This dataset implements the IntervalXYDataset interface and allows the dynamic addition of further data items. The relationship between the DefaultIntervalXYDataSet and XYIntervalSeriesCollection is similar to that between DefaultXYDataset and XYSeriesCollection.