// useful links
// https://bl.ocks.org/mbostock/3184089
// https://bl.ocks.org/d3noob/72f43406bbe9e104e957f44713b8413c
var treeDataL = {
"name": "Root",
"children": [
{
"name": "L_A_1",
"children": [
{"name": "L_B_1"},
{"name": "L_B_2"}
]
},
{
"name": "L_A_2",
"children": [
{"name": "L_B_3"},
{"name": "L_B_4"}
]
}
]
}
var treeDataR = {
"name": "Root",
"children": [
{
"name": "R_A_1",
"children": [
{"name": "R_B_1"},
{"name": "R_B_2"},
{"name": "R_B_3"}
]
},
{
"name": "R_A_2",
"children": [
{"name": "R_B_3"},
{"name": "R_B_4"},
{"name": "R_B_5"},
{"name": "R_B_6"}
]
}
]
}
// set the dimensions and margins of the diagram
var margin = {top: 50, right: 50, bottom: 50, left: 50},
width = 400 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// declares a tree layout and assigns the size
var tree = d3.tree()
.size([width, height]);
// create 2x trees using d3 hierarchy
// this is where the computation of coordinates takes place
var nodesL = tree(d3.hierarchy(treeDataL));
var nodesR = tree(d3.hierarchy(treeDataR));
// get arrays of nodes - need v6
nodesLArray = Array.from(nodesL);
nodesRArray = Array.from(nodesR);
// switch out nodesR root for nodesL
// here the choice is to assign coords of root of LH tree to RH
nodesLRoot = nodesLArray.find(n => n.data.name == "Root");
nodesRRoot = nodesRArray.find(n => n.data.name == "Root");
nodesRRoot.x = nodesLRoot.x;
nodesRRoot.y = nodesLRoot.y;
// this is kinda like the 'important bit'
// REMEMBER for horizontal layout, flip x and y...
// LH: halve and negate all y's in nodesL add width / 2
nodesLArray.forEach(n => n.y = ((n.y * 0.5) * -1) + width / 2);
// RH: halve and add width / 2 to all y's nodesR
nodesRArray.forEach(n => n.y = (n.y * 0.5) + width / 2);
// now sticking a bit more closely to the tutorial in link 3
// append svg
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
// align g with margin
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// render both trees
[nodesL, nodesR].forEach(function(nodes, i) {
// adds the links between the nodes
// need to select links based on index to prevent bad rendering
var link = g.selectAll(`links${i}`)
.data( nodes.descendants().slice(1))
.enter()
.append("path")
.attr("class", `link links${i}`) // note two classes
.attr("d", function(d) {
// x and y flipped here to achieve horizontal placement
return `M${d.y},${d.x}C${d.y},${(d.x + d.parent.x) / 2} ${d.parent.y},${(d.x + d.parent.x) / 2} ${d.parent.y},${d.parent.x}`
});
// adds each node as a group
// need to select nodes based on index to prevent bad rendering
var node = g.selectAll(`.nodes${i}`)
.data(nodes.descendants())
.enter()
.append("g")
.attr("class", `node nodes${i}`) // note two classes
.attr("transform", function(d) {
// x and y flipped here to achieve horizontal placement
return `translate(${d.y},${d.x})`;
});
// adds the circle to the node
node.append("circle")
.attr("r", 10);
// adds the text to the node
node.append("text")
.attr("dy", ".35em")
.attr("y", function(d) { return d.children ? -20 : 20; })
.style("text-anchor", "middle")
.text(function(d) { return d.data.name; });
});
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 3px;
}
.node text {
font: 10px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
<script src="https://d3js.org/d3.v6.min.js"></script>