The transition between links is not working for me in my d3 tree diagram it is a vertical tree with rectangular nodes so i edited attr which shapes the link to fit my diagram
original code:
var link = svg.selectAll('path.link')
.data(links, function(d) { return d.id; });
// Enter any new links at the parent's previous position.
var linkEnter = link.enter().insert('path', "g")
.attr("class", "link")
.attr('d', function(d){
var o = {x: source.x0, y: source.y0}
return diagonal(o, o)
});
// UPDATE
var linkUpdate = linkEnter.merge(link);
// Transition back to the parent element position
linkUpdate.transition()
.duration(duration)
.attr('d', function(d){ return diagonal(d, d.parent) });
// Remove any exiting links
var linkExit = link.exit().transition()
.duration(duration)
.attr('d', function(d) {
var o = {x: source.x, y: source.y}
return diagonal(o, o)
})
.remove();
function diagonal(s, d) {
path = `M ${s.y} ${s.x}
C ${(s.y + d.y) / 2} ${s.x},
${(s.y + d.y) / 2} ${d.x},
${d.y} ${d.x}`
return path
}
Updated code with a manipulated diagonal that is directly added as an attr:
// Update the links...
var link = svg.selectAll('path.link')
.data(links, function(d) { return d.id; });
// Enter any new links at the parent's previous position.
var linkEnter = link.enter().insert('path', "g")
.attr("class", "link")
.attr("d", function(d) {
return "M" + (d.x + rectW / 2) + "," + (d.y + rectH / 2)
+ "C" + (d.x + rectW / 2) + "," + (d.y + d.parent.y) / 2
+ " " + (d.parent.x + rectW / 2) + "," + (d.y + d.parent.y) / 2
+ " " + (d.parent.x + rectW / 2) + "," + (d.parent.y + rectH / 2);
});
// UPDATE
var linkUpdate = linkEnter.merge(link);
// Transition back to the parent element position
linkUpdate.transition()
.duration(0)
.attr("d", function(d) {
return "M" + (d.x + rectW / 2) + "," + (d.y + rectH / 2)
+ "C" + (d.x + rectW / 2) + "," + (d.y + d.parent.y) / 2
+ " " + (d.parent.x + rectW / 2) + "," + (d.y + d.parent.y) / 2
+ " " + (d.parent.x + rectW / 2) + "," + (d.parent.y + rectH / 2);
})
// Remove any exiting links
var linkExit = link.exit().transition()
.duration(duration)
.attr("d", function(d) {
return "M" + (d.x + rectW / 2) + "," + (d.y + rectH / 2)
+ "C" + (d.x + rectW / 2) + "," + (d.y + d.parent.y) / 2
+ " " + (d.parent.x + rectW / 2) + "," + (d.y + d.parent.y) / 2
+ " " + (d.parent.x + rectW / 2) + "," + (d.parent.y + rectH / 2);
})
.remove();
I've noticed the attr that shapes the links in my updated code is the same for linkEnter, linkUpdateTransition and linkExit but for the original code the linkUpdateTransition is different, since I pretty much got my attr working by trail and error I'm not sure how to adjust the linkUpdateTransition attr to have a smooth transition