Search code examples
javaandroidgraphreal-timeandroid-graphview

GraphView is not updated when new data is added


I have a GraphView that is not updated when new data is added, it changes the Y value to accommodate for the new data, but it doesn't draw them, here is the code:

 public class MainActivity extends Activity {
    double data = 1;
    double xgraph = 5;
    GraphViewSeries sensorSeries;
    GraphView graphView;
    ...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        sensorSeries = new GraphViewSeries(new GraphViewData[] {
                new GraphViewData(1, 2.0d),
                new GraphViewData(2, 1.5d),
                new GraphViewData(3, 2.5d),
                new GraphViewData(4, 1.0d)
        });
        graphView = new LineGraphView(this,"TEST");
        graphView.addSeries(sensorSeries);
        LinearLayout layout = (LinearLayout) findViewById(R.id.graph2);
        graphView.setViewPort(0, 20);
        layout.addView(graphView);

        refreshButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                sensorSeries.appendData(new GraphViewData(xgraph, data), false);
                graphView.redrawAll();
                myLabel.setText(""+data+" "+xgraph);
                xgraph++;
                dataAcx++;
            }
        });
    }
    ...
}

Solution

  • Right now its chafing values but not changing UI, because you need to do such UI update operations in separate thread. You need to place all your code which is inside onClick in Thread/Runnable. Create Thread object inside onClick & place that code.

    Refer below link :

    http://karanbalkar.com/2014/05/display-graphs-using-graphview-in-android/