中心力
🌐 Center force
中心力以均匀方式平移节点,使得所有节点的平均位置(如果所有节点权重相等,则为质心)位于给定位置 ⟨x,y⟩。每次施加此力时,它都会修改节点的位置;它不会修改速度,因为这样通常会导致节点超出目标中心并围绕其振荡。此力有助于将节点保持在视口中心,并且与位置力不同,它不会扭曲节点的相对位置。
🌐 The center force translates nodes uniformly so that the mean position of all nodes (the center of mass if all nodes have equal weight) is at the given position ⟨x,y⟩. This force modifies the positions of nodes on each application; it does not modify velocities, as doing so would typically cause the nodes to overshoot and oscillate around the desired center. This force helps keep nodes in the center of the viewport, and unlike the position forces, it does not distort their relative positions.
forceCenter(x, y)
来源 · 使用指定的 x- 和 y- 坐标创建一个新的中心力。如果未指定 x 和 y,它们默认为 ⟨0,0⟩。
const center = d3.forceCenter(width / 2, height / 2);center.x(x)
来源 · 如果指定了 x,将居中位置的 x 坐标设置为指定的数字,并返回该力。如果未指定 x,则返回当前的 x 坐标,默认值为零。
center.y(y)
来源 · 如果指定了 y,将居中位置的 y 坐标设置为指定的数值并返回该力。如果未指定 y,则返回当前的 y 坐标,默认值为零。
center.strength(strength)
示例 · 来源 · 如果指定了 strength,则设置中心力的强度。例如,将强度降低到 0.05 会使在交互式图表中新节点进入或退出图表时的运动变得更柔和。如果未指定 strength,则返回力的当前强度,默认为 1。