[英]MPAndroidChart Realtime LineChart jumps on addEntry
I use MPAndroidChart to display values coming in periodically (every 3 seconds). 我使用MPAndroidChart定期(每3秒)显示一次值。 The values are added to the dataset on the main thread, then
setVisibleXRangeMaximum(10)
is called, and afterwards, the view is moved to the end, using moveViewToX(dataSet.getEntryCount())
. 将值添加到主线程上的数据集,然后
setVisibleXRangeMaximum(10)
,然后,使用moveViewToX(dataSet.getEntryCount())
将视图移到末尾。
This, however, results in the chart moving ever so slightly to the left (about a 10th of the width between two datapoints), instead of showing the newest data. 但是,这导致图表向左移动得太小(大约是两个数据点之间宽度的十分之一),而不是显示最新的数据。 If I scroll between adding two points, the view jumps to the beginning of the dataset.
如果在添加两个点之间滚动,则视图将跳到数据集的开头。
How can I achieve the behavior found in the example app, with the view showing 10 entries at once and scrolling to the newest entry on adding? 我如何实现示例应用程序中的行为,该视图一次显示10个条目,并在添加时滚动到最新条目?
This is how I initialize the chart: 这是我初始化图表的方式:
// Initializing
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.device_detail_element_graph, parent, false);
LineChart chart = (LineChart) v.findViewById(R.id.lineChart);
LineData lineData = new LineData();
lineData.setDrawValues(false);
chart.setDrawBorders(false);
chart.setDrawMarkers(false);
chart.getLegend().setEnabled(false);
chart.getAxisRight().setEnabled(false);
chart.setDrawGridBackground(false);
chart.setDescription(null);
chart.setData(lineData);
chart.setTouchEnabled(true);
chart.setData(lineData);
chart.notifyDataSetChanged();
And how I add new datapoints (called periodically): 以及我如何添加新数据点(定期调用):
LineData data = chart.getData();
ILineDataSet dataSet = data.getDataSetByIndex(0);
if(dataSet == null){
dataSet = new LineDataSet(null, "Dynamic data");
dataSet.setValueTextColor(Color.TRANSPARENT);
dataSet.setHighlightEnabled(false);
data.addDataSet(dataSet);
}
int lastIndex = dataSet.getEntryCount();
dataSet.addEntry(new Entry(lastIndex, (float) newValue.getValue()));
data.notifyDataChanged();
chart.notifyDataSetChanged();
chart.setVisibleXRangeMaximum(20.0f);
chart.moveViewToX(dataSet.getEntryCount());
Got it. 得到它了。 When I called the function that added new entrys to the graph, I always called
notifyDataChanged
on the RecyclerView
the graph was ultimately in. This somehow reset the view window of the graph, resulting in the jumps. 当我调用向图添加新
notifyDataChanged
的函数时,我总是在RecyclerView
上最终调用了该图,然后才调用该图。这以某种方式重置了图的视图窗口,从而导致跳转。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.