I have made a tree graph structure and it's working absolutely fine. But now I want to make a small change but unable to make that change. I want to connect the two-parent node into one child node...also I am trying to add a tooltip into each node pragmatically.
For example - if you run the code, you will see it more clearly. I wanna make a child node of Hanna and mark which will be connected to a child node named "Eric".
any idea how to achieve this?
var svg = d3
.select("body")
.append("svg")
.attr("width", 600)
.attr("height", 600)
.append("g")
.attr("transform", "translate(50,50)");
//tree data
var data = [
{ child: "John", parent: "" },
{ child: "Aron", parent: "Kevin" },
{ child: "Kevin", parent: "John" },
{ child: "Hannah", parent: "Anna" },
{ child: "Rose", parent: "Sarah" },
{ child: "Anna", parent: "John" },
{ child: "Sarah", parent: "Kevin" },
{ child: "Mark", parent: "Anna" },
{ child: "Angle", parent: "Sarah" },
];
//to construct
var dataStructure = d3
.stratify()
.id(function (d) {
return d.child;
})
.parentId(function (d) {
return d.parent;
})(data);
//to define the size of the structure tree
var treeStructure = d3.tree().size([500, 300]);
var information = treeStructure(dataStructure);
//to make the connections curves
var connections = svg.append("g").selectAll("path").data(information.links());
connections
.enter()
.append("path")
.attr("d", function (d) {
return (
"M" +
d.source.x +
"," +
d.source.y +
" C " +
d.source.x +
"," +
(d.source.y + d.target.y) / 2 +
" " +
d.target.x +
"," +
(d.source.y + d.target.y) / 2 +
" " +
d.target.x +
"," +
d.target.y
);
});
//creating the circles with data info
var circles = svg
.append("g")
.selectAll("circle")
.data(information.descendants());
//placing the circles
circles
.enter()
.append("circle")
.attr("cx", function (d) {
return d.x;
})
.attr("cy", function (d) {
return d.y;
})
.attr("r", 7)
.append("text");
//names
var names = svg.append("g").selectAll("text").data(information.descendants());
names
.enter()
.append("text")
.text(function (d) {
return d.data.child;
})
.attr("x", function (d) {
return d.x + 7;
})
.attr("y", function (d) {
return d.y + 4;
});
circle {
fill: rgb(88, 147, 0);
}
path {
fill: none;
stroke: black;
}
<script src="https://d3js.org/d3.v6.min.js"></script>