Combining D3 and Raphael to make a network graph

During the past week I have been working on a visualization for Sveriges Radio about Melodifestivalen, the Swedish qualification for the Eurovision Song Contest.

Every year there is a HUGE fuzz about this show over here in Sweden. I wanted to explore the songwriters in the competition from a dataist perspective. Who are the guys behind the scene?

If you follow Melodifestivalen a few years you will notice how many names occur year after year. By linking every songwriter to the years when they contributed I came up with this network graph.

In making this graph I managed to draw several quite interesting conclusions, for example that there are by far more men than women among the songwriters. And that there is a small elite of songwriters that does particularly well in the competition almost every year.

But this is not what I wanted to blog about today, but rather about the making of this visualization.


I have really come to like the Raphael.js library, but unfortunately it does not provide the same robust support for advanced data visualizations (for example network graphs) as its big brother D3.js. D3 on the other hand lacks Raphael’s broad browser compability, which is important when you are working with a public broadcaster like Sveriges Radio. So what if you could combine the two?

D3 has a really powerful library for making network graphs, or force-directed layouts. I used this library to make the foundation for the graph (take a look at the draft here). I won’t go into details about the code. The bulk is borrowed from this Stack Overflow thread.

The problem with force-directed layouts in D3 is that they quickly tend to become very burdensome for the browser. The user will have to wait for the graph to equilibrate. And that can take some time if you have 100+ nodes. But since I in this case only needed  a static layout I might as well have the computer do all those calculations in advance.

This is the idea: Raphael doesn’t have a built-in way to draw force-directed layouts, instead I take the svg-output from D3 and continue building my visualization (interactivity etc.) on top of that in Raphael. In brief, this is how I went about:

  • I started by copying the svg code from in Firebug (inspect the element and click Copy SVG) and pasted it into an empty document and saved it as an xml-file.
  • Iterated the nodes (circles) in the file and extracted the coordinates (cx,cy). I did this in Ruby using the Hpricot gem.
  • Saved the coordinates and the radius as Javascript objects: id:{ cx: 12.34, cy: 43.21, r: 5}
  • Here is the simple piece of code:
    doc = Hpricot(open("mf-graph.svg"))"//circle").each do |node|
       x = (node.attributes["cx"].to_f*100).round.to_f / 100 # I round the nodes to two decimals to reduce the size of the file.
       y = (node.attributes["cy"].to_f*100).round.to_f / 100
       r = (node.attributes["r"].to_f*100).round.to_f / 100
       id = node.attributes["id"]
       puts "#{id}: {x: #{x}, y: #{y}, r: #{r} },"

With the coordinates of the nodes in hand it was easy to rebuild the graph in Raphael. This way I managed to vastly reduce the loading time and make it more cross-browser friendly. Here is the result once again:

5 Comments on “Combining D3 and Raphael to make a network graph”

  1. Thanks for sharing this. I think it is an interesting solution to the d3-old-browser-incompatibility problem.

    Is the ruby script you wrote something that is generic enough to pull out content from any d3 generated graph – or is it more of a one-off thing? Could be useful if you made it available for others trying to follow this method.

    • Jens Finnäs says:

      Thanks for the comment. I added the Ruby code now as well. I suppose you could use that for any standard force-directed layout in D3. Note that I manually copy-paste the svg-code from the browser to the svg/xml-file, though.

      • Thanks! looks pretty straightforward.

        I guess to really automate this process one would need to node.js to run the d3 code for a time and then pull down the svg.

        It would get a bit more complicated…

  2. cyhung says:

    Thanks so MUCH for sharing! I was looking for a way to do treemap but with cross-browser compatibility. I don’t know why my head got stuck on d3.js before I realized google chart tools (fusion table) does have a treemap. And it renders in svg/vml/html5.

    But in my search for an answer, I found your post. And I think this can be an answer to something else I want to graph – a customized sunburst chart. So I think when the time comes I’ll play with that using your method here. Thanks in advance!

  3. […] “musklene” i d3.js og kombinerte det med Raphaël.js noe bedre nettleser-støtte (her) og instruksjoner om hvordan du “pimper” Google maps i ny fargedrakt. Imponerende […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s