我正在尝试MPAndroidChart绘制实时数据,但当图表使用MoveViewToX(float xIndex)方法移动时,有时当前视图中显示的第一个值(和标签)会消失。这是代码:
//Adding entries
protected void AddEntry(int rssi, int i)
{
LineData data = linechart.LineData;
if (data != null)
{
LineDataSet dataset = (LineDataSet)data.GetDataSetByIndex(0);
if (dataset == null)
{
dataset = createSet();
data.AddDataSet(dataset);
}
//Add new value
data.AddXValue(i.ToString() + " ");
data.AddEntry(new Entry(rssi, dataset.EntryCount), 0);
linechart.NotifyDataSetChanged();
linechart.SetVisibleXRangeMaximum(10);
linechart.SetVisibleXRangeMinimum(10);
//scroll to the last entry
linechart.MoveViewToX(data.XValCount);
}
}
数据集:
protected LineDataSet createSet()
{
LineDataSet lineset = new LineDataSet(null, "Signal Power (dBm)");
lineset.SetDrawCubic(false);
//lineset.SetDrawFilled(true);
lineset.Color = Color.Aqua;
lineset.SetCircleColor(Color.DarkCyan);
lineset.CircleSize = 3f;
lineset.SetCircleColorHole(Color.DarkCyan);
lineset.AxisDependency = linechart.AxisRight.GetAxisDependency();
lineset.LineWidth = 2f;
lineset.SetDrawValues(false);
return lineset;
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:p1="http://schemas.android.com/apk/res/android"
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:id="@+id/relativeLayout1">
<com.github.mikephil.charting.charts.LineChart
p1:id="@+id/chart1"
p1:layout_width="match_parent"
p1:layout_height="250dp" />
<ListView
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="wrap_content"
p1:layout_marginTop="260dp"
p1:listSelector="@android:color/transparent"
p1:id="@+id/list" />
</RelativeLayout>
以下是发生的事情:enter image description here 提前谢谢。