Skip to content

Instantly share code, notes, and snippets.

@elijahc
Forked from anonymous/jsbin.ugacud.html
Created September 15, 2013 19:50
Show Gist options
  • Save elijahc/6573822 to your computer and use it in GitHub Desktop.
Save elijahc/6573822 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<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>
<script>
var json = {
"name": "flare with a space",
"children": [
{
"name": "analytics a very long description",
"children": [
{
"name": "cluster",
"children": [
{"name": "AgglomerativeCluster", "size": 3938},
{"name": "CommunityStructure", "size": 3812},
{"name": "HierarchicalCluster", "size": 6714},
{"name": "MergeEdge", "size": 743}
]
},
{
"name": "graph",
"children": [
{"name": "BetweennessCentrality", "size": 3534},
{"name": "LinkDistance", "size": 5731},
{"name": "MaxFlowMinCut", "size": 7840},
{"name": "ShortestPaths", "size": 5914},
{"name": "SpanningTree", "size": 3416}
]
},
{
"name": "optimization",
"children": [
{"name": "AspectRatioBanker", "size": 7074}
]
}
]
}
]
};
</script>
<script src="http://mbostock.github.com/d3/d3.v2.js?2.8.1"></script>
</head>
<body>
<h1 id='dendrogram'>Dendrogram</h1>
<div class='gallery' id='chart'> </div>
<script type='text/javascript'>
var width = 960, height = 900;
var cluster = d3.layout.tree()
.size([height, width - 160]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.x, d.y]; });//flip it onto its side!
//var diagonal = d3.svg.line()
//.x(function(d) { return d.y; })
//.y(function(d) { return d.x; })
//.interpolate("basis");
//set up the visualisation:
var vis = d3.select("#chart").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(50, 0)"); //once the graph is materialised, transform it 50 to the right (x), 0 up (y)
var nodes = cluster.nodes(json);
var link = vis.selectAll("path.link")
.data(cluster.links(nodes))
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal);
var node = vis.selectAll("g.node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
//the circles on the nodes:
node.append("circle")
.attr("r", 4.5);
//where to put the text, if it has children, then place -8 to the left of the node, otherwise, +8 to the right
node.append("text")
.attr("dx", function(d) { return d.children ? -10 : 10; })
.attr("dy", function(d) { return d.children ? 5 : -10; })
.attr("transform", "rotate(45)")
.attr("text-anchor", function(d) { return d.children ? "end" : "start"; })
.text(function(d) { var sz = (d.size == undefined ? '': ' => '+d.size+' classes');
return d.name + sz; });
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment