Circle Packing
Enclosure diagrams use containment to represent the hierarchy. Although circle packing is not as space-efficient as a treemap, it better reveals the hierarchy. Implementation based on work by Jeff Heer. Data shows the Flare class hierarchy, also courtesy Jeff Heer.
Source Code
1 var width = 960,
2 height = 960,
3 format = d3.format(",d");
4
5 var pack = d3.layout.pack()
6 .size([width - 4, height - 4])
7 .value(function(d) { return d.size; });
8
9 var vis = d3.select("#chart").append("svg")
10 .attr("width", width)
11 .attr("height", height)
12 .attr("class", "pack")
13 .append("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("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("title")
24 .text(function(d) { return d.name + (d.children ? "" : ": " + format(d.size)); });
25
26 node.append("circle")
27 .attr("r", function(d) { return d.r; });
28
29 node.filter(function(d) { return !d.children; }).append("text")
30 .attr("text-anchor", "middle")
31 .attr("dy", ".3em")
32 .text(function(d) { return d.name.substring(0, d.r / 3); });
33 });