0

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:

enter image description here

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.

Community
  • 1
  • 1
dumbfingers
  • 7,001
  • 5
  • 54
  • 80
  • 1
    please double check the data. you could also try to set a small sized viewport, for better visualization. – appsthatmatter Oct 28 '13 at 10:48
  • @jjoe64 ah, thank you! The problem finally turned out to be the `viewport` which I didn't set. After I set the viewport to (0, 500), it can draw the graph in real time just what I wanted. Would you mind to post your comment as an answer? – dumbfingers Oct 28 '13 at 12:40

0 Answers0