我在我的应用程序中使用 MPAndroidChart,我正在实时生成点并将它们添加到图表中。 如何使 View “跟随”图表并随着新值的添加而移动? 从某种意义上说,一旦图表到达屏幕的右端(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); } }
关于android - MPAndroidGraph 如何让图表自动滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28850749/