The following url is going to get horizontally-oriented tree. My requirement is however to get a vertically-oriented tree using d3. Please suggest a proper valid solution for this requirement.
Asked
Active
Viewed 9,081 times
7
-
1Have you seen [this example](http://bl.ocks.org/mbostock/3184089)? – Lars Kotthoff Oct 18 '13 at 08:12
-
i saw but i need tree like pedigree tree – Saihariharan Oct 18 '13 at 08:54
2 Answers
9
I know its been a while since you asked the question, but just in case I would like to bring to your attention a diagram that I made:
The code in on codepen. If you have any questions regarding the code, please let me know.

VividD
- 10,456
- 6
- 64
- 111
-
1Please see http://stackoverflow.com/questions/31245751/how-do-you-create-a-family-tree-in-d3-js to get an extra 500 pts. Just read the comments of the question. – ChrLipp Jul 09 '15 at 06:40
-
@VividD - how would you add support for siblings? Let's say Robert/Elizabeth Shanks had another child other than James. What I'm trying to achieve @ http://stackoverflow.com/questions/34011999/how-can-i-prevent-overlapping-in-a-family-tree-based-on-familyecho – meder omuraliev Dec 02 '15 at 02:58
4
change line 35, line 56 and elbow function to
<!DOCTYPE html>
<meta charset="utf-8">
<style>
text {
font-family: "Helvetica Neue", Helvetica, sans-serif;
}
.name {
font-weight: bold;
}
.about {
fill: #777;
font-size: smaller;
}
.link {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="http://d3js.org/d3.v2.min.js?2.9.4"></script>
<script>
var margin = {top: 0, right: 0, bottom: 320, left: 0},
width = 960- margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var tree = d3.layout.tree()
.separation(function(a, b) { return a.parent === b.parent ? 1 : .5; })
.children(function(d) { return d.parents; })
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.json("tree.json", function(json) {
var nodes = tree.nodes(json);
var link = svg.selectAll(".link")
.data(tree.links(nodes))
.enter().append("path")
.attr("class", "link")
.attr("d", elbow);
var node = svg.selectAll(".node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
node.append("text")
.attr("class", "name")
.attr("x", 8)
.attr("y", -6)
.text(function(d) { return d.name; });
node.append("text")
.attr("x", 8)
.attr("y", 8)
.attr("dy", ".71em")
.attr("class", "about lifespan")
.text(function(d) { return d.born + "–" + d.died; });
node.append("text")
.attr("x", 8)
.attr("y", 8)
.attr("dy", "1.86em")
.attr("class", "about location")
.text(function(d) { return d.location; });
});
function elbow(d, i) {
console.log(d)
return "M" + d.source.x + "," + d.source.y
+ "V" + d.target.y + "H" + d.target.x
+ (d.target.children ? "" : ("v" + margin.bottom))
}
</script>
</body>
this my result

mqshen
- 501
- 3
- 11
-
About halfway down the page is a good explanation of how to do this... http://www.d3noob.org/2014/01/tree-diagrams-in-d3js_11.html – user1009073 Nov 09 '17 at 15:28