Built with blockbuilder.org
Last active
July 10, 2016 00:21
-
-
Save lindep/d603cf55d457ce1725872b67fdb8cd8b to your computer and use it in GitHub Desktop.
treemap_hierarchy
This file contains 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
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="//d3js.org/d3.v4.min.js"></script> | |
<style> | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
.node circle { | |
fill: #999; | |
} | |
.node text { | |
font: 10px sans-serif; | |
} | |
.node--internal circle { | |
fill: #555; | |
} | |
.node--internal text { | |
text-shadow: 0 1px 0 #fff, 0 -1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff; | |
} | |
.link { | |
fill: none; | |
stroke: #555; | |
stroke-opacity: 0.4; | |
stroke-width: 1.5px; | |
} | |
</style> | |
</head> | |
<body> | |
<svg width="450" height="208"></svg> | |
<script> | |
var treeSite ={ | |
"name": "007-106", | |
"children": [ | |
{"name": "007-001"}, | |
{"name": "007-002", | |
"children": [ | |
{"name": "006-001"}, | |
{"name": "006-002"} | |
] | |
}, | |
{"name": "007-003", | |
"children": [ | |
{"name": "004-001"}, | |
{"name": "004-002"} | |
] | |
}, | |
{"name": "007-004", | |
"children": [ | |
{"name": "005-001" } | |
] | |
}, | |
{"name": "007-005" } | |
] | |
}; | |
var svg = d3.select("body").select("svg"), | |
width = +svg.attr("width"), | |
height = +svg.attr("height"), | |
g = svg.append("g").attr("transform", "translate(100,0)"), | |
area = g.append("rect"); | |
var treemap = d3.tree() | |
.size([height, width - 237]); | |
var nodes = d3.hierarchy(treeSite, function(d) { | |
return d.children; | |
}); | |
nodes = treemap(nodes); | |
var link = g.selectAll(".link") | |
.data( nodes.descendants().slice(1)) | |
.enter().append("path") | |
.attr("class", "link") | |
.attr("d", function(d) { | |
return "M" + d.y + "," + d.x | |
+ "C" + (d.y + d.parent.y) / 2 + "," + d.x | |
+ " " + (d.y + d.parent.y) / 2 + "," + d.parent.x | |
+ " " + d.parent.y + "," + d.parent.x; | |
}); | |
var node = g.selectAll(".node") | |
.data(nodes.descendants()) | |
.enter().append("g") | |
.attr("class", function(d) { return "node" + (d.children ? " node--internal" : " node--leaf"); }) | |
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; }); | |
node.append("circle") | |
.attr("r", 2.5); | |
node.append("text") | |
.attr("dy", 3) | |
.attr("x", function(d) { return d.children ? -8 : 8; }) | |
.style("text-anchor", function(d) { return d.children ? "end" : "start"; }) | |
.text(function(d) { return d.data.name; }); | |
console.log("you are now rocking with d3", width,height); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment