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
xxxxxxxxxx
<meta charset="utf-8">
<style>
text {
font: 10px sans-serif;
text-anchor: middle;
}
</style>
<svg width="960" height="880"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
// create svg container
var svg = d3.select("svg"),
width = +svg.attr("width");
// format function
var format = d3.format(",d");
// color scale
var color = d3.scaleOrdinal(d3.schemeCategory20c);
// pack layout
var pack = d3.pack()
.size([width, width])
.padding(1.5);
// async data loader
d3.csv("flare.csv", function(d) {
// format data, make sure value is numeric
d.value = +d.value;
if (d.value) return d;
}, function(error, classes) {
if (error) throw error;
// create mai data object called root
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);
}
});
// convert root using pack(root).leaves, and use this as our main data
var data = pack(root).leaves();
// append nodes: essentially here we just set up a collection
// of well stratified g elements, of class 'node'
var node = svg.selectAll(".node")
.data(data)
.enter().append("g")
.attr("class", "node")
// note if we use a g element we have to use translate
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
// append a circle to each g
node.append("circle")
.attr("id", function(d) { return d.id; })
.attr("r", function(d) { return d.r; })
// style the circles via the color scale
.style("fill", function(d) { return color(d.package); });
// append a clipPath to each g, this makes sure that text that falls
// outside a circle will not be shown
node.append("clipPath")
.attr("id", function(d) { return "clip-" + d.id; })
.append("use")
.attr("xlink:href", function(d) { return "#" + d.id; });
// append svg text elements to each g
node.append("text")
.attr("clip-path", function(d,i ) {
return "url(#clip-" + d.id + ")";
})
.selectAll("tspan")
// note for each h,
// here we append new data, based on the already bound data d
.data(function(d) {
// the bound data is based on d.class
// here d.class is text like 'ForceDirectedLayout'
// we convert this into an array m,
// which looks like var m = ["Force", "Directed", "Layout"]
var m = d.class.split(/(?=[A-Z][^A-Z])/g);
return m;
})
// next, for each value in the array (m), we append a tspan element
.enter().append("tspan")
// here x is the center of the g element bound earlier
.attr("x", 0)
// for y we have to compute an offset per for each word
.attr("y", function(d, i, nodes) {
// d is the value in the array, i is the index
// and nodes is the complete array i.e. m
// here we use an offset to position each
// new word an a new line
var offset = (i - nodes.length / 2 - 0.5) * 10;
return 13 + offset;
})
// finally, we set the text in each tspan to d
// (the current word in the array m)
.text(function(d) { return d; });
// append a title element to each g
// so if we mouse over we get some info on the underlying data
node.append("title")
.text(function(d) {
return d.id + "\n" + format(d.value);
});
});
</script>
https://d3js.org/d3.v4.min.js