MPAndroidChart highlightValue无效,抛出ArrayIndexOutOfBoundsException

转自:https://stackoverflow.com/questions/51769982/android-mp-chart-highlightvalue-not-working-throws-arrayindexoutofboundsexcepti.

解决方案:

Highlight high = new Highlight(highlightIndex, 0, 0);
high.setDataIndex(0);
combinedChart.highlightValue(high, false);

 

 

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    final int DATA_MAX_COUNT = 30;

    List list    = new ArrayList<>(); ///  entries = new ArrayList<>(); /// 0) {
                highlightIndex--;
            }
        } else if (v.equals(nextBtn)) {
            if (highlightIndex + 1 < DATA_MAX_COUNT) {
                highlightIndex++;
            }
        }

        //Does not work, throws exception
        //combinedChart.highlightValue(new Highlight(highlightIndex, 0, 0));

        //Does not work, throws exception
        //combinedChart.highlightValue(highlightIndex, 0, false);

        //Exception

//        java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1
//        at com.github.mikephil.charting.data.CombinedData.getDataByIndex(CombinedData.java:152)
//        at com.github.mikephil.charting.data.CombinedData.getEntryForHighlight(CombinedData.java:183)
//        at com.github.mikephil.charting.charts.Chart.highlightValue(Chart.java:635)
//        at com.github.mikephil.charting.charts.Chart.highlightValue(Chart.java:613)


        //Works, but highlights value on chart with like x=0 and y= 190, wtf?
        combinedChart.highlightValue(combinedChart.getHighlighter().getHighlight(highlightIndex, 0));


    }


    //Generating random data to a list
    public void generateData() {
        for (int i = 0; i < DATA_MAX_COUNT; i++) {
            MyData myData = new MyData(new Random().nextInt(100) + 100);
            list.add(myData);
        }
    }

    //Simple func for adding data to entries and drawing chart
    private void drawChart() {

        CombinedData combinedData = new CombinedData();

        for (int i = 0; i < list.size(); i++) {
            MyData myData = list.get(i);
            entries.add(new Entry(i, myData.getValue(), myData));
        }

        LineDataSet lineDataSet = new LineDataSet(entries, "My data list");

        lineDataSet.setHighLightColor(Color.RED);
        lineDataSet.setHighlightLineWidth(3);

        LineData lineData = new LineData();
        lineData.addDataSet(lineDataSet);

        combinedData.setData(lineData);
        combinedChart.setData(combinedData);

        combinedChart.invalidate();
    }


    //Dummy data class
    public static class MyData {

        private int value;

        public MyData(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }
    }
}

报错:java.lang.ArrayIndexOutOfBoundsException:length = 10; 指数= -1

答案:见上

你可能感兴趣的:(MPAndroidChart highlightValue无效,抛出ArrayIndexOutOfBoundsException)