Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android MP图表highlightValue不工作,引发ArrayIndexOutOfBoundsException_Java_Android_Kotlin_Mpandroidchart_Combinedchart - Fatal编程技术网

Java Android MP图表highlightValue不工作,引发ArrayIndexOutOfBoundsException

Java Android MP图表highlightValue不工作,引发ArrayIndexOutOfBoundsException,java,android,kotlin,mpandroidchart,combinedchart,Java,Android,Kotlin,Mpandroidchart,Combinedchart,我正在玩Android lib来绘制很棒的图表 我试图突出显示图表上的值,但它没有将突出显示放置到正确的位置,或者抛出ArrayIndexOutOfBoundsException 我为它做了一个小的虚拟项目。当用户单击“下一步”按钮时,突出显示应向正方向移动。 public class MainActivity extends AppCompatActivity implements View.OnClickListener { final int DATA_MAX_COUNT =

我正在玩Android lib来绘制很棒的图表

我试图突出显示图表上的值,但它没有将突出显示放置到正确的位置,或者抛出ArrayIndexOutOfBoundsException

我为它做了一个小的虚拟项目。当用户单击“下一步”按钮时,突出显示应向正方向移动。

  public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    final int DATA_MAX_COUNT = 30;

    List<MyData> list    = new ArrayList<>(); ///<Dummy data stored in here
    List<Entry>  entries = new ArrayList<>(); ///<Entries for MP Chart

    int highlightIndex = 0; ///<Chart's data index to be highlighted

    CombinedChart combinedChart; ///<I use combined chart because there will be more data sets added later on

    Button prevBtn; ///<Button for highlight control
    Button nextBtn; ///<Button for highlight control

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        combinedChart = (CombinedChart) findViewById(R.id.chart);

        prevBtn = (Button) findViewById(R.id.prev_btn);
        prevBtn.setOnClickListener(this);

        nextBtn = (Button) findViewById(R.id.next_btn);
        nextBtn.setOnClickListener(this);

        generateData();
        drawChart();
    }


    @Override
    public void onClick(View v) {

        //Clicking buttons should move the highlighted value
        if (v.equals(prevBtn)) {
            if (highlightIndex > 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;
        }
    }
}
以下是解决方案:

Highlight high = new Highlight(highlightIndex, 0, 0);
high.setDataIndex(0);
combinedChart.highlightValue(high, false);
您需要添加
high.setDataIndex(0)

解释

高亮显示中
构造函数的第一个参数是X值,即增加或减少的值。第二个是要选择的图的索引。因为只有一个,所以在那里指定0。您还应该第二次指定它
high.setDataIndex(0)(否则将被视为-1,bug!),您的代码将正常工作:

以下是解决方案:

Highlight high = new Highlight(highlightIndex, 0, 0);
high.setDataIndex(0);
combinedChart.highlightValue(high, false);
您需要添加
high.setDataIndex(0)

解释

高亮显示中
构造函数的第一个参数是X值,即增加或减少的值。第二个是要选择的图的索引。因为只有一个,所以在那里指定0。您还应该第二次指定它
high.setDataIndex(0)(否则将被视为-1,bug!),您的代码将正常工作: