KIC KIC - 4 months ago 58
Android Question

NegativeArraySizeException adding Scatter Data to a CombinedChart

I use MPAndriodChart and I want to combine multiple ScatterDataSet with one LineDataSet

CombinedChart chart = (CombinedChart) findViewById(R.id.chart);
CombinedData chartData = new CombinedData();
ScatterData scatterData = data.getScatterData();
chartData.setData(scatterData);
chartData.setData(data.getLineData());
chart.setData(chartData);
chart.invalidate();


The Line just works fine but on the ScatterData I get a NegativeArraySizeException:

java.lang.NegativeArraySizeException: -10
at com.github.mikephil.charting.utils.Transformer.generateTransformedValuesScatter(Transformer.java:110)
at com.github.mikephil.charting.renderer.ScatterChartRenderer.drawValues(ScatterChartRenderer.java:106)
at com.github.mikephil.charting.renderer.CombinedChartRenderer.drawValues(CombinedChartRenderer.java:95)


What I pass in seems to be perfectly valid data:

[Entry, x: 5.5699 y (sum): -0.5020655, Entry, x: 4.2394395 y (sum): 0.21312527, Entry, x: 3.5619884 y (sum): 0.45908606, Entry, x: 0.64683366 y (sum): 0.13041855, Entry, x: 5.2063227 y (sum): -0.3853986, Entry, x: 4.3252435 y (sum): -0.31767017, Entry, x: 5.9881873 y (sum): 0.2213649, Entry, x: 4.0705724 y (sum): 0.28665566, Entry, x: 3.1188233 y (sum): 0.33925194, Entry, x: 4.1558833 y (sum): 0.349967, Entry, x: 5.616512 y (sum): 0.76478994, Entry, x: 3.5945375 y (sum): 0.29368117, Entry, x: 4.4086146 y (sum): 0.085573964, Entry, x: 3.894157 y (sum): 0.6461969, Entry, x: 3.2885375 y (sum): 0.9365549, Entry, x: 3.2674248 y (sum): 0.86991787, Entry, x: 6.1956058 y (sum): -0.42813104, Entry, x: 5.3698792 y (sum): -0.22390603, Entry, x: 4.403929 y (sum): 0.08022818, Entry, x: 5.3132596 y (sum): 0.022318058, Entry, x: 5.8791475 y (sum): -0.0365236, Entry, x: 2.5035682 y (sum): -0.22752166]
[Entry, x: 0.7963708 y (sum): 0.12507936, Entry, x: 1.2852284 y (sum): 0.44808355]
[Entry, x: 6.883231 y (sum): -0.15286136]
[Entry, x: 5.358568 y (sum): -0.9050642]
[Entry, x: 6.221557 y (sum): -0.87225515]
[Entry, x: 3.392378 y (sum): -0.06295807, Entry, x: 3.9897776 y (sum): 0.21555887]
[Entry, x: 1.9450009 y (sum): 0.039005354, Entry, x: 3.6542387 y (sum): 0.13659218, Entry, x: 2.7017648 y (sum): -0.532821, Entry, x: 3.6622684 y (sum): 0.6588981, Entry, x: 4.9132524 y (sum): -0.04732264]

Answer

Most of the time the NegativeArraySizeException is caused by unsorted data in the new 3.0.0-beta1 version of the MPAndroidChart library. Sorting the data should resolve the issue.

This is because the library relies on sorted data for performance optimizations and thus doesn't work properly with unsorted data.

Comments