Skip to content

Instantly share code, notes, and snippets.

@zanarmstrong
Last active January 14, 2016 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zanarmstrong/c91e596a3769afcd494f to your computer and use it in GitHub Desktop.
Save zanarmstrong/c91e596a3769afcd494f 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 radial layout.

forked from mbostock's block: Cluster Dendrogram

forked from anonymous's block: Cluster Dendrogram

[{
"name": "neuron",
"children": [{
"name": "interneuron"
}, {
"name": "pyramidalNeuron",
"children": [{
"name": "CA1"
}, {
"name": "S1"
}]
}, {
"name": "striatalNeuron"
}]
}, {
"name": "glia",
"children": [{
"name": "astrocyte"
}, {
"name": "oligodendrocyte"
}, {
"name": "microglia"
}, {
"name": "endothelial"
}]
}]
<!DOCTYPE html>
<meta charset="utf-8">
<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="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 600,
height = 100;
var cluster = d3.layout.cluster()
.size([height, width - 160]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(40,0)");
var data = [{
"name": "neuron",
"children": [{
"name": "interneuron"
}, {
"name": "pyramidalNeuron",
"children": [{
"name": "CA1"
}, {
"name": "S1"
}]
}, {
"name": "striatalNeuron"
}]
}, {
"name": "glia",
"children": [{
"name": "astrocyte"
}, {
"name": "oligodendrocyte"
}, {
"name": "microglia"
}, {
"name": "endothelial"
}]
}]
root = data[1]
for (k in data){
console.lg
var nodes = cluster.nodes(root),
links = cluster.links(nodes);
nodes.forEach(function(d) { d.y = d.depth * 100; });
var link = svg.selectAll(".link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal);
var node = svg.selectAll(".node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })
node.append("circle")
.attr("r", 4.5);
node.append("text")
.attr("dx", function(d) { return d.children ? -8 : 8; })
.attr("dy", 3)
.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
.text(function(d) { return d.name; });
}
d3.select(self.frameElement).style("height", height + "px");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment