gpt4 book ai didi

javascript - D3 力图 - 固定不重叠的节点

转载 作者:行者123 更新时间:2023-11-28 20:12:30 27 4
gpt4 key购买 nike

我正在寻求开发一个由节点链接图组成的可视化。我有一系列点,我不想更改其位置,除非图表上发生碰撞(一个节点与另一个节点)。如果节点发生碰撞,我想将它们隔开,这样它们就不会重叠。我的JS代码如下

var chartWidth = 200;
var chartHeight = 200;
var widthPadding = 40;
var heightPadding = 40;

var link, node;

$(function(){
initialize();
});


function initialize() {
var jsonString = '{"nodes":[{"x":40,"y":64,"r":6,"fixed":true},{"x":40,"y":63,"r":6,"fixed":true},{"x":119,"y":53,"r":6,"fixed":true},{"x":119,"y":73,"r":6,"fixed":true},{"x":137,"y":73,"r":6,"fixed":true},{"x":140,"y":140,"r":6,"fixed":true},{"x":68,"y":57,"r":6,"fixed":true},{"x":70,"y":75,"r":6,"fixed":true},{"x":51,"y":59,"r":6,"fixed":true},{"x":51,"y":54,"r":6,"fixed":true},{"x":137,"y":40,"r":6,"fixed":true}],"links":[{"source":0,"target":1},{"source":1,"target":2},{"source":2,"target":3},{"source":3,"target":4},{"source":4,"target":5},{"source":0,"target":1},{"source":1,"target":6},{"source":6,"target":7},{"source":7,"target":4},{"source":4,"target":5},{"source":0,"target":1},{"source":1,"target":8},{"source":8,"target":9},{"source":9,"target":10},{"source":10,"target":5}]}';
drawForceDirectedNodeLink($.parseJSON(jsonString));
}


function drawForceDirectedNodeLink(graph){
var width = chartWidth + (2*widthPadding);
var height = chartHeight + (2*heightPadding);

var q = d3.geom.quadtree(graph.nodes),
i = 0,
n = graph.nodes.length;

while (++i < n) {
q.visit(collide(graph.nodes[i]));
}

var force = d3.layout.force()
.size([width, height])
.gravity(0.05)
.on("tick", function(){
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; });

node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("r", function(d) { return d.r; });
});

var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);

var link = svg.selectAll(".link"),
node = svg.selectAll(".node");

force
.nodes(graph.nodes)
.links(graph.links)
.start();

link = link.data(graph.links)
.enter().append("line")
.attr("class", "link");

node = node.data(graph.nodes)
.enter().append("circle")
.attr("class", "node");
}


function collide(node) {
var r = node.radius + 16,
nx1 = node.x - r,
nx2 = node.x + r,
ny1 = node.y - r,
ny2 = node.y + r;
return function(quad, x1, y1, x2, y2) {
if (quad.point && (quad.point !== node)) {
var x = node.x - quad.point.x,
y = node.y - quad.point.y,
l = Math.sqrt(x * x + y * y),
r = node.radius + quad.point.radius;
if (l < r) {
l = (l - r) / l * .5;
node.x -= x *= l;
node.y -= y *= l;
quad.point.x += x;
quad.point.y += y;
}
}
return x1 > nx2
|| x2 < nx1
|| y1 > ny2
|| y2 < ny1;
};
}

正如你所看到的,我尝试实现提到的碰撞检测逻辑 here 。但有些原因我无法让这部分工作。

Also attaching the output that I've managed until now

最佳答案

请注意,在 initialize() 内的 jsonString 声明中,每个节点都被赋予一个 r 属性。但是,在 collide() 中,您将执行以下操作:

.attr("r", function(d) { return d.radius - 2; })

确保您的节点附加有radius 属性。如果没有,请进行以下更改:

.attr("r", function(d) { return d.r - 2; })

您可以在 Mike Bostock 脚本的第 30 行看到,他的节点最初是使用 radius 属性声明的,而不是您的 r 属性。

var nodes = d3.range(200).map(function() { return {radius: Math.random() * 12 + 4}; }),

关于javascript - D3 力图 - 固定不重叠的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19672293/

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