How to change a value in dataset.collection.series?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
pl477150
Posts: 12
Joined: Tue Oct 04, 2016 6:20 pm
antibot: No, of course not.

How to change a value in dataset.collection.series?

Post by pl477150 » Tue Oct 11, 2016 8:34 pm

I need a way to change a value in a series that is in a collection that is added to a dataset.

This is the dataset I have created:

Code: Select all

private XYBarDataset createMyDataset() {
		int barWidth = 5;
		double startValue = 10.0;
		
		XYSeriesCollection c1 = new XYSeriesCollection();
		XYSeries s1 = new XYSeries("Y Values");
		for(int i = 0; i < xValues.size(); i++) {
			s1.add(Double.parseDouble(xValues.get(i)), startValue); //X values are provided to me as Strings
		}
		c1.addSeries(s1);
		
		XYBarDataset data = new XYBarDataset(c1, barWidth);
		
		return data;
	}
This is for a bar chart that will change dynamically.
I have read on this forum that I can use series.addOrUpdate() to make the change.
My problem is getting to the series. Is there a way I can access the x,y values of the s1 series?
Should I just make references to the series before packing it away in the collection and dataset? That seemed like a bad idea to me.

I don't really see anything in the API to do this. That or I am just not understanding it.
I have tried getSeriesKey(0) but that seems to only provide a String?

Any help would be much appreciated.
PL

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: How to change a value in dataset.collection.series?

Post by david.gilbert » Wed Oct 12, 2016 3:04 am

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:

Code: Select all

s.addOrUpdate(5.0, 12.3);
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.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

pl477150
Posts: 12
Joined: Tue Oct 04, 2016 6:20 pm
antibot: No, of course not.

Re: How to change a value in dataset.collection.series?

Post by pl477150 » Wed Oct 12, 2016 1:34 pm

Thank you so much for the thorough response David. And thank you for this great piece of software you have shared with the world.

I was missing the casting of the Dataset and, as I suspected, misunderstanding how to use the String key. I tried it out and it is working like a well oiled machine.
Thank you again.
PL

Locked