Search code examples
javaandroidandroid-studioandroid-graphview

Android Graph View Update


As a Java & Android beginner I find it still hard to understand most documentations, but I think this can be solved easily by someone who knows what they are doing:

I'm using Graph View for Android Studio to build a simple graph. This is working good so far, but what I want to achieve is to set the last variable of my data array +1 or -1 by the push of a button.

So I generate a fixed dataset when the app starts:

private LineGraphSeries<DataPoint> mSeries1 = new LineGraphSeries<DataPoint>(new DataPoint[] {
        new DataPoint(1, 1),
        new DataPoint(2, 5),
        new DataPoint(3, 3),
        new DataPoint(4, 2),
        new DataPoint(5, mCigCount)
});

where mCigCount is the variable I spoke of. It gets +1 or -1 by a button push, which does work already fine.

The problem is, how do I update my Graph so that the new value for mCigCount will be rendered correctly after the button push?

This tutorial does it in a complicated way with some random numbers and updates every second, which is still way too confusing for me. It also mentions the methods resetData and appendData on which I fail to use correctly, because I dont understand what I have to put inside the (...):

mSeries1.resetData(...);
mSeries1.appendData(...);

How can I adress my last data point and update the mCigCount in it?


Solution

  • Okay I found a workaround for me, although this is not a good solution in my eyes, so please feel free to correct me with resetData and appendData!

    I basically erase every Data in my series and then write it completely new. Problem with this method is, that the view is flickering at each update because it gets deleted and recreated:

    GraphView graph = (GraphView) findViewById(R.id.graph);
        graph.removeAllSeries();
        LineGraphSeries<DataPoint> mSeries1 = new LineGraphSeries<DataPoint>(new DataPoint[] {
                new DataPoint(1, 1),
                new DataPoint(2, 5),
                new DataPoint(3, 3),
                new DataPoint(4, 2),
                new DataPoint(5, mCigCount)
        });
        LineGraphSeries<DataPoint> series = mSeries1;
        graph.addSeries(series);