嗨,我是 D3 的新手,我找不到任何具有路径障碍的 d3 力布局示例。我在想这样的事情。
圆节点的沙漏
像这样但不规则的边界,而不是一个盒子。 https://bl.ocks.org/mbostock/1129492
我不明白为什么这是不可能的,示例中使用的代码:
function tick() {
node.attr("cx", function(d) { return d.x = Math.max(radius, Math.min(width - radius, d.x)); })
.attr("cy", function(d) { return d.y = Math.max(radius, Math.min(height - radius, d.y)); });
只需设置节点中心(CX 和 CY(的最大值和最小值。因此,如果可以编写表示要创建的边的形状的公式,则应该能够包含该形状中的节点。
困难可能在于镜像 svg 另一侧的形状。
像这样:
function tick() {
node.attr("cx", function(d) { return function(d){
d.x = Math.max(radius, Math.min(d3.select(this).attr("cy")- radius, d.x)); }}
.attr("cy", function(d) { return d.y = Math.max(radius, Math.min(height - radius, d.y)))
}
我认为这应该在右侧设置一条 1:1 渐变的对角线,但显然没有经过测试,它可能会大大减慢力。