gpt4 book ai didi

javascript - d3.j 将径向树与链接(直)树混合

转载 作者:行者123 更新时间:2023-11-30 19:14:54 29 4
gpt4 key购买 nike

关于这个例子(d3.j radial tree node links different sizes),我想知道是否可以在 d3.js 中混合径向树和直线树。

对于我的 jsFiddle 示例:https://jsfiddle.net/j0kaso/fow6xbdL/我想让父级(level0)与第一个子级(level1)有一条直线,然后是径向弯曲的树(就像现在一样)。

这可能吗?

我找不到与之相关的任何内容,但由于我对 d3.js/JS 比较陌生,所以我可能只是错过了正确的关键字。希望有人有一个可行的例子或者可以指出我正确的方向 - 无论如何我很感激任何提示和评论!

最佳答案

如果链接的源深度为 0,则可以从链接的源和目标的 x 和 y 生成 SVG 路径,类似于使用三 Angular 函数计算节点位置的方式,其中 x 是旋转 Angular ,y 是半径.

    //create the linkRadial function for use later in the 'd' generation
const radialPath = d3.linkRadial()
.angle(l => l.x)
.radius(l => l.y)

const link = svg.append("g")
.attr("fill", "none")
.attr("stroke-opacity", 0.4)
.attr("stroke-width", 1.5)
.selectAll("path")
.data(root.links())
.enter()
.append("path")

link.attr("d", function(d){

let adjust = 1.5708 //90 degrees in radians

// calculate the start and end points of the path, using trig
let sourceX = (d.source.y * Math.cos(d.source.x - adjust));
let sourceY = (d.source.y * Math.sin(d.source.x - adjust));
let targetX = (d.target.y * Math.cos(d.target.x - adjust));
let targetY = (d.target.y * Math.sin(d.target.x -adjust));


// if the source node is at the centre, depth = 0, then create a straight path using the L (lineto) SVG path. Else, use the radial path
if (d.source.depth==0){
return "M" + sourceX + " " + sourceY + " "
+ "L" + targetX + " " + targetY
} else {
return radialPath(d)
}

})

关于javascript - d3.j 将径向树与链接(直)树混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58100069/

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