Skip to content

Instantly share code, notes, and snippets.

@devangmundhra
Forked from mundhradevang/index.html
Created May 11, 2011 00:56
Show Gist options
  • Save devangmundhra/965707 to your computer and use it in GitHub Desktop.
Save devangmundhra/965707 to your computer and use it in GitHub Desktop.
Branch treemap node with mouse click
{
"history of religion":6,
"software by domain":2,
"films by producer":1,
"contents":2,
"1970s":2,
"1990s":2,
"christian holidays":1,
"works by nationality":2,
"american soul musicians":2,
"political parties in the united states by ideology":2,
"science software":2,
"festivals":2,
"former countries by status":1,
"robotics":1,
"universities and colleges in north america":1,
"sound production technology":6
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Treemap</title>
<script type="text/javascript" src="http://d3js.org/d3.v2.min.js"></script>
<link type="text/css" rel="stylesheet" href="treemap.css"/>
</head>
<body>
<div id="chart"></div>
<script type="text/javascript" src="treemap_branch_on_click.js"></script>
</body>
</html>
.cell {
border: solid 1px white;
font-family: Georgia;
font-size: 15px;
font-weight: bolder;
color: Black;
line-height: 15px;
overflow: hidden;
position: absolute;
text-indent: 2px;
text-align: center;
vertical-align: middle;
}
var w = window.innerWidth - 1,
h = window.innerHeight - 1,
color = d3.scale.category20c();
var treemap = d3.layout.treemap()
.size([w, h])
.children(function(d) { return isNaN(d.value) ? d3.entries(d.value) : null; })
.value(function(d) { return d.value; })
.sticky(true);
var div = d3.select("#chart").append("div")
.style("position", "relative")
.style("width", w + "px")
.style("height", h + "px");
d3.json("readme.json", function(json) {
div.data(d3.entries(json)).selectAll("div")
.data(treemap)
.enter().append("div")
.attr("class", "cell")
.style("background", function(d) { return d.depth == 1 ? color(d.data.key) : null; })
.style("left", function(d) { return d.depth == 1 ? d.x + "px" : null; })
.style("top", function(d) { return d.depth == 1 ? d.y + "px" : null; })
.style("width", function(d) { return d.depth == 1 ? d.dx + "px" : null; })
.style("height", function(d) { return d.depth == 1 ? d.dy + "px" : null; })
.text(function(d){return d.depth == 1 ? d.data.key : null;})
.on('click', onClick);
});
function onClick(d,i) {
var datum = d, dep = d.depth;
div.selectAll("div.cell")
.filter(function(d){
if(d.parent)
{
return d.depth == dep+1 && d.parent == datum;
}
else
{
return d.depth == dep+1;
}
})
.text(function(d){return d.data.key})
.transition()
.duration(1000)
.style("left", function(d){return d.x + "px";})
.style("top", function(d){return d.y + "px";})
.style("width", function(d){return d.dx - 1 + "px";})
.style("height", function(d){return d.dy - 1 + "px";})
.style("background", function(d) { return color(d.data.key); })
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment