Skip to content

Instantly share code, notes, and snippets.

@baramuyu
Created September 6, 2016 21:13
Show Gist options
  • Save baramuyu/efa706949c3244327a1d0a846aec69b1 to your computer and use it in GitHub Desktop.
Save baramuyu/efa706949c3244327a1d0a846aec69b1 to your computer and use it in GitHub Desktop.
Treemap
license: gpl-3.0

A treemap recursively subdivides area into rectangles; the area of any node in the tree corresponds to its value. This example uses color to encode different packages of the Flare visualization toolkit. Treemap design invented by Ben Shneiderman. Squarified algorithm by Bruls, Huizing and van Wijk. Data courtesy Jeff Heer.

forked from mbostock's block: Treemap

<!DOCTYPE html>
<meta charset="utf-8">
<style>
rect {
fill: none;
stroke: #fff;
}
text {
font: 10px sans-serif;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500,
color = d3.scale.category20c();
var treemap = d3.layout.treemap()
.padding(4)
.size([width, height])
.value(function(d) { return d.size; });
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(-.5,-.5)");
d3.json("/mbostock/raw/4063550/flare.json", function(error, json) {
if (error) throw error;
var cell = svg.data([json]).selectAll("g")
.data(treemap.nodes)
.enter().append("g")
.attr("class", "cell")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
cell.append("rect")
.attr("width", function(d) { return d.dx; })
.attr("height", function(d) { return d.dy; })
.style("fill", function(d) { return d.children ? color(d.name) : null; });
cell.append("text")
.attr("x", function(d) { return d.dx / 2; })
.attr("y", function(d) { return d.dy / 2; })
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) { return d.children ? null : d.name; });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment