我正在使用mpandroidchart生成一个折线图,它添加来自心率和o2的实时数据。图表一开始会正确地填充数据,画两条线,但是当这些线到达屏幕中间时,图表会停止添加新数据,但会继续滚动,直到最后剩下一个空图表。
我不知道我哪里出错了,但这是我的代码。如果有人能告诉我我做错了什么,以及如何改正,我将不胜感激。
public class LineGraphFragment extends Fragment {
private LineChart heartRateGraph;
private YAxis yAxis;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.line_graph_fragment, container, false);
heartRateGraph = view.findViewById(R.id.heartRateGraph);
heartRateGraph.setDescription("");
heartRateGraph.setHighlightPerTapEnabled(true);
heartRateGraph.setDragEnabled(true);
heartRateGraph.setScaleEnabled(true);
heartRateGraph.setDrawGridBackground(false);
heartRateGraph.setPinchZoom(true);
heartRateGraph.setBackgroundColor(getResources().getColor(R.color.colorBlack, null));
LineData data = new LineData();
data.setValueTextColor(Color.WHITE);
heartRateGraph.setData(data);
Legend legend = heartRateGraph.getLegend();
legend.setForm(Legend.LegendForm.LINE);
legend.setTextColor(Color.WHITE);
XAxis xAxis = heartRateGraph.getXAxis();
xAxis.setTextColor(Color.WHITE);
xAxis.setDrawGridLines(false);
xAxis.setAvoidFirstLastClipping(true);
yAxis = heartRateGraph.getAxisLeft();
yAxis.setTextColor(Color.WHITE);
yAxis.setDrawGridLines(false);
yAxis.setAxisMinValue(MainActivity.lowLimitHeartRate - 20);
yAxis.setAxisMaxValue(200f);
yAxis.setDrawGridLines(true);
YAxis yAxis2 = heartRateGraph.getAxisRight();
yAxis2.setEnabled(false);
return view;
}
public void setOpacityLow(){
heartRateGraph.setAlpha(0.2f);
}
public void setOpacityNormal(){
heartRateGraph.setAlpha(1.0f);
}
public void addEntry(int rate){
LineData data = heartRateGraph.getData();
if(data != null){
LineDataSet set;
set = (LineDataSet) data.getDataSetByIndex(0);
if(set == null){
set = createSet();
data.addDataSet(set);
}
data.addXValue("");
data.addEntry(new Entry(rate, set.getEntryCount()), 0);
heartRateGraph.notifyDataSetChanged();
heartRateGraph.setVisibleXRange(30,30);
heartRateGraph.moveViewToX(data.getXValCount());
}
}
public void addO2Entry(int o2){
LineData o2data = heartRateGraph.getData();
if(o2data != null){
LineDataSet set2 = (LineDataSet) o2data.getDataSetByIndex(1);
if(set2 == null){
set2 = createSet2();
o2data.addDataSet(set2);
}
o2data.addXValue("");
o2data.addEntry(new Entry(o2, set2.getEntryCount()), 1);
heartRateGraph.notifyDataSetChanged();
}
}
private LineDataSet createSet(){
LineDataSet set = new LineDataSet(null, "Heart Rate");
set.setDrawCubic(true);
set.setCubicIntensity(0.2f);
set.setAxisDependency(YAxis.AxisDependency.LEFT);
set.setColor(ColorTemplate.getHoloBlue());
set.setLineWidth(2f);
set.setCircleRadius(2f);
set.setFillAlpha(65);
set.setFillColor(ColorTemplate.getHoloBlue());
set.setHighLightColor(Color.rgb(244,117,177));
set.setValueTextColor(Color.WHITE);
set.setValueTextSize(10f);
return set;
}
private LineDataSet createSet2(){
LineDataSet set2 = new LineDataSet(null, "O2");
set2.setDrawCubic(true);
set2.setCubicIntensity(0.2f);
set2.setAxisDependency(YAxis.AxisDependency.LEFT);
set2.setColor(Color.RED);
set2.setLineWidth(2f);
set2.setCircleRadius(2f);
set2.setFillAlpha(65);
set2.setFillColor(Color.RED);
set2.setHighLightColor(Color.rgb(244,117,177));
return set2;
}
1条答案
按热度按时间raogr8fs1#
我发现我正在愚蠢地加倍编写以下代码:
data.addxvalue(“”);
通过以下方式:
o2data.addxvalue(“”);
在我删除了o2data.addxvalue(“”)之后;图表又正常运行了。