嗨,这是我的代码,但不起作用(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());
}
`这个鳕鱼的输出: not correct
但是当我改变我的代码时:
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());
}
并且输出正确,那么我该怎么做才能从图表中删除旧数据? correct but I want delet old data
答案 0 :(得分:1)
问题解决了。 我应该添加这一行。
if(set.getEntryCount() == MAX_ENTRIES) {
set.removeFirst();
// change Indexes - move to beginning by 1
for (Entry entry : set.getValues() )
entry.setX(entry.getX() - 1);
}
答案 1 :(得分:1)
我在先前的答案中发现了一些问题。因此,我尝试了不同的方法。
优点: 1.数据集的条目数保持不变。 2.像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;
}
答案 2 :(得分:0)
我也遇到过这个问题。我想在LineChart中显示最后5个条目,所以当数据出现时,我调用addXValue()和addEntry()。由于条目数为5,我调用removeXValue(0 )和removeEntry(0)删除最旧的条目,但LineChart很奇怪:实际上条目数是5,但它最后只有一个条目,现在我不知道怎么处理它。这是我的代码:
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();
}
}
答案 3 :(得分:0)