Skip to content

中心力

¥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⟩。

¥Source · Creates a new center force with the specified x- and y- coordinates. If x and y are not specified, they default to ⟨0,0⟩.

js
const center = d3.forceCenter(width / 2, height / 2);

center.x(x) {#center_x}

源代码 · 如果指定了 x,则将中心位置的 x 坐标设置为指定的数字,并返回力。如果未指定 x,则返回当前的 x 坐标,默认为 0。

¥Source · If x is specified, sets the x-coordinate of the centering position to the specified number and returns this force. If x is not specified, returns the current x-coordinate, which defaults to zero.

center.y(y) {#center_y}

源代码 · 如果指定了 y,则将圆心的 y 坐标设置为指定的数字,并返回此力。如果未指定 y,则返回当前 y 坐标,默认为零。

¥Source · If y is specified, sets the y coordinate of the centering position to the specified number and returns this force. If y is not specified, returns the current y coordinate, which defaults to zero.

center.strength(strength) {#center_strength}

示例 · 源代码 · 如果指定了强度,则设置中心力的强度。降低强度(例如 0.05)可以柔化交互式图形中新节点进入或退出图形的移动。如果未指定力量,则返回力量的当前强度,默认为 1。

¥Examples · Source · If strength is specified, sets the center force’s strength. A reduced strength of e.g. 0.05 softens the movements on interactive graphs in which new nodes enter or exit the graph. If strength is not specified, returns the force’s current strength, which defaults to 1.