XYPlot.datasets.size field doesn't update ?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
pho
Posts: 5
Joined: Thu Nov 16, 2006 6:47 pm

XYPlot.datasets.size field doesn't update ?

Post by pho » Thu Nov 16, 2006 6:59 pm

Hi

After creating a chart, I dynamically add new time series to it, and I
want to use the data by looking at the dataset. It's ok when I add the
first series. But when I add the second series it fails.

Section of the offending code: -

for (int row=0; row < total_rows; row++) {
XYPlot plot = (XYPlot)chart.getChart().getPlot();
XYDataset dataset = plot.getDataset(row);
...
...
}

Run it in the debugger, I find the datasets.size remains 1, even though
datasets.objects[0].data.elementData contains 2 time series.

This results in plot.getDataset(1) to return null
because it uses if (this.datasets.size() > index) to
check whether the index is valid.

Have some experienced this problem before?

Thanks.

Phil

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

Post by david.gilbert » Fri Nov 17, 2006 10:49 am

I'd guess that you have one dataset with two series in it. If not, could you post a JUnit test that shows the failure, and tell me the version of JFreeChart you are using.
David Gilbert
JFreeChart Project Leader

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

pho
Posts: 5
Joined: Thu Nov 16, 2006 6:47 pm

Post by pho » Fri Nov 17, 2006 10:34 pm

Thanks David. You're right.

I created a TimeSeriesCollection object and added 2 time series to
it, and then added the TimeSeriesCollection object
to createTimeSeriesChart(). So there is really 1 dataset.

If I have N time series, which way is more efficient for jfreechart: -

(1) Create a TimeSeriesCollection, then add the N time series to it.
And then pass the TSC to: -
ChartFactory.createTimeSeriesChart
(CHART_TITLE, "Time", "Value", TSC, true, false, false)

(2) Create N dataset each has 1 time series then add these N
datasets to a Plot inside a loop like: -
chart.getXYPlot().setDataset(datasets)

Thanks for your help.

Regards,
Phil

Locked