0

I'm new to android and now I want to write an application which show an array of 8000 double which is sin and cos signal on a chart, every 7 seconds sin chart will change to cos and vice versa. I used GraphView class to draw the chart. but I can't change the chart every 7 second and it draw it just for first time and it won't change any more. the on create method is as follow :

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = (TextView)findViewById(R.id.text);
    text2 = (TextView)findViewById(R.id.text2);
    Handler handler = new Handler();
    GraphView graphView = new LineGraphView(
            this
            , "Chart"
        );
    GraphViewData [] data= new GraphViewData [8000];
    for (int i = 0; i < data.length; i++) {
        data [i] = new GraphViewData(i,Math.sin(i*Math.PI/20.0)*40+50);}

    GraphViewSeries graphViewSeries = new GraphViewSeries(data);
    graphView.setManualYAxisBounds(130, 0);
    graphView.setScrollable(true);
    graphView.setScalable(true);
    graphView.getGraphViewStyle().setGridStyle(GridStyle.BOTH);
    LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
    graphView.getGraphViewStyle().setTextSize(11);
    graphView.addSeries(graphViewSeries);
    final GraphView graphView2 = graphView;
    final LinearLayout layout2=layout;
     runOnUiThread(new Runnable(){
            @Override

            public void run(){

                layout2.removeAllViews();
                layout2.addView(graphView2);
            }
        });

    for (int count = 0; count < 10; count++){
        final int count2=count;

        if(count2%2==0){
            for (int i = 0; i < data.length; i++) {

                data [i] = new GraphViewData(i,Math.sin(i*Math.PI/20.0)*20+50);
            }
        }
        else{

            for (int i = 0; i < data.length; i++) {
                data [i] = new GraphViewData(i,Math.cos(i*Math.PI/20.0)*45+50);

            }

        }

          graphView.removeAllSeries();
          graphViewSeries.resetData(data);
          graphView.addSeries(graphViewSeries);
          final GraphView graphView3=graphView;
          final LinearLayout layout3=layout;
        handler.postDelayed(new Runnable(){
            @Override
            public void run() {
                text2.setText(s);
                graphView3.redrawAll();
                runOnUiThread(new Runnable(){
                    @Override

                    public void run(){

                        layout3.removeAllViews();
                        layout3.addView(graphView3);
                    }
                });

            }
        }, 7000* (count + 1) );
    }

}

I can't recognise what is it's problem. One way is that I create a new GraphView instance in the loop which should be final but I don't want to redraw all my chart every time because every time it draw it first it move to right for a second and then expand and place in the proper position. I mean I just want to redraw chart every 7 second

Paweł Fus
  • 44,795
  • 3
  • 61
  • 77
sandra
  • 949
  • 3
  • 12
  • 25

1 Answers1

0

why are you removing all series from the graph?

if you have two series and want to reset the data of them you just have to call the resetData() method of the GraphViewSeries objects.

Take a look at the example at the GraphView-Demos project.

http://android-graphview.org/#doc_realtime

https://github.com/jjoe64/GraphView-Demos/blob/master/graphviewdemos/src/main/java/com/jjoe64/graphviewdemos/RealtimeGraph.java#L111

appsthatmatter
  • 6,347
  • 3
  • 36
  • 40