Skip to content

Instantly share code, notes, and snippets.

@j0hnsmith
Forked from mbostock/.block
Last active August 29, 2015 13:56
Show Gist options
  • Save j0hnsmith/9272589 to your computer and use it in GitHub Desktop.
Save j0hnsmith/9272589 to your computer and use it in GitHub Desktop.

Bubble charts encode data in the area of circles. Although less perceptually-accurate than bar charts, they can pack hundreds of values into a small space. Implementation based on work by Jeff Heer. Data shows the Flare class hierarchy, also courtesy Jeff Heer.

{
"name": "flare",
"children": [
{"name": "AgglomerativeCluster", "size": 0},
{"name": "AgglomerativeCluster1", "size": 0},
{"name": "AgglomerativeCluster2", "size": 0},
{"name": "AgglomerativeCluster3", "size": 0},
{"name": "AgglomerativeCluster4", "size": 0},
{"name": "AgglomerativeCluster5", "size": 0},
{"name": "AgglomerativeCluster6", "size": 0},
{"name": "AgglomerativeCluster7", "size": 0},
{"name": "AgglomerativeCluster8", "size": 0},
{"name": "AgglomerativeCluster9", "size": 0},
{"name": "AgglomerativeCluster10", "size": 0},
{"name": "AgglomerativeCluster11", "size": 0},
{"name": "AgglomerativeCluster12", "size": 0},
{"name": "AgglomerativeCluster13", "size": 0},
{"name": "AgglomerativeCluster14", "size": 0},
{"name": "AgglomerativeCluster15", "size": 0},
{"name": "AgglomerativeCluster16", "size": 0},
{"name": "MergeEdge", "size": 84780862.0},
{"name": "CommunityStructure", "size": 29136745.0},
{"name": "HierarchicalCluster", "size": 23169225.0},
{"name": "BetweennessCentrality", "size": 32695061.0},
{"name": "LinkDistance", "size": 26713571.0},
{"name": "MaxFlowMinCut", "size": 5846479.0},
{"name": "ShortestPaths", "size": 608888.0},
{"name": "SpanningTree", "size": 5925210.0},
{"name": "Easing", "size": 16834016.0},
{"name": "FunctionSequence", "size": 2228888.0}
]
}
<!DOCTYPE html>
<meta charset="utf-8">
<style>
text {
font: 10px sans-serif;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var diameter = 606,
format = d3.format(",d"),
color = d3.scale.category20c();
var bubble = d3.layout.pack()
.sort(null)
.size([diameter, diameter])
.padding(5);
var svg = d3.select("body").append("svg")
.attr("width", diameter)
.attr("height", diameter)
.attr("class", "bubble");
d3.json("flare.json", function(error, root) {
var node = svg.selectAll(".node")
.data(bubble.nodes(classes(root))
.filter(function(d) { return !d.children; }))
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
node.append("title")
.text(function(d) { return d.className + ": " + format(d.value); });
node.append("circle")
.attr("r", function(d) { return d.r; })
.style("fill", function(d) { return color(d.className); });
node.append("text")
.attr("dy", ".3em")
.style("text-anchor", "middle")
.text(function(d) { return d.className.substring(0, d.r / 3); });
});
// Returns a flattened hierarchy containing all leaf nodes under the root.
function classes(root) {
var classes = [];
function recurse(name, node) {
if (node.children) node.children.forEach(function(child) { recurse(node.name, child); });
else classes.push({packageName: name, className: node.name, value: node.size});
}
recurse(null, root);
return {children: classes};
}
d3.select(self.frameElement).style("height", diameter + "px");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment