【问题标题】:Android graphview not automatically updatingAndroid graphview不会自动更新
【发布时间】:2013-05-28 14:44:54
【问题描述】:

我正在使用 GraphView 库,当我更新系列(或用新系列替换系列)时,我无法让图形自行重绘。当我触摸屏幕时,它似乎只会重新绘制自己。这是我的更新功能。

private void updateGraph(Animal a)
{
    Toast.makeText(getApplicationContext(), "Updating Graph", Toast.LENGTH_SHORT).show();
    GraphViewData[] newData = new GraphViewData[2];
    newData[0] = new GraphViewData(0,a.getAvg());
    newData[1] = new GraphViewData(1,0.2);

    GraphViewSeries newSeries = new GraphViewSeries(a.getName(),dStyle,newData);

    graph.removeSeries(exampleSeries);
    graph.addSeries(newSeries);
    //update horizontal value
    //graph.setHorizontalLabels(new String[]{a.getName()});
}

【问题讨论】:

    标签: android


    【解决方案1】:

    我想通了……

    //test random y value
        double rnd = generator.nextDouble();
    
        // round is an incremental value (1,2,3...)
        GraphViewData newData = new GraphViewData(round,rnd);
    
        exampleSeries.appendData(newData, false);
        graph.redrawAll();
    

    【讨论】:

    • 这已经过时了。该方法目前是graph.refreshDrawableState()
    • 这个答案已经过时了。请参阅下面的my answer 使用 onDataChanged
    【解决方案2】:

    使用onDataChanged 让图形重绘并重新计算视口。

    graph.onDataChanged(true, true);
    

    注意keepLabelsSizekeepViewport 这两个参数。正如 Javadoc 所说:

    • @param keepLabelsSize
      如果您不想重新计算标签的大小,则为 true。建议使用“true”,因为这将提高性能并防止闪烁。
    • @param keepViewport
      如果您不希望重新计算视口,则为 true。建议使用“true”来提高性能。

    无论如何,请记住,当添加或删除新系列以及通过appendDataresetData 附加数据时,onDataChanged 会被隐式调用。因此,在大多数情况下,不需要手动调用 onDataChanged

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多