D3 Force Layout Graph - Self linking node

前端 未结 1 1606
青春惊慌失措
青春惊慌失措 2020-12-03 21:30

Using a force directed graph, how do you get a link to actually show up when the target and source are the same node. So basically just a nice little loop indicating that s

相关标签:
1条回答
  • 2020-12-03 22:15

    The trick is to draw the self link as a path with an arc in it. It took me a bit of fiddling with the arc parameter syntax to get things working and the key seemed to be that the arc could not start and end at the same point. Here is the relevant code that draws the edges at each update.

    function tick() {
      link.attr("d", function(d) {
      var x1 = d.source.x,
          y1 = d.source.y,
          x2 = d.target.x,
          y2 = d.target.y,
          dx = x2 - x1,
          dy = y2 - y1,
          dr = Math.sqrt(dx * dx + dy * dy),
    
          // Defaults for normal edge.
          drx = dr,
          dry = dr,
          xRotation = 0, // degrees
          largeArc = 0, // 1 or 0
          sweep = 1; // 1 or 0
    
          // Self edge.
          if ( x1 === x2 && y1 === y2 ) {
            // Fiddle with this angle to get loop oriented.
            xRotation = -45;
    
            // Needs to be 1.
            largeArc = 1;
    
            // Change sweep to change orientation of loop. 
            //sweep = 0;
    
            // Make drx and dry different to get an ellipse
            // instead of a circle.
            drx = 30;
            dry = 20;
    
            // For whatever reason the arc collapses to a point if the beginning
            // and ending points of the arc are the same, so kludge it.
            x2 = x2 + 1;
            y2 = y2 + 1;
          } 
    
     return "M" + x1 + "," + y1 + "A" + drx + "," + dry + " " + xRotation + "," + largeArc + "," + sweep + " " + x2 + "," + y2;
    });
    

    And here is a jsfiddle that demonstrates the whole thing, and a screenshot:

    enter image description here

    0 讨论(0)
提交回复
热议问题