Curved line on d3 force directed tree

后端 未结 2 1130
逝去的感伤
逝去的感伤 2020-12-15 09:17

New to d3 and trying to develop a force directed tree into which we can plug varioss dataset. I\'ve managed to get the basic idea up and running but would like to make the l

相关标签:
2条回答
  • 2020-12-15 09:39

    This also worked for me.

    First define a path:

    var path = vis.selectAll("path")
       .data(force.links());
    
    path.enter().insert("svg:path")
       .attr("class", "link")
       .style("stroke", "#ccc");
    

    Then define the curve, as Bob Haslett says and in the Mobile Patent Suits example:

    path.attr("d", function(d) {
        var dx = d.target.x - d.source.x,
            dy = d.target.y - d.source.y,
            dr = Math.sqrt(dx * dx + dy * dy);
        return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
    });
    
    0 讨论(0)
  • 2020-12-15 09:45

    Der, worked it out.

    change

    .enter().append("line")
    

    to

    .enter().append("path")
    

    then change

    link.attr("x1", function(d) { return d.source.x; })
      .attr("y1", function(d) { return d.source.y; })
      .attr("x2", function(d) { return d.target.x; })
      .attr("y2", function(d) { return d.target.y; });
    

    to

    link.attr("d", function(d) {
    var dx = d.target.x - d.source.x,
        dy = d.target.y - d.source.y,
        dr = Math.sqrt(dx * dx + dy * dy);
    return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
    });
    

    Hope that help anyone stuck as I was

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