在使用 d3-force 碰撞后,我试图获取圆的坐标(cx,cy),但我仍然得到原始位置。有什么方法可以获取圆的平移坐标(碰撞检查后)?
以下只是代码摘要。
let data = [
{ 'x': 100, 'y': 100, 'radius': 10, cluster: 4 },
{ 'x': 100, 'y': 100, 'radius': 6, cluster: 3 },
{ 'x': 50, 'y': 200, 'radius': 10, cluster: 2 },
{ 'x': 350, 'y': 200, 'radius': 10, cluster: 2 },
{ 'x': 100, 'y': 300, 'radius': 20, cluster: 3 },
{ 'x': 100, 'y': 300, 'radius': 25, cluster: 1 },
{ 'x': 100, 'y': 300, 'radius': 33, cluster: 2 }];
var collisionForce = d3.forceCollide((d) => d.radius).strength(1).iterations(100);
var simulation = d3.forceSimulation(data).alphaDecay(0.01).force('collisionForce', collisionForce)
.force('center', d3.forceCenter(width / 2, height / 2));
var node = svg.selectAll('circle').data(data)
.enter().append('circle')
.attr('r', function (d) {
return d.radius;
}).attr('cx', function (d) {
return d.x;
}).attr('cy', function (d) {
return d.y;
})
.attr('cluster', d => d.cluster)
.attr('fill', d => that.palette[d.cluster]);
function ticked() {
node.attr('cx', function (d) {
return d.x;
})
.attr('cy', function (d) {
return d.y;
});
}
simulation.on('tick', ticked);