MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

Negative Array Size Exception from LineChartRenderer

Open frankyenshaw opened this issue 2 years ago • 1 comments

Summary

Receiving the following NegativeArraySizeExcpetion: E AndroidRuntime: java.lang.NegativeArraySizeException: -4 E AndroidRuntime: at com.github.mikephil.charting.utils.Transformer.generateTransformedValuesLine(Transformer.java:178) E AndroidRuntime: at com.github.mikephil.charting.renderer.LineChartRenderer.drawValues(LineChartRenderer.java:549) E AndroidRuntime: at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:278)

This occurs because of the following lines in LineChartRenderer:

            mXBounds.set(mChart, dataSet);

            float[] positions = trans.generateTransformedValuesLine(dataSet, mAnimator.getPhaseX(), mAnimator
                    .getPhaseY(), mXBounds.min, mXBounds.max);

mXBounds.min is > mXBounds.max resulting in a negative number.

Expected Behavior

I expect max > min and the chart to render or the library to handle this case gracefully and not crash

Possible Solution

one solution would be to handle the negative case instead of trying to access the dataSet array using the negative index value.

Device (please complete the following information):

  • Device: [e.g. Google Pixel] Google Pixel 4a
  • Android Version [e.g. 7.0] 12
  • Library Version (e.g. 3.1.0-alpha) implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'

Additional Context

Below is a screenshot of the dataset provided: Screen Shot 2022-06-26 at 6 18 16 PM

Below is the mXBounds object: mXBounds = {BarLineScatterCandleBubbleRenderer$XBounds@26969} max = 2 min = 12 range = -10 this$0 = {LineChartRenderer@26967} shadow$klass = {Class@25640} "class com.github.mikephil.charting.renderer.BarLineScatterCandleBubbleRenderer$XBounds" shadow$monitor = 0

FYI, The X values are 10 digit epoch timestamps

frankyenshaw avatar Jun 27 '22 01:06 frankyenshaw

You might want to take a look at this library XFree-MPAndroidChart which is based on MPAndroidChart and allows unsorted data

oneAcorn avatar Apr 19 '23 09:04 oneAcorn