0
votes

I use MPAndroidChart to display values coming in periodically (every 3 seconds). 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()).

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?

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());
1

1 Answers

1
votes

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.