-
-
Notifications
You must be signed in to change notification settings - Fork 9k
Dynamic & Realtime Data
Available since v1.6.3 - constantly improved.
For adding new data to the chart or removing data dynamically, there are various methods that allow to either add or remove Entry
objects to an existing DataSet
or DataSet
objects to/from an existing ChartData
object.
###Possibilities of adding / removing data dynamically
Class DataSet
(and all subclasses):
-
addEntry(Entry e)
: Adds the givenEntry
object to theDataSet
.
Class ChartData
(and all subclasses):
-
addEntry(Entry e, int dataSetIndex)
: Adds the givenEntry
to theDataSet
at the specified dataset index. -
addDataSet(DataSet d)
: Adds the givenDataSet
object to theChartData
object.
In addition to that, there are also methods for removing data dynamically:
Class DataSet
(and all subclasses):
-
public boolean removeFirst()
: Removes the first Entry (at index 0) of this DataSet from the entries array. Returns true if successful, false if not. -
public boolean removeLast()
: Removes the last Entry (at index size-1) of this DataSet from the entries array. Returns true if successful, false if not. -
public boolean removeEntry(Entry e)
: Removes the givenEntry
object from theDataSet
. Returns true if successful. -
public boolean removeEntry(int xIndex)
: Removes theEntry
at the given x-index from theDataSet
. Returns true if successful.
Class ChartData
(and all subclasses):
-
public boolean removeEntry(Entry e, int dataSetIndex)
: Removes the givenEntry
object from theDataSet
with the given dataset index. Returns true if successful. -
public boolean removeEntry(int xIndex, int dataSetIndex)
: Removes theEntry
at the given x-index from theDataSet
with the given dataset index. Returns true if successful. -
public boolean removeDataSet(DataSet d)
: Removes the givenDataSet
object from theChartData
object. Returns true if successful. -
public boolean removeDataSet(int index)
: Removes theDataSet
at the given index from theChartData
object. Returns true if successful.
###Keep in mind
After adding or removing data dynamically, notifyDataSetChanged()
must be called on the chart before refreshing it (by calling invalidate()
).
// 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
Note: Methods like moveViewTo(...)
will automatically call invalidate()
.
Examples of dynamic data
For examples of how to implement dynamical data adding/removing, please refer the example app and the following example Activities
in it: