Tree Diagram Visualization
.node rect {
stroke: #3d3d3d;
stroke-width: 3px;
}
.link {
fill: none;
stroke: #3d3d3d;
stroke-width: 2px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.5.0/d3.min.js"></script>
const data = {
"name": "Top Level",
"children": [
{
"name": "Level 2: A",
"children": [
{
"name": "Son of A"
},
{
"name": "Son of A"
},
{
"name": "Daughter of A",
"children": [
{
"name": "Grand Son of A"
}
]
},
{
"name": "Daughter of A"
}
]
},
{
"name": "Level 2: B",
"children": [
{
"name": "Son of B"
}
]
}
]
}
function diagonal(s, d) {
const path = `M ${d.x},
${d.y} C ${d.x},
${(d.y + s.y) / 2} ${s.x},
${(d.y + s.y) / 2} ${s.x},
${s.y}`
return path
}
// Toggle children on click.
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
function mousedown () {
d3.event.stopImmediatePropagation();
}
const margin = {top: 20, right: 120, bottom: 20, left: 120},
width = 1260 - margin.right - margin.left,
height = 800 - margin.top - margin.bottom;
const duration = 750,
rectWidth = 30;
let i = 0,
root;
const tree = d3.tree().size([height, width]);
const svg = d3.select("body").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.call(d3.zoom().scaleExtent([.4, 2]).on("zoom", function () {
svg.attr("transform", d3.event.transform)
}))
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
root = d3.hierarchy(data, function (d) {
return d.children;
});
root.x0 = height / 2;
root.y0 = 0;
function collapse(d) {
if (d.children) {
d._children = d.children;
d._children.forEach(collapse);
d.children = null;
}
}
root.children.forEach(collapse);
update(root);
function update(source) {
const treeData = tree(root);
// Compute the new tree layout.
let nodes = treeData.descendants(),
links = treeData.descendants().slice(1);
// Normalize for fixed-depth.
nodes.forEach(function(d) { d.y = d.depth * 100; });
// Update the nodes…
let node = svg.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); });
// Enter any new nodes at the parent's previous position.
let nodeEnter = node.enter().append("g")
.attr("cursor", function(d) {
return d._children || d.children ? 'pointer' : null;
})
.attr("class", "node")
.attr("transform", function() { return "translate(" + source.x0 + "," + source.y0 + ")"; })
.on("click", click)
.on("mousedown", mousedown);
nodeEnter.append("rect")
.attr("width", rectWidth)
.attr("height", rectWidth)
.attr("x", -rectWidth/2)
.attr("y", -rectWidth/2)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
let nodeUpdate = nodeEnter.merge(node);
// Transition nodes to their new position.
nodeUpdate.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
nodeUpdate.select("rect")
.attr("width", rectWidth)
.attr("height", rectWidth)
.attr("x", -rectWidth/2)
.attr("y", -rectWidth/2)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
// Transition exiting nodes to the parent's new position.
let nodeExit = node.exit().transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + source.x + "," + source.y + ")"; })
.remove();
nodeExit.select("rect")
.attr("width", 0)
.attr("height", 0)
.attr("x", 0)
.attr("y", 0);
// Update the links…
let link = svg.selectAll("path.link")
.data(links, function(d) { return d.id; });
// Enter any new links at the parent's previous position.
const linkEnter = link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", function(d) {
const o = {x: source.x0, y: source.y0};
return diagonal(o, o);
});
let linkUpdate = linkEnter.merge(link);
// Transition links to their new position.
linkUpdate.transition()
.duration(duration)
.attr("d", function(d){ return diagonal(d, d.parent)});
// Transition exiting nodes to the parent's new position.
link.exit().transition()
.duration(duration)
.attr("d", function(d) {
const o = {x: source.x, y: source.y};
return diagonal(o, o);
})
.remove();
// Stash the old positions for transition.
nodes.forEach(function(d) {
d.x0 = d.x;
d.y0 = d.y;
});
}