jjoe64 / GraphView

Android Graph Library for creating zoomable and scrollable line and bar graphs.
https://github.com/jjoe64/GraphView/wiki
Other
2.76k stars 811 forks source link

Dynamic XY plot #162

Closed Aakash16 closed 10 years ago

Aakash16 commented 10 years ago

can any one explain me why the plot is not dynamic ... it gets plotted an once... public void plot() {

//eData is having data to be plotted //xView initialised to 10. for (int y =0; y <1000; y++) {

        Series.appendData(
                new GraphViewData(graph2LastXValue, Double
                        .parseDouble(eData[y])), AutoScrollX);

        // X-axis control
        if (graph2LastXValue >= Xview) {
         Series.resetData(new GraphViewData[] {});
         graph2LastXValue = 0;
        } else
        graph2LastXValue += 0.1;
            graphView.setViewPort(Xview, Xview+graph2LastXValue);

                      GraphView.removeView(graphView);
              GraphView.addView(graphView);

    }

    }
MarcDahlem commented 10 years ago

It is not plotted dynamically, because you insert and run the loop at the same time. As soon as a new value is added with 'appendData', the graph is refreshed.

If you want a kind of 'animation', you need to do at least a Thread.sleep(time_between_two_points).

Aakash16 commented 10 years ago

thanks MarcDahlem, actually i want to pick a value from array and display it by appending to the series... thats why i thought of using for loop.... is there a way to plot series from array and display dynamically ..... so that it seems like a real time graph plot....

MarcDahlem commented 10 years ago

You could simulate this behaviour for example with a timer:

private class MyTimerTask extends TimerTask {
  Random random;
  int counter;

  public MyTimerTask() {
      this.counter=0;
      this.random=new Random();
  }

  @Override
  public void run() {
      appendData...
      if (counter++ <=100) {
          //setup new timer
          int nextTime=MainActivity.MIN_DELAY+random.nextInt(MainActivity.MAX_TIMER_DEVIATION);
          MainActivity.this.timer.schedule(this, nextTime);
    }
  }
}

And in your MainActivity you need to start a single timer with this timertask.