Skip to content

Instantly share code, notes, and snippets.

@Hugoberry
Last active May 16, 2017
Embed
What would you like to do?
Bubble Chart
license: gpl-3.0
height: 960
border: no

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.

forked from mbostock's block: Bubble Chart

id value
flare
flare.analytics
flare.analytics.cluster
flare.analytics.cluster.AgglomerativeCluster 1
flare.analytics.cluster.CommunityStructure 2
flare.analytics.cluster.HierarchicalCluster 1
flare.analytics.cluster.MergeEdge 1
flare.analytics.graph
flare.analytics.graph.BetweennessCentrality 1
flare.analytics.graph.LinkDistance 1
flare.analytics.graph.MaxFlowMinCut 1
flare.analytics.graph.ShortestPaths 1
flare.analytics.graph.SpanningTree 1
flare.analytics.optimization
flare.analytics.optimization.AspectRatioBanker 1
<!DOCTYPE html>
<svg width="960" height="960" font-family="sans-serif" font-size="10" text-anchor="middle"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var format = d3.format(",d");
var color = d3.scaleOrdinal(d3.schemeCategory20c);
var pack = d3.pack()
.size([width, height])
.padding(1.5);
d3.csv("flare.csv", function(d) {
d.value = +d.value;
if (d.value) return d;
}, function(error, classes) {
if (error) throw error;
var root = d3.hierarchy({children: classes})
.sum(function(d) { return d.value; })
.each(function(d) {
if (id = d.data.id) {
var id, i = id.lastIndexOf(".");
d.id = id;
d.package = id.slice(0, i);
d.class = id.slice(i + 1);
}
});
var node = svg.selectAll(".node")
.data(pack(root).leaves())
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
node.append("circle")
.attr("id", function(d) { return d.id; })
.attr("r", function(d) { return d.r; })
.style("fill", function(d) { return color(d.package); });
node.append("clipPath")
.attr("id", function(d) { return "clip-" + d.id; })
.append("use")
.attr("xlink:href", function(d) { return "#" + d.id; });
node.append("text")
.attr("clip-path", function(d) { return "url(#clip-" + d.id + ")"; })
.selectAll("tspan")
.data(function(d) { return d.class.split(/(?=[A-Z][^A-Z])/g); })
.enter().append("tspan")
.attr("x", 0)
.attr("y", function(d, i, nodes) { return 13 + (i - nodes.length / 2 - 0.5) * 10; })
.text(function(d) { return d; });
node.append("title")
.text(function(d) { return d.id + "\n" + format(d.value); });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment