Search code examples
androidgraphandroid-graphview

How to pause and start a Real time line Graph in GraphView when it is running?


I need to pause and start a line graph in the middle while running.I am using GraphView library. My line graph is showing the graph correctly.I need to implement pause and start from the point where it has paused.How to do that?
Thank you in advance..

I have added my code below.

protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                    requestWindowFeature(Window.FEATURE_NO_TITLE);
                    setContentView(R.layout.fhrlistitemgraph);
                    String data = getIntent().getExtras().getString("data");
                    GraphView graph = (GraphView) findViewById(R.id.llgraphview);
                    series = new LineGraphSeries<DataPoint>();
                    graph.addSeries(series);

                    // customize a little bit viewport
                    Viewport viewport = graph.getViewport();
                    viewport.setYAxisBoundsManual(true);
                    viewport.setMinY(0);
                    viewport.setMaxY(200);
                    //viewport.setYAxisBoundsManual(true);
                    viewport.setScalable(true);
                    viewport.setScrollable(true);
            }
        @Override
                protected void onResume() {
                    super.onResume();
                    // we're going to simulate real time with thread that append data to the
                    // graph
                    new Thread(new Runnable() {

                        @Override
                        public void run() {
                            String value = null;
                            String[] data = data.replaceAll("\\[", "").replaceAll(" ", "").replaceAll("\\]", "").split(",");


                            // we add 100 new entries
                            for (int i = 0; i < data.length; i++) {
                                final int j;
                                j=i;
                                value = data[i];
                                final String values;
                                values = value;
                                runOnUiThread(new Runnable() {

                                    @Override
                                    public void run() {
                                        addEntry(j,values);
                                    }
                                });

                                // sleep to slow down the add of entries
                                try {
                                    Thread.sleep(600);
                                } catch (InterruptedException e) {
                                    // manage error ...
                                }
                            }
                        }
                    }).start();

                }
    private void addEntry(int i,String value) {
            // here, we choose to display max 10 points on the viewport and we
            // scroll to end
            series.appendData(new DataPoint(i, Integer.valueOf(value)), true, 5);
        }

Solution

  • If you stop the thread you've stopped even graph, and when you restart the thread also the graph restart to screen data. You can use a button in UI to stop the thread.

    In a my case, i have insert a boolean to stop the update of graph because the thread was not only that but also other calculations they needed, when the value is true the graph is updated when the value is false the graph is stopped. The value of boolean change whe the button in UI is pressed.

    boolean updateGraph = true;
    if(updateGraph=true){
        series.appendData(new DataPoint(countx++, usedramd), true, 60);
    }
    

    And with the button you change the value of boolean updateGraph.