shuaiwangVM / achartengine

Automatically exported from code.google.com/p/achartengine
0 stars 0 forks source link

Calling series.clear() when annotations exist causes an array out of bounds index #386

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a line graph and add an XYSeries with annotations. 
2. Call the clear() method on the previously created XYSeries
3. Add new points to the series

What is the expected output? 
   -  The graph should be drawn correctly
What do you see instead?
   -  An ArrayIndexOutOfBounds exception on attempting to draw the graph

Please provide a source code snippet that we can use to replicate the
issue.

XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

XYSeries test = new XYSeries("TEST");
test.add(1,2);
test.add(3,4);
test.add(4,5);
test.addAnnotation("ann1",1,2);
test.addAnnotation("ann2",3,4);
test.addAnnotation("ann3",4,5);
drawGraph(test);
test.clear();
test.add(1,2);
test.add(3,4);
test.add(4,5);
test.addAnnotation("ann1",1,2);
test.addAnnotation("ann2",3,4);
test.addAnnotation("ann3",4,5);
drawGraph(test);

What version of the product binary library are you using?
 - 1.2.0 snapshot

Please provide any additional information below.
  - I think the problem is that the XYSeries clear method wipes the annotation xy map, but doesn't clear the array of annotations. Doing this should fix the problem.

Original issue reported on code.google.com by mccuthbe...@gmail.com on 20 Jan 2014 at 11:14

GoogleCodeExporter commented 9 years ago
Yes, I think this is the reason. Annotations are stored in both mStringXY and 
mAnnotations variables. Whenever clearAnnotations() is called only mStringsXY 
are cleared, while call to getAnnotationCount() returns size of mAnnotations. 
Hence adding annotations and then clearing them breaks everything. 
The work around is to call removeAnnotation(idx) because it removes annotations 
from both lists.

Original comment by etsi...@gmail.com on 30 Jul 2014 at 9:15

GoogleCodeExporter commented 9 years ago
Here is how I do it:

while (points.getAnnotationCount() > 0)
{
    points.removeAnnotation(points.getAnnotationCount()-1);
}

Original comment by etsi...@gmail.com on 30 Jul 2014 at 9:36

GoogleCodeExporter commented 9 years ago
The fix is easier; you need to add
 mAnnotations.clear()
in clearAnnotions()

 public synchronized void clearAnnotations() {
    mAnnotations.clear();
    mStringXY.clear();
  }

Original comment by verna...@gmail.com on 7 Aug 2014 at 11:02

GoogleCodeExporter commented 9 years ago

Original comment by dandrome...@gmail.com on 16 Sep 2014 at 7:22