当scrollToEnd为true时,GraphView在实时模式下显示错误的图形

时间:2015-10-19 13:29:03

标签: android graph android-graphview

我正在使用Graph View库(4.0.1)对加速计传感器进行采样并实时显示图表。

应用程序运行良好,但图表错误:

enter image description here

如图所示,Z轴(品红色)的值为~9.8但在图中它显示为~15,Y轴(绿色)的值为0.2但在图中我们在零下与X轴相同的想法。

这是我的代码:

SensorManager sensorManager;
TextView tvX, tvY, tvZ;
GraphView graph;
LineGraphSeries<DataPoint> seriesX, seriesY, seriesZ;
long startTime;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_runtime_graph);

    tvX = (TextView) findViewById(R.id.tvAcc_X);    // color : blue
    tvY = (TextView) findViewById(R.id.tvAcc_Y);    // color : green
    tvZ = (TextView) findViewById(R.id.tvAcc_Z);    // color : magenta
    graph = (GraphView) findViewById(R.id.graph);

    seriesX = new LineGraphSeries<>();
    seriesY = new LineGraphSeries<>();
    seriesZ = new LineGraphSeries<>();

    seriesX.setColor(Color.BLUE);
    seriesY.setColor(Color.GREEN);
    seriesZ.setColor(Color.MAGENTA);

    graph.addSeries(seriesX);
    graph.addSeries(seriesY);
    graph.addSeries(seriesZ);

    startTime = System.nanoTime() / 100000000;

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
public void onSensorChanged(SensorEvent event)
{
    tvX.setText(String.valueOf(event.values[0]));
    tvY.setText(String.valueOf(event.values[1]));
    tvZ.setText(String.valueOf(event.values[2]));

    updateGraph((event.timestamp / 100000000) - startTime,
            event.values[0], event.values[1], event.values[2]);
}

void updateGraph (final long timestamp, final float x, final float y, final float z)
{
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            seriesX.appendData(new DataPoint(timestamp, x), true, 40);
            seriesY.appendData(new DataPoint(timestamp, y), true, 40);
            seriesZ.appendData(new DataPoint(timestamp, z), true, 40);
        }
    });
}

我发现问题发生在scrollToEnd字段设置为true时,否则图表是正确的。 我发现的另一个问题可能相关,当scroolToEnd为真时,轴不会更新。

有没有办法解决这个问题? 谢谢!

1 个答案:

答案 0 :(得分:3)

logcat显示为什么scrollToEnd在你的情况下不起作用:

GraphView: scrollToEnd works only with manual x axis bounds

设置x轴边界手册,在create方法中添加此代码

Viewport vp = graph.getViewport();
vp.setXAxisBoundsManual(true);
vp.setMinX(0);
vp.setMaxX(1000);

但代码还有另一个问题: System.nanoTime() event.timestamp 是不同的时间格式

System.nanoTime() // current value of system timer in nanoseconds
event.timestamp // nanoseconds since device uptime

记录不完善:SensorEvent timestamp

另请参阅:Accelerometer SensorEvent timestamp