【发布时间】:2019-10-16 01:10:35
【问题描述】:
我有一个强制布局图,用户可以在其中动态添加节点。我怎样才能使我的所有节点在中间有一点距离,并使它们独立移动而不是围绕中心移动。
我已尝试删除 d3.forceCenter(width / 2, height / 2),它使节点独立移动,但随后它将所有节点定位在 (0, 0)。
simulation = d3.forceSimulation()
.force('charge', d3.forceManyBody().strength(0))
.force('center', d3.forceCenter(width / 2, height / 2));
我希望所有节点都居中并独立移动。
编辑:
我尝试设置 cx 和 cy 值,但这也不起作用。
const nodeEnter = nodeElements
.enter()
.append('circle')
.attr('r', 20)
.attr('fill', 'orange')
.attr('cx', (d, i) => {
return (width / 2) + i * 10;
})
.attr('cy', (d, i) => {
return (height / 2) + i * 10;
})
.call(dragDrop(simulation))
.on('click', ({ id }) => handleClick(id));
【问题讨论】:
-
为什么不能同时使用这两种力量?独立是什么意思?
-
@thedude 当 forceCenter 存在时,所有节点都放置在 (width / 2, height / 2) 周围。如果我移动 1 个节点,那么所有其他节点都会相对移动,以将质心保持在同一位置。我不想要那种行为。我希望所有节点都放在中心。但是当我移动 1 个节点时,其余节点应该留在原地。
标签: d3.js centering force-layout