如何在强制布局D3.js中生成右侧链接

时间:2015-03-19 13:29:42

标签: javascript svg d3.js

如何在力布局D3.js中向右侧生成链接弧角,如图所示:

desired output

enter image description here

我尝试了以下内容:

path.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,

    if (x1 === x2) {

        dr = Math.sqrt(dx * dy + dy * dy);
        return "M" +
            d.source.x + "," +
            d.source.y + "A" +
            dr + "," + dr + " 0 0,1 " +
            d.target.x + "," +
            d.target.y;

    }
});

但似乎左边有些,右边有些。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

您始终将sweep-flag设置为1,因此它将从(x1,y1)顺时针绘制到(x2,y2)。下降时右侧,上升时右侧。你可以通过检查方向是向上还是向下来规范化,然后交换点的顺序或反转扫描方向:

if (x1 === x2) {
    var dr = Math.sqrt(dx * dy + dy * dy); // note that this is always equal to Math.abs(dy)
    var sweep = 0;
    if (y1 > y2) {
        sweep = 1
    }
    return "M" +
        d.source.x + "," d.source.y +
        "A" + dr + "," + dr +
        " 0 0," + sweep + " " +
        d.target.x + "," + d.target.y;
}
相关问题