Most responsive data graphing implementation
-
As a learning project, I'm working on building my own graphing program 'from scratch', and have been toying around with different implementations. Right now, I'm loading in the data I want to graph (~30,000 points) into a vector, then I generate a QPainterPath by scaling the X and Y values by the size of the parent widget, and I draw it using a QPainter. Resize events trigger redraws where the QPainterPath is scaled up or down.
This works nicely, but I was hoping to include some responsive mouse events where the data is highlighted in a different color when the mouse intersects the QPainterPath. If I graph multiple QPainterPaths, I'm starting to already notice a bit of lag with the data highlight. The other problem is that QPainterPath acts as a 'closed path', even if it isn't drawn that way, so my mouse intersection is triggered inappropriately if it happens to be in any concave-like part of the data (since it's inside the shape). QPolygon has the same problem. I thought detecting intersections from a list of QLines might be too computationally intense, but I haven't actually tried it. Is there any obvious way to graph my data that I'm not thinking of that would allow mouse hover events and also be fast?
I briefly looked at how QCustomPlot does theirs, and as far as I can tell they use drawLine or polyLine calls directly with QPainter instead of using a QPainterPath, and I don't think it supports mouse hover events over the data. Thanks for any ideas.
-
@_bmb For anyone's reference, I've found a good solution is to not use QPainterPath at all, and simply store a QList of QLines (about 30,000 of them for my data), and draw them using QPainter. I put pointers to these lists in a vector, so I can keep track of multiple data sets on the graph if I want.
In order to check for mouse hover events over my data, I create a tiny diagonal line at the current mouse position, iterate through the entire list of 30,000 lines and check each for intersections. Surprisingly, this is a very fast operation and I'm noticing zero lag, even with multiple data sets on the graph.
The benefit of using QLines is that you can return the actual intersection location, which QPainterPath intersections are apparently not capable of.