D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
RosaRomeroGomez
Full window
Github gist
Bubble Chart D3.v4 One Hierarchical Level
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> text { font: 10px sans-serif; } </style> <body> <script src="//d3js.org/d3.v4.js"></script> <script> var diameter = 500, format = d3.format(",d"), color = "#FABE58"; var bubble = d3.pack() .size([diameter, diameter]) .padding(2); var svg = d3.select("body").append("svg") .attr("width", diameter) .attr("height", diameter) .attr("class", "bubble"); d3.json("data.json", function(error, data) { if (error) throw error; var root = d3.hierarchy(clusters(data)) .sum(function(d) { return d.value; }) .sort(function(a, b) { return b.value - a.value; }); bubble(root); var node = svg.selectAll(".node") .data(root.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.data.clusterID + ": " + format(d.value); }); node.append("circle") .attr("r", function(d) { return d.r; }) .style("fill", color); node.append("text") .attr("dy", ".3em") .style("text-anchor", "middle") .text(function(d) { return d.data.clusterID.substring(0, d.r / 3); }); }); // Returns a flattened hierarchy containing all leaf nodes under the root. function clusters(root) { var clusters = []; function recurse(name, node) { if (node.children) node.children.forEach(function(child) { recurse(node.name, child); }); else clusters.push({topic: name, clusterID: node.name, value: node.size}); } recurse(null, root); return {children: clusters}; } d3.select(self.frameElement).style("height", diameter + "px"); </script>
https://d3js.org/d3.v4.js