我正在尝试使用GraphView库绘制实时图形。该示例显示了两种方法-resetData和appendData。这两种方法之间的区别如下
resetData 此方法将重置整个数据,因此当前数据将被新的替换。 appendData :此方法将单个数据集添加到当前数据。还有一个标志“ scrollToEnd”,它将自动将GraphView滚动到最后一个X值。
他们的LineGraphSeries的初始化也就是differentenet
private LineGraphSeries<DataPoint> mSeries1; // resetData
private LineGraphSeries<> mSeries2; // appendData
现在,我在MainActivity中创建一个这样的LineGraphSeries数组,并将传感器数据附加到每个LineGraphSeries。
// pitch, yaw and roll, external sensor.
LineGraphSeries<>[] gyroDataArray = new LineGraphSeries[8];
LineGraphSeries<DataPoint>[] gyroDataArray= new LineGraphSeries[8];
我要在viewModels中添加传感器值
gyroDataArray[0].appendData(new DataPoint(index,sensorRealtimeData.getPitch()), true,50);
gyroDataArray[1].appendData(new DataPoint(index,sensorRealtimeData.getRoll()), true,50);
gyroDataArray[2].appendData(new DataPoint(index,sensorRealtimeData.getYaw()), true,50);
index++;
sensorViewModel.setAllData(gyroDataArray);
此后,我在我获取这些值的“片段”中调用观察者-
gyroViewModel.getAllSensorValues().observe(getViewLifecycleOwner(), new Observer<LineGraphSeries<>[]>() {
@Override
public void onChanged(@Nullable LineGraphSeries<>[] lineGraphSeriesArray) {
updateSensorGraph(lineGraphSeriesArray);
}
});
private void updateSensorGraph(LineGraphSeries<>[] lineGraphSeriesArray){
sensorAdapter = new GraphAdapter(getActivity(),lineGraphSeriesArray);
sensorGraphs.setAdapter(sensorAdapter );
}
现在这在我的适配器内。我在这里看不到任何图形,但是我可以记录所有值...
public class GraphAdapter extends BaseAdapter {
private Context ctx;
private LineGraphSeries<>[] lineGraphSeriesArray;
public GraphAdapter(Context ctx, LineGraphSeries<>[] lineGraphSeriesArray) {
this.ctx = ctx;
this.lineGraphSeriesArray = lineGraphSeriesArray;
}
@Override
public int getCount() {
return lineGraphSeriesArray.length;
}
@Override
public LineGraphSeries<> getItem(int position) {
return lineGraphSeriesArray[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View graphview = convertView;
if (graphview == null) {
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
assert inflater != null;
graphview = inflater.inflate(R.layout.graphcard, null);
}
GraphView graph = (GraphView) graphview.findViewById(R.id.graphs);
graph.addSeries(getItem(position));
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setMinX(0);
graph.getViewport().setMaxX(40);
Log.d("GYRO graphs",getItem(position).toString());
return graphview;
}
}