#685 DefaultXYDataset addSeries not replacing existing series

General (896)

(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

The actual code:
int seriesIndex = indexOf(seriesKey);
if (seriesIndex == -1) { // add a new series
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);


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


ddennison@up.com - Daniel C. Dennison


  • 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

    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.


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks