Start by having a look at the DataSet
and BaseDataSet
classes in the MPAndroidChart source.
The DataSet
is backed by a simple list of Entry
. Note also that MPAndroidChart needs to know the xMin
, xMax
, yMin
and yMax
for its own internal calculations and these are encapsulated in the DataSet
object.
Every time you call notifyDataSetChanged()
the min/max are recalculated inside the DataSet
object which involves iterating over the entire backing list of Entry
. Moreover, adding an unordered Entry
will trigger iteration of the backing list to find the correct position in the list. Removal of an Entry
will likewise trigger iteration of the backing list. In short, the DataSet
object is optimised for adding ordered Entry
only.
If you suspect that creation of new DataSet
objects with 512 Entry
is a bottleneck and could be optimised by offloading to another thread, I suggest you write a microbenchmark to check the time taken for recreating the DataSet
object and adding the Entry
you want. While new object allocation in Android is expensive, the actual iteration of the Entry
list in order to calculate the min/max is unlikely to be expensive because of CPU caching and spatial locality. If you want to make the case for offloading the construction of the new DataSet
object to a new thread (using, say, an AsyncTask
) you would have to prove in your benchmark that the overhead from offloading to another thread is small enough to justify your efforts.
Once you have called mChart.setData(newDataSet)
then there is very little opportunity for multithreading without making extensive modification to the library. You can see the flow of control for yourself: setData
triggers calculation of offsets and preparing matrices. Calculating the offsets is simple floating point addition (inexpensive), the transformation matrices are handled using native C++ code (already optimised).
When the View
is drawn, the code in onDraw()
must necessarily be executed on the main/UI thread. Although all this may seem sub-optimal, MPAndroidChart still manages to achieve a high level of performance in comparison to other charting libraries as can be seen in this blog post. You can see for yourself what frame rate you are achieving by calling mChart.setLogEnabled(true)
and inspecting the logcat for the frame rate.
If you are unsatisfied with the frame rate you are getting, you could consider SciChart which has better performance. Do note, however, that there is a licensing cost for the same. The tl;dr from this is there is only a little space for optimisation in MPAndroidChart as it stands and if performance is a requirement you may have to look to another library.