Skip to content

Instantly share code, notes, and snippets.

@ABSegler
Forked from mbostock/.block
Last active August 29, 2015 14:25
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 ABSegler/e55280307535762a4b62 to your computer and use it in GitHub Desktop.
Save ABSegler/e55280307535762a4b62 to your computer and use it in GitHub Desktop.

This “icicle” diagram uses d3.layout.partition to divide space with area proportional to the value of nodes in a tree. See also the zoomable icicle.

<!DOCTYPE html>
<meta charset="utf-8">
<title>Partition - Icicle</title>
<style>
.node {
fill: #ddd;
stroke: #fff;
}
.label {
font: 10px sans-serif;
text-anchor: middle;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
var width = 960,
height = 500;
var color = d3.scale.category20();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var partition = d3.layout.partition()
.size([width, height])
.value(function(d) { return d.size; });
d3.json("/mbostock/raw/4063550/flare.json", function(error, root) {
if (error) throw error;
var nodes = partition.nodes(root);
svg.selectAll(".node")
.data(nodes)
.enter().append("rect")
.attr("class", "node")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y; })
.attr("width", function(d) { return d.dx; })
.attr("height", function(d) { return d.dy; })
.style("fill", function(d) { return color((d.children ? d : d.parent).name); });
svg.selectAll(".label")
.data(nodes.filter(function(d) { return d.dx > 6; }))
.enter().append("text")
.attr("class", "label")
.attr("dy", ".35em")
.attr("transform", function(d) { return "translate(" + (d.x + d.dx / 2) + "," + (d.y + d.dy / 2) + ")rotate(90)"; })
.text(function(d) { return d.name; });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment