Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing chart with lots of points efficiently on iOS

I'm looking for a way to draw a line chart with lots of points (up to 10 000) efficiently on iOS. The graph gets real-time data and needs to be updated multiple times a second. I'm trying to figure out a way to draw the graph efficiently so it doesn't max out the CPU and block the main thread while drawing.

I'm currently creating a UIBezierPath for the line chart, in a background thread, adding all the points and drawing it in a CALayer (that has asynchronous drawing enabled). It's not very quick, maxes out the CPU and the drawing is so slow the UI becomes laggy. I am plotting real time data, and in theory I could reuse the same UIBezierPath every time and just append the new points, however older values are discarded after a while, meaning the points for the deleted values would have to be removed from the bezier path, which isn't possible.

I haven't found anything on efficient plotting large data sets on iOS, but I'm hoping there are ways to use the GPU to increase the performance.

like image 911
JonasG Avatar asked Jan 19 '16 21:01

JonasG


1 Answers

I had a situation like this a few years ago (back in iPhone 4 days).

I used a CGMutablePathRef to which I added points using CGPathMoveToPoint for the first point, and CGPathAddLineToPoint for subsequent points.

To get acceptable performance, I stored the data points in a C array (not an NSArray). Also, when plotting a point, if its display coordinates were the same as the last plotted point, I just skipped the call to CGPathAddLineToPoint. (This will often be the case when you have more data points than pixels).

I don't recall the exact drawing time, but it was surprisingly fast.

like image 132
northernman Avatar answered Nov 13 '22 16:11

northernman