Skip to content

Instantly share code, notes, and snippets.

@gilbarbara
Last active December 20, 2015 13:39
Show Gist options
  • Save gilbarbara/6140905 to your computer and use it in GitHub Desktop.
Save gilbarbara/6140905 to your computer and use it in GitHub Desktop.
Bubble Tree example.

Implementation based on Gregor Aisch's work with Raphäel

<!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 = 960,
format = d3.format(",d"),
color = d3.scale.category20c();
var bubble = d3.layout.pack()
.sort(null)
.size([diameter, diameter])
.padding(1.5);
var svg = d3.select("body").append("svg")
.attr("width", diameter)
.attr("height", diameter)
.attr("class", "bubble");
d3.json("tags.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.packageName); });
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>
{
"name": "Last.fm Tags",
"children": [
{ "name": "rock", "size": "3761926" },
{ "name": "seen live", "size": "1957849" },
{ "name": "alternative", "size": "1995418" },
{ "name": "indie", "size": "1897478" },
{ "name": "electronic", "size": "2123652" },
{ "name": "pop", "size": "1775250" },
{ "name": "metal", "size": "1173247" },
{ "name": "female vocalists", "size": "1479274" },
{ "name": "classic rock", "size": "1069579" },
{ "name": "alternative rock", "size": "1078271" },
{ "name": "jazz", "size": "1056591" },
{ "name": "punk", "size": "812903" },
{ "name": "indie rock", "size": "817794" },
{ "name": "folk", "size": "812887" },
{ "name": "ambient", "size": "906040" },
{ "name": "singer-songwriter", "size": "772660" },
{ "name": "experimental", "size": "925091" },
{ "name": "electronica", "size": "607729" },
{ "name": "hard rock", "size": "775106" },
{ "name": "hip-hop", "size": "786567" },
{ "name": "80s", "size": "717351" },
{ "name": "dance", "size": "744899" },
{ "name": "hardcore", "size": "639634" },
{ "name": "black metal", "size": "750688" },
{ "name": "chillout", "size": "619862" },
{ "name": "progressive rock", "size": "621791" },
{ "name": "death metal", "size": "658662" },
{ "name": "instrumental", "size": "736168" },
{ "name": "heavy metal", "size": "619367" },
{ "name": "british", "size": "632949" },
{ "name": "punk rock", "size": "498106" },
{ "name": "soundtrack", "size": "513181" },
{ "name": "industrial", "size": "501073" },
{ "name": "soul", "size": "607123" },
{ "name": "blues", "size": "499149" },
{ "name": "classical", "size": "521096" },
{ "name": "emo", "size": "301726" },
{ "name": "rap", "size": "478753" },
{ "name": "90s", "size": "433052" },
{ "name": "thrash metal", "size": "449884" },
{ "name": "metalcore", "size": "436126" },
{ "name": "trance", "size": "381611" },
{ "name": "japanese", "size": "392044" },
{ "name": "favorites", "size": "255372" },
{ "name": "reggae", "size": "373050" },
{ "name": "acoustic", "size": "445184" },
{ "name": "country", "size": "308886" },
{ "name": "progressive metal", "size": "370489" },
{ "name": "trip-hop", "size": "370961" },
{ "name": "hip hop", "size": "351206" }
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment