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.59k stars 9.02k forks source link

NegativeArraySizeException - More than 1K reported bugs every week #4457

Open eakteam opened 5 years ago

eakteam commented 5 years ago

I have more than 1K bug reports because of this issue. I have tried myself to modify and trying things but can't be solved. Does anyone know to fix this very old issue ???

Fatal Exception: java.lang.NegativeArraySizeException: -2
       at com.github.mikephil.charting.utils.Transformer.generateTransformedValuesLine(SourceFile:178)
       at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(SourceFile:278)
       at android.view.View.draw(View.java:19135)
       at android.view.View.updateDisplayListIfDirty(View.java:18085)
       at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4202)
       at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4182)
       at android.view.View.updateDisplayListIfDirty(View.java:18044)
       at android.view.View.draw(View.java:18863)
       at android.view.ViewGroup.drawChild(ViewGroup.java:4218)
       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4004)
       at android.view.View.draw(View.java:19138)
       at androidx.viewpager.widget.ViewPager.draw(SourceFile:2426)
       at android.view.View.updateDisplayListIfDirty(View.java:18085)
       at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4202)
       at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4182)
       at android.view.View.updateDisplayListIfDirty(View.java:18044)
       at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4202)
       at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4182)
       at android.view.View.updateDisplayListIfDirty(View.java:18044)
       at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4202)
       at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4182)
       at android.view.View.updateDisplayListIfDirty(View.java:18044)
       at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4202)
       at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4182)
       at android.view.View.updateDisplayListIfDirty(View.java:18044)
       at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4202)
       at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4182)
       at android.view.View.updateDisplayListIfDirty(View.java:18044)
       at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4202)
       at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4182)
       at android.view.View.updateDisplayListIfDirty(View.java:18044)
       at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4202)
       at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4182)
       at android.view.View.updateDisplayListIfDirty(View.java:18044)
       at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4202)
       at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4182)
       at android.view.View.updateDisplayListIfDirty(View.java:18044)
       at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4202)
       at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4182)
       at android.view.View.updateDisplayListIfDirty(View.java:18044)
       at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:643)
       at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:649)
       at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:757)
       at android.view.ViewRootImpl.draw(ViewRootImpl.java:3008)
       at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2822)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2375)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1414)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6827)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
       at android.view.Choreographer.doCallbacks(Choreographer.java:723)
       at android.view.Choreographer.doFrame(Choreographer.java:658)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
       at android.os.Handler.handleCallback(Handler.java:789)
       at android.os.Handler.dispatchMessage(Handler.java:98)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6673)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
PaulWoitaschek commented 5 years ago

I just tried the library for the first time and it crashes every time:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.github.mikephil.charting.charts.LineChart
      android:layout_width="match_parent"
      android:id="@+id/chart"
      android:layout_height="400dp" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
    val entries = listOf(
      Entry(22F, 40F),
      Entry(21F, 32F),
      Entry(20F, 34F)
    )
    val set = LineDataSet(entries, "label")
    val data = LineData(set)
    chart.data = data
    chart.invalidate()

com.github.PhilJay:MPAndroidChart:v3.1.0-alpha

PaulWoitaschek commented 5 years ago

The issue is that generateTransformedValuesLine calculates the count:

fnal int count = ((int) ((max - min) * phaseX) + 1) * 2;

However the count is negative (-2) because min=2 and max=0 calculated by BarLineScatterCandleBubbleRenderer#set.

eakteam commented 5 years ago

@PaulWoitaschek , do you find any solution for this ? It is really causing problems with Apps Stability. And Crash Rate have major impact on App visibility/ranking in the Play Store.

PaulWoitaschek commented 5 years ago

My solution is not not use this library 😉

As my first experience was a crash, I didn't want to include this in my app.

kamalh16 commented 5 years ago

I found that my data was not ordered. So make sure to sort your data.

dstd commented 5 years ago

@kamalh16 yeah, this library uses the perfect behavior to improve one's extrasensory skills

eakteam commented 5 years ago

@PaulWoitaschek , @kamalh16 , @dstd ... hmmm... I don't think that this is something that can't be solved and just say "don't use this lib". This lib. is in fact very nice but there are some issues (this is normal) but should be fixed. Maybe they are not fixing as it should because promotion of SciChart to stay on top, but regardless of this, for everyone this is the best solution for open source. If you have any testing that solves this issue would be great to share with us. One way is to manually edit the library and add catch methods in every place where the crashes are thrown, but better to solve it than to hide it if you understand what i mean with my low English(sorry for that). Best Regards, hope that everything will be improved and solved in following ....

PaulWoitaschek commented 5 years ago

I don't say that anyone shouldn't be using this library, in fact it's fascinating to see such a complex and customizable library.

I'm just saying that if my very fist experiment with this library immediately crashes I simply have lost the trust.

kamalh16 commented 5 years ago

@eakteam @PaulWoitaschek Completely agreed, specially with such a trivial error like this, the error logs should either state that you should order your data, or the library should implicitly order the data based on a default parameter, which you can change and customize.

eakteam commented 5 years ago

@kamalh16 , @PaulWoitaschek yes you'r right i understand, but i have didn't found any way to fix and simplify this issue within the library itself (not in our code). And crashes or issues have direct impact on how the app is ranked in Google Play Store, so this is making very bad effect in my case (and everyone else who is experiencing same thing). I just mean that this should be fixed ASAP. @PhilJay , what do you think , it is really important for some of us

Mr-Ramzan commented 4 years ago

I faced the Same Issue. The problem was solved

What I was doing was as below : I was setting Labels For X- Exis.. ==>[ My data was Unsorted ]<== lets say [3.0, 3.1, 2.0, 4.5, 0.0] so long.....

    //        xAxisLabels = ArrayList()
          //        xAxisLabels.add("0")
          //        xAxisLabels.add("1")
          //        xAxisLabels.add("2")
          //        xAxisLabels.add("3")
          //        xAxisLabels.add("4")
         //        xAxisLabels.add("5")

First what solved the error Was sorting My Data, but that did not give the required results... i.e. Gives you Straight line or an ascending line. [No data Downward only upward since sorted]

The second solution was not sorting data, but removing custom labels from X-Axis. Also commenting code that sets values for X-Labels. like below

//              chart!!.xAxis.setValueFormatter { value, _ ->
//
//                 return@setValueFormatter xAxisLabels[value.toInt()]
//              }

Now I get UP and Down Line as is my data with a default step size one[1] auto-incremented.

        for (i in 0 until yval.size-1) {
            values.add(
                Entry(i.toFloat(),
                       if(yval[i]<0){0f}else{yval[i]},
                       this@ProfileView.getDrawable(R.drawable.graph_drawable)
                )
            )
        }

I hope It helps Someone.