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.53k stars 9.01k forks source link

CombinedChart HighLightFullBar causes ArrayIndexOutOfBoundsException #2679

Open devrtech opened 7 years ago

devrtech commented 7 years ago

Hello,

Thank you for your great library. We encounter a crash when using setHighLightFullBarEnabled in a CombinedChart. To reproduce simply build the demo app, and in the CombinedChartActivity set to true the following option:

The same issue seems to have been fixed on the iOS port: https://github.com/danielgindi/Charts/issues/1185

Here is the stacktrace:

com.xxmassdeveloper.mpchartexample E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.xxmassdeveloper.mpchartexample, PID: 24238
                                                                                    java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1
                                                                                        at java.util.ArrayList.get(ArrayList.java:413)
                                                                                        at com.github.mikephil.charting.data.CombinedData.getEntryForHighlight(CombinedData.java:185)
                                                                                        at com.github.mikephil.charting.charts.Chart.highlightValue(Chart.java:634)
                                                                                        at com.github.mikephil.charting.listener.ChartTouchListener.performHighlight(ChartTouchListener.java:124)
                                                                                        at com.github.mikephil.charting.listener.BarLineChartTouchListener.onSingleTapUp(BarLineChartTouchListener.java:611)
                                                                                        at android.view.GestureDetector.onTouchEvent(GestureDetector.java:635)
                                                                                        at com.github.mikephil.charting.listener.BarLineChartTouchListener.onTouch(BarLineChartTouchListener.java:112)
                                                                                        at com.github.mikephil.charting.charts.BarLineChartBase.onTouchEvent(BarLineChartBase.java:556)
                                                                                        at android.view.View.dispatchTouchEvent(View.java:9942)
GoMino commented 7 years ago

I second that, same problem encountered here!

ghost commented 7 years ago

Hello! I faced the same problem using CombinedChart. This occurred when highlighting value programatically.

mChart.highlightValue();

The error happens in getEntryForHighlight() method of CombinedData.java

When switching to simply BarChart - the problem doesn't exists.

Hope it will be helpful.

devrtech commented 7 years ago

Thanks for your workaround but we have to use combined chart...

VickyDonor36 commented 7 years ago

I am also getting the same issue using combined chart as devrtech is getting by using

I need to use the combined chart not the barchart for my thing. I would appreciate if any solution could be provided.

Stack trace is here: Process: com.example.admin.groupfull, PID: 22233 java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1 at java.util.ArrayList.get(ArrayList.java:310) at com.github.mikephil.charting.data.CombinedData.getEntryForHighlight(CombinedData.java:185) at com.github.mikephil.charting.charts.Chart.highlightValue(Chart.java:634) at com.github.mikephil.charting.listener.ChartTouchListener.performHighlight(ChartTouchListener.java:124) at com.github.mikephil.charting.listener.BarLineChartTouchListener.onSingleTapUp(BarLineChartTouchListener.java:611) at android.view.GestureDetector.onTouchEvent(GestureDetector.java:635) at com.github.mikephil.charting.listener.BarLineChartTouchListener.onTouch(BarLineChartTouchListener.java:112) at com.github.mikephil.charting.charts.BarLineChartBase.onTouchEvent(BarLineChartBase.java:556) at android.view.View.dispatchTouchEvent(View.java:9303) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2549)

alex-fer commented 7 years ago

I've faced the same bug. I've tried also setting the highlight entry manually instead of automatically with combinedChart.highlightValue(entry.getX(), 1, false); but the error persist.

prefect4742 commented 7 years ago

I managed, in a roundabout way:

public void select(float x) {
    ScatterData scatterData = mChart.getScatterData();
    for (int dsIndex = 0 ; dsIndex < scatterData.getDataSetCount() ; dsIndex++) {
        IScatterDataSet ds = scatterData.getDataSetByIndex(dsIndex);
        for (int entryIndex = 0; entryIndex < ds.getEntryCount(); entryIndex++) {
            Entry entry = ds.getEntryForIndex(entryIndex);
            if (entry.x == x) {
                Highlight highlight = new Highlight(entry.getX(), entry.getY(), dsIndex);
                highlight.setDataIndex(mChart.getData().getDataIndex(scatterData));
                mChart.highlightValue(highlight, false);
                return;
            }
        }
    }
    mChart.highlightValue(0, -1, false);
}

It seems that if I already have the Entry I should be able to just say "highlight this", but alas... :)

Ornolfr commented 7 years ago

Have the same problem. @PhilJay please help

biokys commented 6 years ago

I manage it to work finally after debugging library ` Highlight highlight = new Highlight(valX, Float.NaN, 0);

highlight.setDataIndex(your_chart_data_index); // IMPORTANT set it to value regarding CombinedData#getAllData() order

highlightValue(highlight); `

laumancheuck commented 5 years ago

@Override public Highlight getHighlightByTouchPoint(float x, float y) {

    if (mData == null) {
        Log.e(LOG_TAG, "Can't select by touch. No data set.");
        return null;
    } else {
        Highlight h = getHighlighter().getHighlight(x, y);
        if (h == null || !isHighlightFullBarEnabled()) return h;

        // For isHighlightFullBarEnabled, remove stackIndex
        Highlight highlight = new Highlight(h.getX(), h.getY(), h.getXPx(), h.getYPx(), h
                .getDataSetIndex(), -1, h.getAxis());
        // modify this
        highlight.setDataIndex(h.getDataIndex());
        return highlight;
    }
}

Because the Highlight will new one when isHighlightFullBarEnabled is true , and the variable 'mDataIndex' becomes -1 . it makes ArrayIndexOutOfBoundsException. So keep the 'mDataIndex'. You can override it in class "CombinedChart" or extend "CombinedChart". It works for me.

hareeshatgithub commented 4 years ago

@PhilJay I am facing the same issue. I am using v3.0.3 (Can't use latest version as we have to be on same version as our other module which uses MP charts) and I see by default highLightFullBarEnabled() is set to true. But, it is not working and when I again explicitly try to set it to true, it crashes. Tried above workarounds, but no luck. Can you please help with this?

hareeshatgithub commented 4 years ago

@override public Highlight getHighlightByTouchPoint(float x, float y) {

    if (mData == null) {
        Log.e(LOG_TAG, "Can't select by touch. No data set.");
        return null;
    } else {
        Highlight h = getHighlighter().getHighlight(x, y);
        if (h == null || !isHighlightFullBarEnabled()) return h;

        // For isHighlightFullBarEnabled, remove stackIndex
        Highlight highlight = new Highlight(h.getX(), h.getY(), h.getXPx(), h.getYPx(), h
                .getDataSetIndex(), -1, h.getAxis());
        // modify this
        highlight.setDataIndex(h.getDataIndex());
        return highlight;
    }
}

Because the Highlight will new one when isHighlightFullBarEnabled is true , and the variable 'mDataIndex' becomes -1 . it makes ArrayIndexOutOfBoundsException. So keep the 'mDataIndex'. You can override it in class "CombinedChart" or extend "CombinedChart". It works for me.

@laumancheuck I tried the exact way, but not working for me. Did you find any other workaround? Please help me.

hareeshatgithub commented 4 years ago

I manage it to work finally after debugging library ` Highlight highlight = new Highlight(valX, Float.NaN, 0);

highlight.setDataIndex(your_chart_data_index); // IMPORTANT set it to value regarding CombinedData#getAllData() order

highlightValue(highlight); `

@biokys I tried the exact way, but not working for me. Did you find any other workaround? Please help me.