Circle Packing

Implementation based on work by Jeff Heer. Data shows the Flare class hierarchy, also courtesy Jeff Heer.

Source Code

 1 var w = 960,
 2     h = 960,
 3     format = d3.format(",d");
 4 
 5 var pack = d3.layout.pack()
 6     .size([w - 4, h - 4])
 7     .value(function(d) { return d.size; });
 8 
 9 var vis = d3.select("#chart").append("svg:svg")
10     .attr("width", w)
11     .attr("height", h)
12     .attr("class", "pack")
13   .append("svg:g")
14     .attr("transform", "translate(2, 2)");
15 
16 d3.json("../data/flare.json", function(json) {
17   var node = vis.data([json]).selectAll("g.node")
18       .data(pack.nodes)
19     .enter().append("svg:g")
20       .attr("class", function(d) { return d.children ? "node" : "leaf node"; })
21       .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
22 
23   node.append("svg:title")
24       .text(function(d) { return d.name + (d.children ? "" : ": " + format(d.size)); });
25 
26   node.append("svg:circle")
27       .attr("r", function(d) { return d.r; });
28 
29   node.filter(function(d) { return !d.children; }).append("svg:text")
30       .attr("text-anchor", "middle")
31       .attr("dy", ".3em")
32       .text(function(d) { return d.name.substring(0, d.r / 3); });
33 });
Copyright © 2011 Mike Bostock
Fork me on GitHub