Reputation: 53
Hi this is my code but not work (newest version of library)
LineData dataChart = mainChart.getData();
if (dataChart != null) {
LineDataSet set = (LineDataSet) dataChart.getDataSetByIndex(0);
if (set == null) {
set = (LineDataSet) createSet(ColorTemplate.rgb(colorChart), ColorTemplate.rgb(colorFill));
dataChart.addDataSet(set);
}
dataChart.addEntry(new Entry(set.getEntryCount(), sensorEvent.values[2]), 0);
dataChart.notifyDataChanged();
if(set.getEntryCount() == 20) {
set.removeFirst();
}
// let the chart know it's data has changed
mainChart.notifyDataSetChanged();
// limit the number of visible entries
mainChart.setVisibleXRangeMaximum(20);
mainChart.moveViewToX(dataChart.getEntryCount());
}
`output of this cod: not correct
but when I chang my code:
LineData dataChart = mainChart.getData();
if (dataChart != null) {
LineDataSet set = (LineDataSet) dataChart.getDataSetByIndex(0);
if (set == null) {
set = (LineDataSet) createSet(ColorTemplate.rgb(colorChart), ColorTemplate.rgb(colorFill));
dataChart.addDataSet(set);
}
dataChart.addEntry(new Entry(set.getEntryCount(), sensorEvent.values[2]), 0);
dataChart.notifyDataChanged();
// let the chart know it's data has changed
mainChart.notifyDataSetChanged();
// limit the number of visible entries
mainChart.setVisibleXRangeMaximum(300);
mainChart.moveViewToX(dataChart.getEntryCount());
}
and the output is correct,so what should I do to remove old data from the chart? correct but I want delet old data
Upvotes: 0
Views: 6051
Reputation: 1742
I found some issues in previous answers. So, I tried different approach.
Advantages: 1. Entry count of the dataset remains constant. 2. A real-time graph like ECG
private long removalCounter = 0;
private static final int VISIBLE_COUNT = 300;
private void addNewEntry(double value) {
LineData data = lineChart.getData();
if (data != null) {
ILineDataSet set = data.getDataSetByIndex(0);
if (set == null) {
set = createDataSet();
data.addDataSet(set);
}
data.addEntry(new Entry(set.getEntryCount() + removalCounter, (float) value), 0);
data.notifyDataChanged();
// limit the number of visible entries
lineChart.setVisibleXRangeMaximum(VISIBLE_COUNT);
// move to the latest entry
lineChart.moveViewToX(set.getEntryCount());
//remove entry which is out of visible range
if (set.getEntryCount() > VISIBLE_COUNT){
data.removeEntry(removalCounter, 0);
removalCounter++;
}
lineChart.notifyDataSetChanged();
}
}
private LineDataSet createDataSet() {
LineDataSet set = new LineDataSet(null, "real_time");
set.setAxisDependency(YAxis.AxisDependency.LEFT);
set.setColor(Color.RED);
set.setLineWidth(1f);
set.setDrawCircles(false);
set.setHighlightEnabled(false);
set.setDrawValues(false);
set.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
set.setCubicIntensity(0.2f);
return set;
}
Upvotes: 1
Reputation: 13343
Solution for v3.0.2 of MPAndroidChart from this answer of Adam Wu:
if (set.getEntryCount() >= MAX_ENTRIES) { set.removeFirst(); for (int i=0; i<set.getEntryCount(); i++) { Entry entryToChange = set.getEntryForIndex(i); entryToChange.setX(entryToChange.getX() - 1); } }
Upvotes: 0
Reputation: 53
The problem was solved. I should add this lines.
if(set.getEntryCount() == MAX_ENTRIES) {
set.removeFirst();
// change Indexes - move to beginning by 1
for (Entry entry : set.getValues() )
entry.setX(entry.getX() - 1);
}
Upvotes: 4
Reputation: 6622
I also had this problem.I want to show the last 5 entries in a LineChart,so when a data is comming,I call addXValue() and addEntry().Since the entry count is 5,I call removeXValue(0) and removeEntry(0) to remove the oldest entry,but the LineChart is very strange:in fact the entry count is 5,but it only one entry in the end,now I don't know how to do with it.Here is my code:
private static final int VISIBLE_NUM = 5;
private void refreshData(float value) {
LineData data = mChart.getData();
if (data != null) {
LineDataSet set = data.getDataSetByIndex(0);
if (set == null) {
set = new LineDataSet(null, "DataSet");
set.enableDashedLine(10f, 5f, 0f);
set.setColor(Color.BLUE);
set.setCircleColor(Color.GREEN);
set.setLineWidth(1f);
set.setCircleSize(3f);
set.setDrawCircleHole(false);
set.setValueTextSize(9f);
set.setFillAlpha(65);
set.setFillColor(Color.BLACK);
data.addDataSet(set);
}
if(set.getEntryCount() == VISIBLE_NUM) {
data.removeXValue(0);
set.removeEntry(0);
}
data.addXValue(new SimpleDateFormat("HH:mm:ss")
.format(new Date(System.currentTimeMillis())));
data.addEntry(new Entry(value, set.getEntryCount()), 0);
mChart.notifyDataSetChanged();
//mChart.setVisibleXRange(VISIBLE_NUM-1);
//mChart.moveViewToX(data.getXValCount() - VISIBLE_NUM);
mChart.invalidate();
}
}
Upvotes: 1