Skip to content

Instantly share code, notes, and snippets.

@deenar
Forked from mbostock/.block
Last active September 26, 2015 15:05
Show Gist options
  • Save deenar/f46be238f632f3a00afe to your computer and use it in GitHub Desktop.
Save deenar/f46be238f632f3a00afe to your computer and use it in GitHub Desktop.
Cluster Dendrogram

A dendrogram is a node-link diagram that places leaf nodes of the tree at the same depth. In this example, the classes (leaf nodes) are aligned on the right edge, with the packages (internal nodes) to the left. Data shows the Flare class hierarchy, courtesy Jeff Heer.

Compare to this Cartesian layout.

<!DOCTYPE html>
<meta charset="utf-8">
<title>Flare Dendrogram</title>
<style>
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 1.5px;
}
.node {
font: 10px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 1.5px;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
var radius = 960 / 2;
var cluster = d3.layout.cluster()
.size([360, radius - 120]);
var diagonal = d3.svg.diagonal.radial()
.projection(function(d) { return [d.y, d.x / 180 * Math.PI]; });
var svg = d3.select("body").append("svg")
.attr("width", radius * 2)
.attr("height", radius * 2)
.append("g")
.attr("transform", "translate(" + radius + "," + radius + ")");
d3.json("https://gist.githubusercontent.com/deenar/c16ac22e53165aad50d5/raw/7f5f4cb610ee3f299018c2058362603bb3519d0f/flare.json", function(error, root) {
if (error) throw error;
var nodes = cluster.nodes(root);
var link = svg.selectAll("path.link")
.data(cluster.links(nodes))
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal);
var node = svg.selectAll("g.node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")"; })
node.append("circle")
.attr("r", 4.5);
node.append("text")
.attr("dy", ".31em")
.attr("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
.attr("transform", function(d) { return d.x < 180 ? "translate(8)" : "rotate(180)translate(-8)"; })
.text(function(d) { return d.name; });
});
d3.select(self.frameElement).style("height", radius * 2 + "px");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment