#685 DefaultXYDataset addSeries not replacing existing series

closed-fixed
David Gilbert
General (896)
5
2006-11-02
2006-11-02
Anonymous
No

org.jfree.data.xy.DefaultXYDataset.addSeries
(java.lang.Comparable seriesKey,
double[][] data)

This is the javadoc for addSeries:

"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."

The actual code:
int seriesIndex = indexOf(seriesKey);
if (seriesIndex == -1) { // add a new series
this.seriesKeys.add(seriesKey);
this.seriesList.add(data);
}
else { // replace an existing series
this.seriesList.add(seriesIndex, data);
}

Javadoc for ArrayList.add:

"Inserts the specified element at the specified
position in this list. Shifts the element currently
at that position (if any) and any subsequent elements
to the right (adds one to their indices)."

As can be seen, the charting code is simply moving
series down the array list and not actually replacing
them if they already exit. This causes an
IndexOutOfBoundsException as series are changed and
not replaced because getSeriesCount is returning the
size of the seriesList and not the seriesKeys list on
methods like getSeriesKey.

Code that causes the problem:

DefaultXYDataset ds = ...
for(int i = 0; i < ds.getSeriesCount(); i++) {
MyComparableKey k = (MyComparableKey)
ds.getSeriesKey(i); // this throws an exception
if(key.contains("match of myModel")) {
double[][] s = myModel.sample(1000);
ds.addSeries(myModel, s);
}
}

At another place in the code is a listener that is
removing and adding datasets at the user's request.
So what basically is happening is that
ds.getSeriesCounts() returns something like 3 when
there are really only 2 series in the seriesKeys but
3 in the seriesList.

To fix the problem, I changed

else { // replace an existing series
this.seriesList.add(seriesIndex, data);
}

to

else { // replace an existing series
this.seriesList.remove(seriesIndex);
this.seriesList.add(seriesIndex, data);
}

Thanks,

ddennison@up.com - Daniel C. Dennison

Discussion

  • David Gilbert
    David Gilbert
    2006-11-02

    • assigned_to: nobody --> mungady
    • status: open --> closed-fixed
     
  • David Gilbert
    David Gilbert
    2006-11-02

    Logged In: YES
    user_id=112975

    Good spot. I added some JUnit tests for this (it affects
    the new classes DefaultIntervalXYDataset and
    DefaultXYZDataset too) and fixed the code in CVS. The fixes
    will be included in the 1.0.3 release.