gpt4 book ai didi

javascript - 如何在强制布局 D3.js 中生成指向右侧的链接

转载 作者:行者123 更新时间:2023-11-29 15:35:56 25 4
gpt4 key购买 nike

如何在强制布局D3.js中生成指向右侧的链接弧 Angular ,如图所示:

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;

}
});

不过好像有的在左边,有的在右边。请帮我解决这个问题。

最佳答案

您始终将 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;
}

关于javascript - 如何在强制布局 D3.js 中生成指向右侧的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29145938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com