Skip to content

Instantly share code, notes, and snippets.

@cheneymb
Last active April 26, 2016 16:13
Show Gist options
  • Save cheneymb/54f854f6e49514647e022a15ac6c8a88 to your computer and use it in GitHub Desktop.
Save cheneymb/54f854f6e49514647e022a15ac6c8a88 to your computer and use it in GitHub Desktop.
Pedigree Tree
license: gpl-3.0

A variation on the elbow dendrogram, though using the tree layout. A couple notes: a custom separation function is used so that the distance between sets of parents is half the distance between parents themselves; the tree layout draws the leaf nodes at the right extreme of the layout, so a large right margin is used to inset the leaf nodes; the leaf nodes also have a custom path (d.target.children is null).

In response to a Stack Overflow question.

forked from mbostock's block: Pedigree Tree

<!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="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 0, right: 320, bottom: 0, 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([height, width]);
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(error, json) {
if (error) throw error;
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.y + "," + d.x + ")"; })
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 medical history")
.text(function(d) { return d.medicalhistory; });
});
function elbow(d, i) {
return "M" + d.source.y + "," + d.source.x
+ "H" + d.target.y + "V" + d.target.x
+ (d.target.children ? "" : "h" + margin.right);
}
</script>
{
"name": "Margaret Oakley Dayhoff",
"born": 1994,
"medicalhistory": "Depressive tendencies",
"parents": [
{
"name": "Kenneth Oakley",
"born": 1962,
"medicalhistory": "early-onset Parkinson's Disease",
"parents": [
{
"name": "Robert Oakley",
"born": 1925,
"medicalhistory": "Cardiac arrhythmia"
},
{
"name": "Elizabeth Shanks",
"born": 1927,
"died": 2015,
"medicalhistory": "Parkinson's Disease"
}
]
},
{
"name": "Ruth Clark",
"born": 1965,
"medicalhistory": "None",
"parents": [
{
"name": "Henry Clark",
"born": 1930,
"medicalhistory": "Bipolar disorder, lung disease"
},
{
"name": "Sarah Houchins",
"born": 1932,
"medicalhistory": "Hodgkin's Lymphoma"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment