通过绘制图表graphview android中的每个点来为图形设置动画

时间:2018-05-31 04:18:02

标签: java android android-layout mpandroidchart android-graphview

我正在使用GraphView  我需要同时显示图形点作为实时。但问题是,它完全显示图形。但我的要求是,它应该同时绘制图形。

根据suggested one,我根据我的要求进行了修改,但它没有显示为实时图表。

public class MainActivity extends AppCompatActivity {
    private LineGraphSeries<DataPoint> mSeries1;
    private Runnable mTimer1;
    private final Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GraphView graph = (GraphView) findViewById(R.id.graph);
        mSeries1 = new LineGraphSeries<>(generateData());
        graph.addSeries(mSeries1);
    }

    private DataPoint[] generateData() {
       DataPoint[] dataPoint= new DataPoint[]{
               new DataPoint(0, 1),
               new DataPoint(1, 5),
               new DataPoint(2, 4),
               new DataPoint(3, 6),
               new DataPoint(4, 8),
               new DataPoint(5, 10),
       };
       return dataPoint;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mTimer1 = new Runnable() {
            @Override
            public void run() {
                mSeries1.resetData(generateData());
                mHandler.postDelayed(this, 300);
            }
        };
        mHandler.postDelayed(mTimer1, 300);
    }
    @Override
    public void onPause() {
        mHandler.removeCallbacks(mTimer1);
        super.onPause();
    }
}

在Xml中:

<com.jjoe64.graphview.GraphView
    android:layout_width="match_parent"
    android:layout_height="200dip"
    android:id="@+id/graph" />

2 个答案:

答案 0 :(得分:0)

https://github.com/PhilJay/MPAndroidChart

将此库用于图表。它有动画。

答案 1 :(得分:0)

请确保包含以下方法,以便每次添加新数据时图表都滚动到最后。

<强> appendData

public void appendData(E dataPoint,
                       boolean scrollToEnd,
                       int maxDataPoints)

<强>参数:

dataPoint - 值的值必须正确! x值必须是ASC。首先是最低的x值,至少是最高的x值。

scrollToEnd - true =&gt; graphview将滚动到结尾(maxX)

maxDataPoints - 如果达到最大数据计数,最旧的数据值将丢失以避免内存泄漏