重新绘制GraphView图表peridicaly

时间:2014-09-11 15:37:43

标签: android charts android-graphview

我是android的新手,现在我想编写一个应用程序,它显示一个8000倍的数组,即图表上的sin和cos信号,每7秒sin图表将变为cos,反之亦然。我使用GraphView类绘制图表。但我不能每7秒更换一次图表,它只是第一次画出来而且它不再发生变化。 on create方法如下:

  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) );
    }

}

我无法识别它的问题所在。一种方法是我在循环中创建一个新的GraphView实例,这应该是最终的,但我不想每次都重绘我的所有图表,因为每次它首先绘制它,然后向右移动一秒,然后展开放在合适的位置。我的意思是我只想每7秒重绘一次图表

1 个答案:

答案 0 :(得分:0)

为什么要从图表中删除所有系列?

如果你有两个系列并希望重置它们的数据,你只需要调用GraphViewSeries对象的resetData()方法。

在GraphView-Demos项目中查看示例。

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

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