This is a response to the stackoverflow.com question How do I create a tree layout using JSON data in d3.v4 - without stratify().
Created
August 2, 2016 23:34
-
-
Save mattgiguere/339843b4622a965048fc9f61598f2d47 to your computer and use it in GitHub Desktop.
d3v4 JSON Tree
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> | |
<meta charset="utf-8"> | |
<style> | |
.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> | |
<svg width="500" height="300"></svg> | |
<script src="//d3js.org/d3.v4.min.js"></script> | |
<script> | |
var svg = d3.select("svg"), | |
width = +svg.attr("width"), | |
height = +svg.attr("height"), | |
g = svg.append("g").attr("transform", "translate(60,0)"); | |
var tree = d3.cluster() | |
.size([height, width - 160]); | |
var stratify = d3.stratify() | |
.parentId(function(d) { return d.id.substring(0, d.id.lastIndexOf(".")); }); | |
d3.json("treeData.json", function(error, treeData) { | |
if (error) throw error; | |
var root = d3.hierarchy(treeData); | |
tree(root); | |
var link = g.selectAll(".link") | |
.data(root.descendants().slice(1)) | |
.enter().append("path") | |
.attr("class", "link") | |
.attr("d", function(d) { | |
return "M" + d.y + "," + d.x | |
+ "C" + (d.parent.y + 100) + "," + d.x | |
+ " " + (d.parent.y + 100) + "," + d.parent.x | |
+ " " + d.parent.y + "," + d.parent.x; | |
}); | |
var node = g.selectAll(".node") | |
.data(root.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; | |
}); | |
}); | |
</script> |
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
{ | |
"name": "Top Level", | |
"parent": "null", | |
"children": [ | |
{ | |
"name": "Level 2: A", | |
"parent": "Top Level", | |
"children": [ | |
{ | |
"name": "Son of A", | |
"parent": "Level 2: A" | |
}, | |
{ | |
"name": "Daughter of A", | |
"parent": "Level 2: A" | |
} | |
] | |
}, | |
{ | |
"name": "Level 2: B", | |
"parent": "Top Level" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment