14.Dynamic & Realtime Data(MPAndroidChart中文翻译)

目录

第8节.Setting Colors(MPAndroidChart中文翻译)
第9节.Formatting Data Values (ValueFormatter)(MPAndroidChart中文翻译)
第10节-Formatting Axis Values (AxisValueFormatter)(MPAndroidChart中文翻译)
第11节.General Settings & Styling(MPAndroidChart中文翻译)
第12节.Specific Settings & Styling(MPAndroidChart中文翻译)
第13节.Legend(MPAndroidChart中文翻译)
第14节.Dynamic & Realtime Data(MPAndroidChart中文翻译)
第15节. Modifying the Viewport(MPAndroidChart中文翻译)
第16节.Animations(MPAndroidChart中文翻译)
第17节. MarkerView (Popup View)(MPAndroidChart中文翻译)
第18节. The ChartData class(MPAndroidChart中文翻译)
第19节. ChartData subclasses(MPAndroidChart中文翻译)
第20节. The DataSet class (general DataSet styling)(MPAndroidChart中文翻译)
第21节. DataSet subclasses (specific DataSet styling)(MPAndroidChart中文翻译)
第22节. The ViewPortHandler(MPAndroidChart中文翻译)
第23节. Customizing the Fill-Line-Position (FillFormatter)(MPAndroidChart中文翻译)
第24节. Proguard(MPAndroidChart中文翻译)
第25节. Realm.io mobile database(MPAndroidChart中文翻译)
第26节. Creating your own (custom) DataSets(MPAndroidChart中文翻译)
第27节. Miscellaneous (more useful stuff)(MPAndroidChart中文翻译)

v1.6.3版本开始提供-经常更新.

为了实时的图表中添加或者删除数据,提供了很多方法向一个已有的DataSet对象中添加/删除Entry对象,或向已有的ChartData对象中添加/删除DataSet对象.

Possibilities of adding / removing data dynamically(动态添加和移除数据的可能性)

DataSet类(所有子类)

  • addEntry(Entry e): 向DataSet对象中添加Entry对象.

ChartData类(所有子类)

  • addEntry(Entry e, int dataSetIndex): 向ChartData的指定索引处的DataSet集合中添加Entry对象.
  • addDataSet(DataSet d): 向ChartData中添加DataSet集合

在此之上,还有很多移除实时数据的方法:

DataSet类()

  • public boolean removeFirst(): 移除DataSet条目集合中的第一个Entry对象(索引为0).成功返回true,失败返回false.
  • public boolean removeLast(): 移除DataSet条目集合中的最好一个Entry对象(索引为size-1).成功返回true,失败返回false.
  • public boolean removeEntry(Entry e):移除DataSet条目集合中指定的Entry对象,成功返回true,失败返回false.
  • public boolean removeEntry(int xIndex): 移除DataSet条目集合中指定索引处的Entry对象.成功返回true,失败返回false.

ChartData类(所有子类)

  • public boolean removeEntry(Entry e, int dataSetIndex):移除指定索引处的DataSet集合中的Entry对象.成功返回true,失败返回false.
  • public boolean removeEntry(int xIndex, int dataSetIndex): 移除指定索引处的DataSet集合中的指定索引处的Entry对象.成功返回true,失败返回false.
  • public boolean removeDataSet(DataSet d): 从ChartData中移除DataSet集合.成功返回true,失败返回false.
  • public boolean removeDataSet(int index):从DataChart移除指定索引处的DataSet集合.

Keep in mind(记住)

当进行动态添加或移除操作后,一定要在 刷新图表(调用invalidate()方法)之前调用notifyDataSetChanged()方法.

// EXAMPLE 1
 // add entries to the "data" object
 exampleData.addEntry(...);
 chart.notifyDataSetChanged(); // let the chart know it's data changed
 chart.invalidate(); // refresh

 // EXAMPLE 2
 // add entries to "dataSet" object
 dataSet.addEntry(...);
 exampleData.notifyDataChanged(); // let the data know a dataSet changed
 chart.notifyDataSetChanged(); // let the chart know it's data changed
 chart.invalidate(); // refresh

注意:像moveViewTo(…)这类方法都会自动调用invalidate()方法.

Examples of dynamic data(动态数据的例子)
如何实现动态添加或删除数据的例子,请参考实例app和下面的样例Activity:

    原文作者:xiaobug
    原文地址: https://www.jianshu.com/p/89c1237795f6
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞