MPAndroidChart 如何在删除位置 0 后将数据集移动到位置 -1
MPAndroidChart how to move data set to position -1 after remove position on 0
我的算法有问题。我删除了 0 上的位置,但我的数据集 ddint 像 -1 一样移动。然后我在位置 25 添加一个新条目。连续这使我的项目总是删除最后一个条目并且不停地在位置 25 添加条目。
代码如下:
private void addEntry() {
data = mChart.getData();
if (set1 == null) {
set1 = createSet();
data.addDataSet(set1);
}
int prog = 1;
for (int i = 0; i < prog; i++) {
float val = (float) (Math.random() * 2);
float high = (float) (Math.random() * 1) + 2f;
float low = (float) (Math.random() * 1) + 2f;
float open = (float) (Math.random() * 2) + 1f;
float close = (float) (Math.random() * 2) + 1f;
boolean even = i % 2 == 0;
data.addEntry(new CandleEntry(25, val + high, val - low, even ? val + open : val - open,
even ? val - close : val + close), 0);
data.notifyDataChanged();
mChart.notifyDataSetChanged();
mChart.setVisibleXRangeMaximum(25);
mChart.moveViewTo(data.getEntryCount() - 24, 2f, YAxis.AxisDependency.RIGHT);
}
}
private void removeLastEntry() {
CandleData data = mChart.getData();
if (data != null) {
ICandleDataSet set = data.getDataSetByIndex(0);
if (set != null) {
Entry e = set.getEntryForIndex(0);
data.removeEntry(e, 0);
data.notifyDataChanged();
mChart.notifyDataSetChanged();
mChart.invalidate();
}
}
}
DataSet#getEntryForIndex(int index)
returns Entry
的后备数组中第一个条目可能是也可能不是 Entry
最小值 x
图表上的值。来自 javadoc:
Returns the Entry object found at the given index (NOT xIndex) in the values array.
我怀疑你真的想使用类似下面的东西:
float xMin = dataSet.getXMin();
dataSet.remove(dataSet.getEntryForXPos(xMin);
查看 javadoc here。
但是,您的用例有点不寻常。当您删除具有最小值的 Entry
时,您似乎希望所有其他条目都为 "move down"。如果您只是不经常这样做,那么在您这样做时构建新数据集可能会更容易。请注意,虽然向图表添加 Entry
很容易,但随机访问 Entry
和删除它们的支持并不那么好。请参阅 the wiki
中的以下注释
Please be aware that this library does not officially support drawing LineChart data from an Entry list not sorted by the x-position of the entries in ascending manner. Adding entries in an unsorted way may result in correct drawing, but may also lead to unexpected behaviour.
我的算法有问题。我删除了 0 上的位置,但我的数据集 ddint 像 -1 一样移动。然后我在位置 25 添加一个新条目。连续这使我的项目总是删除最后一个条目并且不停地在位置 25 添加条目。
代码如下:
private void addEntry() {
data = mChart.getData();
if (set1 == null) {
set1 = createSet();
data.addDataSet(set1);
}
int prog = 1;
for (int i = 0; i < prog; i++) {
float val = (float) (Math.random() * 2);
float high = (float) (Math.random() * 1) + 2f;
float low = (float) (Math.random() * 1) + 2f;
float open = (float) (Math.random() * 2) + 1f;
float close = (float) (Math.random() * 2) + 1f;
boolean even = i % 2 == 0;
data.addEntry(new CandleEntry(25, val + high, val - low, even ? val + open : val - open,
even ? val - close : val + close), 0);
data.notifyDataChanged();
mChart.notifyDataSetChanged();
mChart.setVisibleXRangeMaximum(25);
mChart.moveViewTo(data.getEntryCount() - 24, 2f, YAxis.AxisDependency.RIGHT);
}
}
private void removeLastEntry() {
CandleData data = mChart.getData();
if (data != null) {
ICandleDataSet set = data.getDataSetByIndex(0);
if (set != null) {
Entry e = set.getEntryForIndex(0);
data.removeEntry(e, 0);
data.notifyDataChanged();
mChart.notifyDataSetChanged();
mChart.invalidate();
}
}
}
DataSet#getEntryForIndex(int index)
returns Entry
的后备数组中第一个条目可能是也可能不是 Entry
最小值 x
图表上的值。来自 javadoc:
Returns the Entry object found at the given index (NOT xIndex) in the values array.
我怀疑你真的想使用类似下面的东西:
float xMin = dataSet.getXMin();
dataSet.remove(dataSet.getEntryForXPos(xMin);
查看 javadoc here。
但是,您的用例有点不寻常。当您删除具有最小值的 Entry
时,您似乎希望所有其他条目都为 "move down"。如果您只是不经常这样做,那么在您这样做时构建新数据集可能会更容易。请注意,虽然向图表添加 Entry
很容易,但随机访问 Entry
和删除它们的支持并不那么好。请参阅 the wiki
Please be aware that this library does not officially support drawing LineChart data from an Entry list not sorted by the x-position of the entries in ascending manner. Adding entries in an unsorted way may result in correct drawing, but may also lead to unexpected behaviour.