Skip to content

Instantly share code, notes, and snippets.

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

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.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
margin: auto;
position: relative;
width: 960px;
}
form {
position: absolute;
right: 10px;
top: 10px;
}
.node {
border: solid 1px white;
font: 10px sans-serif;
line-height: 12px;
overflow: hidden;
position: absolute;
text-indent: 2px;
}
</style>
<form>
<label><input type="radio" name="mode" value="size" checked> Size</label>
<label><input type="radio" name="mode" value="count"> Count</label>
</form>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 40, right: 10, bottom: 10, left: 10},
width = 670 - margin.left - margin.right,
height = 620 - margin.top - margin.bottom;
var color = d3.scale.category20c();
var treemap = d3.layout.treemap()
.size([width, height])
.sticky(true)
.value(function(d) { return d.size; });
var div = d3.select("body").append("div")
.style("position", "relative")
.style("width", (width + margin.left + margin.right) + "px")
.style("height", (height + margin.top + margin.bottom) + "px")
.style("left", margin.left + "px")
.style("top", margin.top + "px");
d3.json("topics.json", function(error, root) {
var node = div.datum(root).selectAll(".node")
.data(treemap.nodes)
.enter().append("div")
.attr("class", "node")
.call(position)
.style("background", function(d) { return d.children ? color(d.name) : null; })
.text(function(d) { return d.children ? null : d.name; });
d3.selectAll("input").on("change", function change() {
var value = this.value === "count"
? function() { return 1; }
: function(d) { return d.size; };
node
.data(treemap.value(value).nodes)
.transition()
.duration(1500)
.call(position);
});
});
function position() {
this.style("left", function(d) { return d.x + "px"; })
.style("top", function(d) { return d.y + "px"; })
.style("width", function(d) { return Math.max(0, d.dx - 1) + "px"; })
.style("height", function(d) { return Math.max(0, d.dy - 1) + "px"; });
}
</script>
{
"name": "maths",
"children": [
{"name": "Early math", "size": 20},
{"name": "3rd grade (U.S.)", "size": 49},
{"name": "4th grade (U.S.)", "size": 144},
{"name": "5th grade (U.S.)", "size": 100},
{"name": "6th grade (U.S.)", "size": 159},
{"name": "7th grade (U.S.)", "size": 144},
{"name": "8th grade (U.S.)", "size": 202},
{"name": "Arithmetic", "size": 261},
{"name": "Pre-algebra", "size": 384},
{"name": "Algebra I", "size": 588},
{"name": "Basic geometry", "size": 103},
{"name": "Geometry", "size": 219},
{"name": "Algebra II", "size": 307},
{"name": "Trigonometry", "size": 87},
{"name": "Probability and statistics", "size": 170},
{"name": "Precalculus", "size": 165},
{"name": "Differential calculus", "size": 153},
{"name": "Integral calculus", "size": 196},
{"name": "Multivariable calculus", "size": 91},
{"name": "Differential equations", "size": 51},
{"name": "Linear algebra", "size": 136},
{"name": "Recreational math", "size": 71},
{"name": "Math contests", "size": 28}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment