Fast and responsive interactive charts/graphs: SVG, Canvas, other?
Asked Answered
L

5

115

I am trying to choose the right technology to use for updating a project that basically renders thousands of points in a zoomable, pannable graph. The current implementation, using Protovis, is underperformant. Check it out here:

http://www.planethunters.org/classify

There are about 2000 points when fully zoomed out. Try using the handles on the bottom to zoom in a bit, and drag it to pan around. You will see that it is quite choppy and your CPU usage probably goes up to 100% on one core unless you have a really fast computer. Each change to the focus area calls a redraw to protovis which is pretty darn slow and is worse with more points drawn.

I would like to make some updates to the interface as well as change the underlying visualization technology to be more responsive with animation and interaction. From the following article, it seems like the choice is between another SVG-based library, or a canvas-based one:

http://www.sitepoint.com/how-to-choose-between-canvas-and-svg/

d3.js, which grew out of Protovis, is SVG-based and is supposed to be better at rendering animations. However, I'm dubious as to how much better and what its performance ceiling is. For that reason, I'm also considering a more complete overhaul using a canvas-based library like KineticJS. However, before I get too far into using one approach or another, I'd like to hear from someone who has done a similar web application with this much data and get their opinion.

The most important thing is performance, with a secondary focus on ease of adding other interaction features and programming the animation. There will probably be no more than 2000 points at once, with those small error bars on each one. Zooming in, out, and panning around need to be smooth. If the most recent SVG libraries are decent at this, then perhaps the ease of using d3 will outweigh the increased setup for KineticJS, etc. But if there is a huge performance advantage to using a canvas, especially for people with slower computers, then I would definitely prefer to go that way.

Example of app made by the NYTimes that uses SVG, but still animates acceptably smoothly: http://www.nytimes.com/interactive/2012/05/17/business/dealbook/how-the-facebook-offering-compares.html . If I can get that performance and not have to write my own canvas drawing code, I would probably go for SVG.

I noticed that some users have used a hybrid of d3.js manipulation combined with canvas rendering. However, I can't find much documentation about this online or get in contact with the OP of that post. If anyone has any experience doing this kind of DOM-to-Canvas (demo, code) implementation, I would like to hear from you as well. It seems to be a good hybrid of being able to manipulate data and having custom control over how to render it (and therefore performance), but I'm wondering if having to load everything into the DOM is still going to slow things down.

I know that there are some existing questions that are similar to this one, but none of them exactly ask the same thing. Thanks for your help.

Follow-up: the implementation I ended up using is at https://github.com/zooniverse/LightCurves

Lilly answered 6/9, 2012 at 23:56 Comment(4)
"The most important thing is performance, with a secondary focus on ease of adding other interaction" +1 for canvasAppeasement
The question is, is SVG sufficient on most browsers for 2k points + other chart elements? If so, and the slowness is just due to weaknesses in protovis, then I would rather stick with SVG.Lilly
Mike Bostock alread gave a good answer. For some additional infos you can check out these two resources: https://mcmap.net/q/73564/-html5-canvas-vs-svg-vs-div/… blogs.msdn.com/b/ie/archive/2011/04/22/…Saccharo
Follow-up: I've implemented this with a hybrid SVG/canvas approach, where the SVG takes care of axes and gridlines and the canvas can render the dots exceedingly quickly. It is super fast!Lilly
A
187

Fortunately, drawing 2000 circles is a pretty easy example to test. So here are four possible implementations, two each of Canvas and SVG:

These examples use D3's zoom behavior to implement zooming and panning. Aside from whether the circles are rendered in Canvas or SVG, the other major distinction is whether you use geometric or semantic zooming.

Geometric zooming means you apply a single transform to the entire viewport: when you zoom in, circles become bigger. Semantic zooming in contrast means you apply transforms to each circle individually: when you zoom in, the circles remain the same size but they spread out. Planethunters.org currently uses semantic zooming, but it might be useful to consider other cases.

Geometric zooming simplifies the implementation: you apply a translate and scale once, and then all the circles are re-rendered. The SVG implementation is particularly simple, updating a single "transform" attribute. The performance of both geometric zooming examples feels more than adequate. For semantic zooming, you'll notice that D3 is significantly faster than Protovis. This is because it's doing a lot less work for each zoom event. (The Protovis version has to recalculate all attributes on all elements.) The Canvas-based semantic zooming is a bit more zippy than SVG, but SVG semantic zooming still feels responsive.

Yet there is no magic bullet for performance, and these four possible approaches don't begin to cover the full space of possibilities. For example, you could combine geometric and semantic zooming, using the geometric approach for panning (updating the "transform" attribute) and only redrawing individual circles while zooming. You could probably even combine one or more of these techniques with CSS3 transforms to add some hardware acceleration (as in the hierarchical edge bundling example), although that can be tricky to implement and may introduce visual artifacts.

Still, my personal preference is to keep as much in SVG as possible, and use Canvas only for the "inner loop" when rendering is the bottleneck. SVG has so many conveniences for development—such as CSS, data-joins and the element inspector—that it is often premature optimization to start with Canvas. Combining Canvas with SVG, as in the Facebook IPO visualization you linked, is a flexible way to retain most of these conveniences while still eking out the best performance. I also used this technique in Cubism.js, where the special case of time-series visualization lends itself well to bitmap caching.

As these examples show, you can use D3 with Canvas, even though parts of D3 are SVG-specific. See also this force-directed graph and this collision detection example.

Ayannaaycock answered 8/9, 2012 at 23:59 Comment(7)
Wow, that was an awesome answer, and from the master of visualization himself! I think I'd have to stick with semantic zooming, and on my computer, the canvas-based renderer is way faster than the SVG version when panning/zooming (might have to do with the browser implementation?). What you said about using SVG with canvas as the inner loop is exactly what I was looking to confirm, and the code examples are just a sweet bonus. Thanks so much!Lilly
Just had the thought to try the semantic zooming examples out on different browsers: Chrome, both pretty fast, I can't tell the difference; IE: SVG slightly slower; Firefox (last comment): SVG is hella slow compared to canvas. I guess that also complicates the decision a bit, but makes canvas rendering a safe choice. One more question: Is using KineticJS instead of canvas directly going to affect performance significantly?Lilly
Andrew, a little late but here is my experience with FF: It's catching up. I used to run FF 15 and D3 SVG transitions quickly started getting slow. But each new version got substantially faster. Now I am on FF 18 beta and it's fast compared to 17. Not sure whether it is as smooth as chrome though.Alded
@AndrewMao Hi Andrew, I run into a situation where it seems that rendering is the bottleneck. I need to pan and zoom some points and about 6000 curve path. https://mcmap.net/q/189694/-svg-path-rendering-speed/… But I don't quite understand Bostock when he said "keep as much in SVG as possible, and use Canvas only for the "inner loop"" I have looked at the four examples though.. Could you shed me some light?Outride
@Outride is the transformation equally slow in all browsers? If so I would say you either are using the wrong code or you've hit the limits of browser rendering. If you could post some code I may be able to help. mbostock was referring to using SVG for simplicity of manipulation and canvas only as necessary since it's more complicated to code. However libraries such as KineticJS have to some extent simplified that.Lilly
@AndrewMao What does 'inner loop' refer to?Outride
@AndrewMao I'm simply using SVG geometric zooming, add a single transform, g.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")"), to the entire svg. I think I have hit the limits of browser rendering. Please also refer to my question link.Outride
A
9

I think that in your case the decision between canvas and svg is not like a decision between »riding a Horse« or driving a »Porsche«. For me it is more like the decision about the cars color.

Let me explain: Assuming that, based on the framework the operations

  • draw a star,
  • add a star and
  • remove a star

take linear time. So, if your decision of the framework was good it is a bit faster, otherwise a bit slower.

If you go on assuming that the framework is just fast, than it becomes totally obvious that the lack of performance is caused be the high amount of stars and handling them is something none of the frameworks can do for you, at least I do not know about this.

What I want to say is that the base of the problem leads to a basic problem of computational geometry, namely: range searching and another one of computer graphics: level of detail.

To solve your performance problem you need to implement a good preprocessor which is able to find very fast which stars to display and is perhaps able to cluster stars which are close together, depending on the zoom. The only thing that keeps your view vivid and fast is keeping the number of stars to draw as low possible.

As you stated, that the most important thing is performance, than I would tend to use canvas, because it works without DOM operations. It also offers the opportunity to use webGL, what increases graphic performance a lot.

BTW: did you check paper.js? It uses canvas, but emulates vector graphics.

PS: In this Book you can find a very detailed discussion about graphics on the web, the technologies, pros and cons of canvas, SVG and DHTML.

Appeasement answered 8/9, 2012 at 11:45 Comment(0)
H
7

I recently worked on a near-realtime dashboard (refresh every 5 seconds) and chose to use charts that render using canvas.

We tried Highcharts(SVG based JavaScript Charting library) and CanvasJS(Canvas based JavaScript Charting library). Although Highcharts is a fantastic charting API and offers way more features we decided to use CanvasJS.

We needed to display at least 15 minutes of data per chart (with option to pick range of max two hours).

So for 15 minutes: 900 points(data point per second) x2(line and bar combination chart) x4 charts = 7200 points total.

Using chrome profiler, with CanvasJS the memory never went above 30MB while with Highcharts memory usage exceeded 600MB.

Also with refresh time of 5 seconds CanvasJS rendering was allot more responsive then Highcharts.

We used one timer (setInterval 5 seconds) to make 4 REST API calls to pull the data from back end server which connected to Elasticsearch. Each chart updated as data is received by JQuery.post().

That said for offline reports I would go with Highcharts since its more flexible API.

There's also Zing charts which claims to use either SVG or Canvas but haven't looked at them.

Canvas should be considered when performance is really critical. SVG for flexibility. Not that canvas frameworks aren't flexible, but it takes allot more work for canvas framework to get the same functionality as an svg framework.

Heartstrings answered 7/8, 2014 at 15:47 Comment(0)
B
3

Might also look into Meteor Charts, which is built on top of the uber fast KineticJS framework: http://meteorcharts.com/

Binary answered 31/5, 2013 at 5:22 Comment(0)
P
1

I also found when we print to PDF a page with SVG graphics, the resulting PDF still contains a vector-based image, while if you print a page with Canvas graphics, the image in the resulting PDF file is rasterized.

Polychaete answered 25/7, 2020 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.