PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.64k stars 9.02k forks source link

Negative Array Size Exception from LineChartRenderer #5321

Open frankyenshaw opened 2 years ago

frankyenshaw commented 2 years ago

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):

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

oneAcorn commented 1 year ago

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