添加新数据时不会更新GraphView

时间:2014-12-09 09:14:09

标签: java android graph real-time android-graphview

我有一个GraphView在添加新数据时没有更新,它会更改Y值以适应新数据,但它不会绘制它们,这里是代码:

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

3 个答案:

答案 0 :(得分:0)

现在它的摩擦值却没有改变UI,因为你需要在单独的线程中进行这样的UI更新操作。您需要将onClick内的所有代码放在Thread / Runnable中。在Thread&内创建onClick个对象放置那段代码。

请参阅以下链接:

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

答案 1 :(得分:0)

哇现在真的有用了,花了我一段时间注意到我需要一个处理程序...谢谢,继承我的其他像我一样的代码:

refreshButton.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            graphHandler.post(
            graphRunnable = new Runnable()
            {
                public void run() 
                {
                    sensorSeries.appendData(new GraphViewData(xgraph, dataAcx), false, 20);
                    graphView.redrawAll();
                    myLabel.setText(""+dataAcx+" "+xgraph);
                    xgraph++;
                    dataAcx++;
                }
            }
            );
        }
    });

答案 2 :(得分:0)

要使更改生效,您需要致电:

graphView.onDataChanged(false, false);

代替graphView.redrawAll()实现中的View.OnClickListener。您必须使用参数来确保最佳性能,请看一下源代码中的javadoc:

/**
 * Call this to let the graph redraw and recalculate the viewport.
 *
 * This will be called when a new series was added or removed and when data was appended 
 * via {@link com.jjoe64.graphview.series.BaseSeries#appendData(com.jjoe64.graphview.series.DataPointInterface, boolean, int)}
 * or {@link com.jjoe64.graphview.series.BaseSeries#resetData(com.jjoe64.graphview.series.DataPointInterface[])}.
 *
 * @param keepLabelsSize true if you don't want to recalculate the size of the labels. 
 *                       It is recommended to use "true" because this will improve 
 *                       performance and prevent a flickering.
 * @param keepViewport true if you don't want that the viewport will be recalculated.
 *                     It is recommended to use "true" for performance.
 */
public void onDataChanged(boolean keepLabelsSize, boolean keepViewport) { /*...*/ }

完全不需要Thread / Runnable