I tried to build a simple app that uses jjoe64's GraphView library to draw the real time value of accelerometers.
I've already done the accelerometer's value reading part, now I just met a little problem with putting these values onto graph in realtime.
Here's my code:
I set up a service that sending broadcast of the accelerometer's value, and in my graph drawing activity, I use the BroadcastReceiver
to retrieve the value like this:
...
private String sensorType = "";
private float[] sensorVal;
private GraphView mGraphView;
private GraphViewSeries xSeries;
private GraphViewData xData;
private int counter = 0;
...
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getFloatArrayExtra(sensorType) != null) {
sensorVal = intent.getFloatArrayExtra(sensorType);
++counter;
xSeries.appendData(new GraphViewData(counter, sensorVal[0]), true, 500);
// mGraphView.redrawAll(); // Same result if I use redrawAll();
}
}
};
I can get the accelerometer's values correctly, but somehow maybe I didn't use the GraphView
's append method properly, the result I was getting was the graph was only draw the latest value (in my case, the value of x-axis) and "discard" the previous one. Here's the screenshot of what I got:
I've also tried to find out how to do the realtime graph by visiting jjoe64's examples on Github but I couldn't find where I should change my code.
I did some search on SO:
Android graphview not automatically updating Android graphview all x-axis values are set to the latest value
But neither of them can really do some help for me.
Could someone enlighten me on this?
Cheers.