d3.jsd3tree

d3 custom curve direction between 2 links - "parent to child" when parent is behind the child


I have some nodes and links and am drawing a graph, I successfully draw the following graph, enter image description here

I want to change the connections from G to D and from E to C like the second screenshot with the red lines.

enter image description here

some code

// the diagonal link generator
diagonal = d3
    .linkHorizontal()
    .x(d => d.x)
    .y(d => d.y);

// and its usage (generating the d parameter)
...
.attr("d", d3.linkHorizontal().x(d => d.x).y(d => d.y));
...

I tried to implement this using the .linkRadial(), but without any luck, all I could manage is make the curvature bigger

Edit: Managed to achieve this almost using the .linkVertical() for those links instead, but now I need the curves to be bigger so its more obvious. Reading the documentation I cant find any way to edit this..


Solution

  • I managed to solve this drawing a custom link using the d3.path()

    drawLinkCurve = (source: { x; y }, target: { x; y }) => {
        const context = d3.path();
        context.moveTo(source.x, source.y);
        context.bezierCurveTo(source.x - xAxisOffset, source.y - yAxisOffset, target.x + xAxisOffset, target.y + yAxisOffset, target.x, target.y);
        return context + '';
    };
    

    what this does, is create points at source.x - xAxisOffset, source.y - yAxisOffset, and at target.x + xAxisOffset, target.y + yAxisOffset which then draws the curve based on these points. (imagine the points having gravity)