Created
August 6, 2012 07:43
-
-
Save hemulin/3271974 to your computer and use it in GitHub Desktop.
d3 treemap, adding parents, code in progress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script type="text/javascript" src="http://d3js.org/d3.v2.js"></script> | |
<script type="text/javascript" src="https://github.com/mbostock/d3/blob/master/src/layout/treemap.js"></script> | |
<script type="text/javascript" src="https://github.com/mbostock/d3/blob/master/src/layout/hierarchy.js"></script> | |
<style type="text/css"> | |
rect { | |
fill: none; | |
stroke: #fff; | |
} | |
rect:hover{ | |
opacity: 0.5; | |
} | |
text { | |
font-family:"Times New Roman",Times,serif; | |
font-size: 12px; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="body"></body> | |
<script type="text/javascript"> | |
var pathJson = { | |
"name": "Sample data", | |
"children": [ | |
{ | |
"name": "Title 1", "size":1, | |
"children": [ | |
{ | |
"name": "Title 1-1", "size":1, | |
"children": [ | |
{"name": "1-1-1", "size": 1}, | |
{"name": "1-1-2", "size": 1}, | |
{"name": "1-1-3", "size": 1}, | |
{"name": "1-1-4", "size": 1} | |
] | |
}, | |
{ | |
"name": "Title 1-2", "size":1, | |
"children": [ | |
{"name": "1-2-1", "size": 1}, | |
{"name": "1-2-2", "size": 1}, | |
{"name": "1-2-3", "size": 1} | |
] | |
}, | |
{ | |
"name": "Title 1-3", "size":1, | |
"children": [ | |
{"name": "1-3-1", "size": 1} | |
] | |
} | |
] | |
} | |
] | |
} | |
var w = 1280 - 80, | |
h = 800 - 180, | |
x = d3.scale.linear().range([0, w]), | |
y = d3.scale.linear().range([0, h]), | |
color = d3.scale.category10(), | |
root, | |
node; | |
var treemap = d3.layout.treemap() | |
.round(false) | |
.size([w, h]) | |
.sticky(false) | |
.padding([10, 0, 0, 0]) | |
.value(function(d) { return d.size; }); | |
var svg = d3.select("#body").append("div") | |
.attr("class", "chart") | |
.style("width", w + "px") | |
.style("height", h + "px") | |
.append("svg:svg") | |
.attr("width", w) | |
.attr("height", h) | |
.append("svg:g") | |
.attr("transform", "translate(.5,.5)"); | |
node = root = pathJson; | |
var nodes = treemap.nodes(root); | |
var children = nodes.filter(function(d) { return !d.children; }); | |
var parents = nodes.filter(function(d) { return d.children; }); | |
var childCell = svg.selectAll("g") | |
.data(children) | |
.enter().append("svg:g") | |
.attr("class", "childCell") | |
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }) | |
.on("click", function(d) { return zoom(node == d.parent ? root : d.parent); }); | |
childCell.append("svg:rect") | |
.attr("width", function(d) { return d.dx - 1; }) | |
.attr("height", function(d) { return d.dy - 1; }) | |
.style("fill", function(d) { return color(d.parent.name); }); | |
childCell.append("svg: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.name; }) | |
.style("opacity", function(d) { d.w = this.getComputedTextLength(); return d.dx > d.w ? 1 : 0; }); | |
var parentCell = svg.selectAll("g") | |
.data(parents) | |
.enter() | |
.append("svg:g") | |
.attr("class","parentCell") | |
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }); | |
parentCell.append("svg:rect") | |
.attr("width", function(d) { /*CHECK PARENT NAME*/ alert(d.name); return d.dx - 1; }) | |
.attr("height", function(d) { return d.dy - 1; }) | |
.style("fill", "grey"); | |
d3.select(window).on("click", function() { zoom(root); }); | |
d3.select("select").on("change", function() { | |
treemap.value(this.value == "size" ? size : count).nodes(root); | |
zoom(node); | |
}); | |
function size(d) { | |
return d.size; | |
} | |
function count(d) { | |
return 1; | |
} | |
function zoom(d) { | |
//alert(d.name); | |
var kx = w / d.dx, ky = h / d.dy; | |
x.domain([d.x, d.x + d.dx]); | |
y.domain([d.y, d.y + d.dy]); | |
var t = svg.selectAll("g.ChildCell").transition() | |
.duration(d3.event.altKey ? 7500 : 750) | |
.attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; }); | |
t.select("rect") | |
.attr("width", function(d) { return kx * d.dx - 1; }) | |
.attr("height", function(d) { return ky * d.dy - 1; }) | |
t.select("text") | |
.attr("x", function(d) { return kx * d.dx / 2; }) | |
.attr("y", function(d) { return ky * d.dy / 2; }) | |
.style("opacity", function(d) { return kx * d.dx > d.w ? 1 : 0; }); | |
//.style("font-size", function(d) { return kx * d.dx > d.w ? "20px" : "12px";}); | |
node = d; | |
d3.event.stopPropagation(); | |
} | |
</script> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment