Built with blockbuilder.org
Created
January 31, 2017 17:35
-
-
Save markvital/117e3088c7e0dbd6a9b89ad69982c996 to your computer and use it in GitHub Desktop.
D3 Horizontal Tree Layout (Reingold-Tilford )
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
license: mit |
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 type="text/javascript" src="//code.jquery.com/jquery-1.8.3.js"></script> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<style> | |
body{ | |
background:#aae; | |
} | |
.node { | |
cursor: pointer; | |
} | |
.node circle { | |
/*fill: #fff; | |
stroke: steelblue;*/ | |
stroke-width: 3px; | |
} | |
.node text { | |
font: 12px sans-serif; | |
fill: #fff; | |
} | |
.link { | |
fill: none; | |
stroke: #ccc; | |
stroke-width: 2px; | |
} | |
.tree { | |
margin-bottom: 10px; | |
overflow: auto; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="tree"> | |
</div> | |
<script> | |
$(document).ready(function () { | |
//build tree | |
function BuildVerticaLTree(treeData, treeContainerDom) { | |
var margin = { top: 40, right: 120, bottom: 20, left: 120 }; | |
var width = 960 - margin.right - margin.left; | |
var height = 500 - margin.top - margin.bottom; | |
var i = 0, duration = 750; | |
var tree = d3.layout.tree() | |
.size([height, width]); | |
var diagonal = d3.svg.diagonal() | |
.projection(function (d) { return [d.x, d.y]; }); | |
var svg = d3.select(treeContainerDom).append("svg") | |
.attr("width", width + margin.right + margin.left) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
root = treeData; | |
update(root); | |
function update(source) { | |
// Compute the new tree layout. | |
var nodes = tree.nodes(root).reverse(), | |
links = tree.links(nodes); | |
// Normalize for fixed-depth. | |
nodes.forEach(function (d) { d.y = d.depth * 100; }); | |
// Declare the nodes… | |
var node = svg.selectAll("g.node") | |
.data(nodes, function (d) { return d.id || (d.id = ++i); }); | |
// Enter the nodes. | |
var nodeEnter = node.enter().append("g") | |
.attr("class", "node") | |
.attr("transform", function (d) { | |
return "translate(" + source.x0 + "," + source.y0 + ")"; | |
}).on("click", nodeclick); | |
nodeEnter.append("circle") | |
.attr("r", 10) | |
.attr("stroke", function (d) { return d.children || d._children ? "steelblue" : "#00c13f"; }) | |
.style("fill", function (d) { return d.children || d._children ? "lightsteelblue" : "#fff"; }); | |
//.attr("r", 10) | |
//.style("fill", "#fff"); | |
nodeEnter.append("text") | |
.attr("y", function (d) { | |
return d.children || d._children ? 18 : 18; | |
}) | |
.attr("dy", ".35em") | |
.attr("text-anchor", "middle") | |
.text(function (d) { return d.name; }) | |
.style("fill-opacity", 1e-6); | |
// Transition nodes to their new position. | |
//horizontal tree | |
var nodeUpdate = node.transition() | |
.duration(duration) | |
.attr("transform", function (d) { return "translate(" + d.x + "," + d.y + ")"; }); | |
nodeUpdate.select("circle") | |
.attr("r", 10) | |
.style("fill", function (d) { return d._children ? "lightsteelblue" : "#fff"; }); | |
nodeUpdate.select("text") | |
.style("fill-opacity", 1); | |
// Transition exiting nodes to the parent's new position. | |
var nodeExit = node.exit().transition() | |
.duration(duration) | |
.attr("transform", function (d) { return "translate(" + source.x + "," + source.y + ")"; }) | |
.remove(); | |
nodeExit.select("circle") | |
.attr("r", 1e-6); | |
nodeExit.select("text") | |
.style("fill-opacity", 1e-6); | |
// Update the links… | |
// Declare the links… | |
var link = svg.selectAll("path.link") | |
.data(links, function (d) { return d.target.id; }); | |
// Enter the links. | |
link.enter().insert("path", "g") | |
.attr("class", "link") | |
.attr("d", function (d) { | |
var o = { x: source.x0, y: source.y0 }; | |
return diagonal({ source: o, target: o }); | |
}); | |
// Transition links to their new position. | |
link.transition() | |
.duration(duration) | |
.attr("d", diagonal); | |
// Transition exiting nodes to the parent's new position. | |
link.exit().transition() | |
.duration(duration) | |
.attr("d", function (d) { | |
var o = { x: source.x, y: source.y }; | |
return diagonal({ source: o, target: o }); | |
}) | |
.remove(); | |
// Stash the old positions for transition. | |
nodes.forEach(function (d) { | |
d.x0 = d.x; | |
d.y0 = d.y; | |
}); | |
} | |
// Toggle children on click. | |
function nodeclick(d) { | |
if (d.children) { | |
d._children = d.children; | |
d.children = null; | |
} else { | |
d.children = d._children; | |
d._children = null; | |
} | |
update(d); | |
} | |
} | |
var treeData = | |
{ | |
"name": "BU Head", | |
"children": [ | |
{ | |
"name": "Manager", | |
"children": [ | |
{ | |
"name": "TL A", | |
"children": [ | |
{"name": "Dev 1"}, | |
{"name": "Dev 2"}, | |
{"name": "Dev 3"} | |
] | |
}, | |
{ | |
"name": "TL B", | |
"children": [] | |
} | |
] | |
}, | |
{ | |
"name": "Manager 2", | |
"children": [ | |
{ | |
"name": "Team Lead C", | |
"children": [ | |
{"name": "Dev 1"}, | |
{"name": "Dev 2"}, | |
{"name": "Dev 3"} | |
] | |
}, | |
{ | |
"name": "Team Lead D", | |
"children": [ | |
{"name": "Dev 1"}, | |
{"name": "Dev 2"}, | |
] | |
} | |
] | |
} | |
] | |
}; | |
BuildVerticaLTree(treeData, "#tree"); | |
}); $(document).ready(function () { | |
//build tree | |
function BuildVerticaLTree(treeData, treeContainerDom) { | |
var margin = { top: 40, right: 120, bottom: 20, left: 120 }; | |
var width = 960 - margin.right - margin.left; | |
var height = 500 - margin.top - margin.bottom; | |
var i = 0, duration = 750; | |
var tree = d3.layout.tree() | |
.size([height, width]); | |
var diagonal = d3.svg.diagonal() | |
.projection(function (d) { return [d.x, d.y]; }); | |
var svg = d3.select(treeContainerDom).append("svg") | |
.attr("width", width + margin.right + margin.left) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
root = treeData; | |
update(root); | |
function update(source) { | |
// Compute the new tree layout. | |
var nodes = tree.nodes(root).reverse(), | |
links = tree.links(nodes); | |
// Normalize for fixed-depth. | |
nodes.forEach(function (d) { d.y = d.depth * 100; }); | |
// Declare the nodes… | |
var node = svg.selectAll("g.node") | |
.data(nodes, function (d) { return d.id || (d.id = ++i); }); | |
// Enter the nodes. | |
var nodeEnter = node.enter().append("g") | |
.attr("class", "node") | |
.attr("transform", function (d) { | |
return "translate(" + source.x0 + "," + source.y0 + ")"; | |
}).on("click", nodeclick); | |
nodeEnter.append("circle") | |
.attr("r", 10) | |
.attr("stroke", function (d) { return d.children || d._children ? "steelblue" : "#00c13f"; }) | |
.style("fill", function (d) { return d.children || d._children ? "lightsteelblue" : "#fff"; }); | |
//.attr("r", 10) | |
//.style("fill", "#fff"); | |
nodeEnter.append("text") | |
.attr("y", function (d) { | |
return d.children || d._children ? -18 : 18; | |
}) | |
.attr("dy", ".35em") | |
.attr("text-anchor", "middle") | |
.text(function (d) { return d.name; }) | |
.style("fill-opacity", 1e-6); | |
// Transition nodes to their new position. | |
//horizontal tree | |
var nodeUpdate = node.transition() | |
.duration(duration) | |
.attr("transform", function (d) { return "translate(" + d.x + "," + d.y + ")"; }); | |
nodeUpdate.select("circle") | |
.attr("r", 10) | |
.style("fill", function (d) { return d._children ? "lightsteelblue" : "#fff"; }); | |
nodeUpdate.select("text") | |
.style("fill-opacity", 1); | |
// Transition exiting nodes to the parent's new position. | |
var nodeExit = node.exit().transition() | |
.duration(duration) | |
.attr("transform", function (d) { return "translate(" + source.x + "," + source.y + ")"; }) | |
.remove(); | |
nodeExit.select("circle") | |
.attr("r", 1e-6); | |
nodeExit.select("text") | |
.style("fill-opacity", 1e-6); | |
// Update the links… | |
// Declare the links… | |
var link = svg.selectAll("path.link") | |
.data(links, function (d) { return d.target.id; }); | |
// Enter the links. | |
link.enter().insert("path", "g") | |
.attr("class", "link") | |
.attr("d", function (d) { | |
var o = { x: source.x0, y: source.y0 }; | |
return diagonal({ source: o, target: o }); | |
}); | |
// Transition links to their new position. | |
link.transition() | |
.duration(duration) | |
.attr("d", diagonal); | |
// Transition exiting nodes to the parent's new position. | |
link.exit().transition() | |
.duration(duration) | |
.attr("d", function (d) { | |
var o = { x: source.x, y: source.y }; | |
return diagonal({ source: o, target: o }); | |
}) | |
.remove(); | |
// Stash the old positions for transition. | |
nodes.forEach(function (d) { | |
d.x0 = d.x; | |
d.y0 = d.y; | |
}); | |
} | |
// Toggle children on click. | |
function nodeclick(d) { | |
if (d.children) { | |
d._children = d.children; | |
d.children = null; | |
} else { | |
d.children = d._children; | |
d._children = null; | |
} | |
update(d); | |
} | |
} | |
var treeData = | |
{ | |
"name": "BU Head", | |
"children": [ | |
{ | |
"name": "Manager", | |
"children": [ | |
{ | |
"name": "Team Lead", | |
"children": [] | |
}, | |
{ | |
"name": "Team Lead", | |
"children": [] | |
} | |
] | |
}, | |
{ | |
"name": "Manager", | |
"children": [] | |
} | |
] | |
}; | |
BuildVerticaLTree(treeData, "#tree"); | |
}); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment