Skip to content

Instantly share code, notes, and snippets.

@matt-bernhardt matt-bernhardt/README.md forked from mbostock/.block
Last active Aug 29, 2015

Embed
What would you like to do?

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.

{"children":[{"name":"Computer Science and Artificial Intelligence Laboratory","class":"Department","children":[{"name":"2009","class":"Year","size":3,"color":"2009","label":null},{"name":"2010","class":"Year","size":3,"color":"2010","label":null},{"name":"2013","class":"Year","size":5,"color":"2013","label":null},{"name":"2014","class":"Year","size":1,"color":"2014","label":null},{"name":"2011","class":"Year","size":6,"color":"2011","label":null},{"name":"2012","class":"Year","size":2,"color":"2012","label":null}]},{"name":"David H. Koch Institute for Integrative Cancer Research at MIT","class":"Department","children":[{"name":"2013","class":"Year","size":2,"color":"2013","label":null},{"name":"2014","class":"Year","size":1,"color":"2014","label":null}]},{"name":"Department of Aeronautics and Astronautics","class":"Department","children":[{"name":"2014","class":"Year","size":1,"color":"2014","label":null}]},{"name":"Department of Biological Engineering","class":"Department","children":[{"name":"2011","class":"Year","size":1,"color":"2011","label":null},{"name":"2013","class":"Year","size":3,"color":"2013","label":null},{"name":"2014","class":"Year","size":3,"color":"2014","label":null}]},{"name":"Department of Biology","class":"Department","children":[{"name":"2012","class":"Year","size":1,"color":"2012","label":null},{"name":"2013","class":"Year","size":1,"color":"2013","label":null}]},{"name":"Department of Chemical Engineering","class":"Department","children":[{"name":"2009","class":"Year","size":1,"color":"2009","label":null},{"name":"2012","class":"Year","size":1,"color":"2012","label":null}]},{"name":"Department of Civil and Environmental Engineering","class":"Department","children":[{"name":"2011","class":"Year","size":1,"color":"2011","label":null},{"name":"2012","class":"Year","size":1,"color":"2012","label":null},{"name":"2014","class":"Year","size":1,"color":"2014","label":null},{"name":"2009","class":"Year","size":1,"color":"2009","label":null},{"name":"2010","class":"Year","size":2,"color":"2010","label":null}]},{"name":"Department of Earth, Atmospheric, and Planetary Sciences","class":"Department","children":[{"name":"2012","class":"Year","size":1,"color":"2012","label":null}]},{"name":"Department of Electrical Engineering and Computer Science","class":"Department","children":[{"name":"2009","class":"Year","size":4,"color":"2009","label":null},{"name":"2012","class":"Year","size":2,"color":"2012","label":null},{"name":"2013","class":"Year","size":3,"color":"2013","label":null},{"name":"2014","class":"Year","size":2,"color":"2014","label":null},{"name":"2010","class":"Year","size":3,"color":"2010","label":null},{"name":"2011","class":"Year","size":5,"color":"2011","label":null}]},{"name":"Department of Materials Science and Engineering","class":"Department","children":[{"name":"2009","class":"Year","size":1,"color":"2009","label":null},{"name":"2010","class":"Year","size":2,"color":"2010","label":null},{"name":"2012","class":"Year","size":1,"color":"2012","label":null},{"name":"2013","class":"Year","size":1,"color":"2013","label":null},{"name":"2014","class":"Year","size":1,"color":"2014","label":null}]},{"name":"Department of Mathematics","class":"Department","children":[{"name":"2009","class":"Year","size":1,"color":"2009","label":null},{"name":"2012","class":"Year","size":1,"color":"2012","label":null},{"name":"2013","class":"Year","size":2,"color":"2013","label":null},{"name":"2010","class":"Year","size":1,"color":"2010","label":null},{"name":"2011","class":"Year","size":2,"color":"2011","label":null}]},{"name":"Department of Mechanical Engineering","class":"Department","children":[{"name":"2010","class":"Year","size":3,"color":"2010","label":null},{"name":"2011","class":"Year","size":2,"color":"2011","label":null},{"name":"2012","class":"Year","size":2,"color":"2012","label":null},{"name":"2014","class":"Year","size":2,"color":"2014","label":null}]},{"name":"Harvard University--MIT Division of Health Sciences and Technology","class":"Department","children":[{"name":"2012","class":"Year","size":1,"color":"2012","label":null},{"name":"2013","class":"Year","size":1,"color":"2013","label":null},{"name":"2014","class":"Year","size":1,"color":"2014","label":null}]},{"name":"Institute for Medical Engineering and Science","class":"Department","children":[{"name":"2014","class":"Year","size":1,"color":"2014","label":null}]},{"name":"Institute for Soldier Nanotechnologies","class":"Department","children":[{"name":"2009","class":"Year","size":1,"color":"2009","label":null}]},{"name":"Media Laboratory","class":"Department","children":[{"name":"2009","class":"Year","size":1,"color":"2009","label":null},{"name":"2013","class":"Year","size":2,"color":"2013","label":null},{"name":"2014","class":"Year","size":1,"color":"2014","label":null}]},{"name":"Microsystems Technology Laboratories","class":"Department","children":[{"name":"2014","class":"Year","size":1,"color":"2014","label":null}]},{"name":"Program in Media Arts and Sciences (Massachusetts Institute of Technology)","class":"Department","children":[{"name":"2013","class":"Year","size":1,"color":"2013","label":null},{"name":"2014","class":"Year","size":1,"color":"2014","label":null}]},{"name":"School of Architecture and Planning","class":"Department","children":[{"name":"2012","class":"Year","size":1,"color":"2012","label":null}]},{"name":"School of Engineering","class":"Department","children":[{"name":"2011","class":"Year","size":1,"color":"2011","label":null},{"name":"2013","class":"Year","size":1,"color":"2013","label":null}]}]}
<!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 = 960 - margin.left - margin.right,
height = 500 - 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("flare.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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.