MPAndroid Graph 如何使图表自动滚动

MPAndroidGraph How to make the graph auto-scroll

我在我的应用程序中使用 MPAndroidChart,我实时生成点并将它们添加到图表中。 如何使视图 "follow" 成为图形并随着新值的添加而移动? 从某种意义上说,一旦图形到达屏幕的右端(x 值最大),图形应该向前移动并允许用户向后滑动以查看图形的较旧部分。

希望我的问题很清楚。

谢谢!

这是可能的,请查看 realtime-chart-demo,它解释了如何操作。

  • 为图表设置一个空的 LineData(或任何数据)对象:

    chart.setData(new LineData())

  • 每次添加条目时调用此方法:

    private void addEntry(Entry entry) {
    
        LineData data = mChart.getData();
    
        if (data != null) {
    
            // get the dataset where you want to add the entry
            LineDataSet set = data.getDataSetByIndex(0);
    
            if (set == null) {
                // create a new DataSet if there is none yet
                set = createSet();
                data.addDataSet(set);
            }
    
            // add a new x-value first
            data.addXValue("somestring");
            data.addEntry(entry, 0);
    
            // let the chart know it's data has changed
            mChart.notifyDataSetChanged();
    
            // limit the number of visible entries
            mChart.setVisibleXRange(6);
    
            // move to the latest entry
            mChart.moveViewToX(data.getXValCount()-7);
        }
    }