Bug Fix - Can't add duplicate XYDataElements to XYSeries

A free public discussion forum for the JFreeChart class library.

Bug Fix - Can't add duplicate XYDataElements to XYSeries

Postby tedschw » Thu May 01, 2008 3:01 pm

I've found a bug in jfreechart-1.0.9 code for org.jfree.data.xy.XYSeries. There was a change some time ago which introduced the notion of allowing duplicate X values in XYSeries data. The method addOrUpdate(Number x, Number y) was never modified to support this, and therefore duplicate data were overwriting existing data. This is the fix I've made, but I don't know how to submit a patch...

$ diff original/jfreechart-1.0.9/source/org/jfree/data/xy/XYSeries.java fixed/org/jfree/data/xy/XYSeries.java
537c537
< if (index >= 0) {
---
> if (index >= 0 && !allowDuplicateXValues) {
545a546,559
> } else if (index >= 0){
> XYDataItem item = new XYDataItem(x, y);
> // need to make sure we are adding *after* any duplicates
> int size = this.data.size();
> while (index < size
> && item.compareTo(this.data.get(index)) == 0) {
> index++;
> }
> if (index < this.data.size()) {
> this.data.add(index, item);
> }
> else {
> this.data.add(item);
> }
558,561d571
< // check if this addition will exceed the maximum item count...
< if (getItemCount() > this.maximumItemCount) {
< this.data.remove(0);
< }
562a573,576
> // check if this addition will exceed the maximum item count...
> if (getItemCount() > this.maximumItemCount) {
> this.data.remove(0);
> }
tedschw
 
Posts: 3
Joined: Thu May 01, 2008 2:42 pm

Postby david.gilbert » Thu May 01, 2008 3:59 pm

Thanks for the report. Although it is slightly ambiguous what the addOrUpdate() method should do in this case, I think it makes sense to add a new value if duplicate x-values are allowed - otherwise, if there are multiple entries in the series with a matching x-value, which one should we update? Better to just add a new one, since that is permitted.

So, good spot - I'll fix that bug.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11363
Joined: Fri Mar 14, 2003 10:29 am

Postby teodord » Fri Nov 21, 2008 3:39 pm

Hi,

I think there is still something wrong with this.

If you have autosort true and allowduplicates true as well, it can happen
that the value already exists and it is the first in the list, in which
case the index returned would be zero. And we end up trying to add a new
value at index -1, which makes it blow up with:

java.lang.IndexOutOfBoundsException: Index: -1, Size: 13
at java.util.ArrayList.add(ArrayList.java:367)
at org.jfree.data.xy.XYSeries.addOrUpdate(XYSeries.java:571)

Thanks,
Teodor
teodord
 
Posts: 4
Joined: Tue Aug 19, 2008 3:10 pm

Postby david.gilbert » Mon Nov 24, 2008 1:59 pm

Thanks. You are right. I've fixed this in Subversion for the 1.0.12 release.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11363
Joined: Fri Mar 14, 2003 10:29 am


Return to JFreeChart - General

Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 9 guests