位置力
🌐 Position forces
x 和 y 方向的力将节点推向给定维度上的目标位置,其强度可配置。radial 力类似,但它将节点推向给定圆上最近的点。力的强度与节点位置与目标位置之间的一维距离成正比。虽然这些力可以用于定位单个节点,但它们主要用于作用于所有(或大多数)节点的全局力。
🌐 The x- and y-position forces push nodes towards a desired position along the given dimension with a configurable strength. The radial force is similar, except it pushes nodes towards the closest point on a given circle. The strength of the force is proportional to the one-dimensional distance between the node’s position and the target position. While these forces can be used to position individual nodes, they are intended primarily for global forces that apply to all (or most) nodes.
forceX(x)
来源 · 在 x 轴上创建一个指向给定位置 x 的新位置力。如果未指定 x,则默认为 0。
const x = d3.forceX(width / 2);x.strength(strength)
来源 · 如果指定了 strength,则将强度访问器设置为指定的数字或函数,重新评估每个节点的强度访问器,并返回该力。strength 决定了节点 x 速度的增量:(x - node.x) × strength。例如,值为 0.1 表示节点在每次应用时应从当前 x 位置移动到目标 x 位置的十分之一。较高的值会使节点更快地移动到目标位置,但通常会以牺牲其他力或约束为代价。不建议使用范围外的值 [0,1]。
如果未指定strength,则返回当前的强度访问器,默认值为:
🌐 If strength is not specified, returns the current strength accessor, which defaults to:
function strength() {
return 0.1;
}对于模拟中的每个node,都会调用强度访问器,并传递node及其从零开始的index。所得的数字随后会被内部存储,这样每个节点的强度只有在力被初始化或当此方法以新的strength调用时才会重新计算,而不是在每次施加力的时候都计算。
🌐 The strength accessor is invoked for each node in the simulation, being passed the node and its zero-based index. The resulting number is then stored internally, such that the strength of each node is only recomputed when the force is initialized or when this method is called with a new strength, and not on every application of the force.
x.x(x)
来源 · 如果指定了 x,则将 x 坐标访问器设置为指定的数字或函数,重新评估每个节点的 x 访问器,并返回此力。如果未指定 x,则返回当前的 x 访问器,默认值为:
function x() {
return 0;
}x 访问器会针对模拟中的每个 节点 调用,并传入 node 及其零起点 index。所得的数值随后会被内部存储,从而每个节点的目标 x 坐标只有在力初始化或调用此方法并传入新的 x 时才会重新计算,而不是在每次应用该力时都计算。
🌐 The x-accessor is invoked for each node in the simulation, being passed the node and its zero-based index. The resulting number is then stored internally, such that the target x-coordinate of each node is only recomputed when the force is initialized or when this method is called with a new x, and not on every application of the force.
forceY(y)
来源 · 在 y 轴上创建一个指向给定位置 y 的新位置力。如果未指定 y,则默认为 0。
const y = d3.forceY(height / 2);y.strength(strength)
来源 · 如果指定了 strength,则将强度访问器设置为指定的数字或函数,重新评估每个节点的强度访问器,并返回该力。 strength 决定了节点 y 速度的增量:(y - node.y) × strength。例如,值为 0.1 表示每次应用时,节点应从当前 y 位置移动到目标 y 位置的十分之一。较高的值会使节点更快地移动到目标位置,但通常会以牺牲其他力或约束为代价。不建议使用范围外的值 [0,1]。
如果未指定strength,则返回当前的强度访问器,默认值为:
🌐 If strength is not specified, returns the current strength accessor, which defaults to:
function strength() {
return 0.1;
}对于模拟中的每个node,都会调用强度访问器,并传递node及其从零开始的index。所得的数字随后会被内部存储,这样每个节点的强度只有在力被初始化或当此方法以新的strength调用时才会重新计算,而不是在每次施加力的时候都计算。
🌐 The strength accessor is invoked for each node in the simulation, being passed the node and its zero-based index. The resulting number is then stored internally, such that the strength of each node is only recomputed when the force is initialized or when this method is called with a new strength, and not on every application of the force.
y.y(y)
来源 · 如果指定了 y,则将 y 坐标访问器设置为指定的数字或函数,重新评估每个节点的 y 访问器,并返回此力。如果未指定 y,则返回当前的 y 访问器,默认值为:
function y() {
return 0;
}y 访问器会在模拟中的每个 节点 上被调用,并传入 node 及其从零开始的 index。生成的数字随后会被内部存储,因此每个节点的目标 y 坐标只有在力被初始化或当此方法以新的 y 调用时才会重新计算,而不会在每次应用力时重新计算。
🌐 The y-accessor is invoked for each node in the simulation, being passed the node and its zero-based index. The resulting number is then stored internally, such that the target y coordinate of each node is only recomputed when the force is initialized or when this method is called with a new y, and not on every application of the force.
forceRadial(radius, x, y)
来源 · 创建一个新的位置力,指向以 ⟨x,y⟩ 为中心、指定 半径 的圆。如果未指定 x 和 y,它们默认为 ⟨0,0⟩。
const radial = d3.forceRadial(r, width / 2, height / 2);radial.strength(strength)
来源 · 如果指定了strength,则将强度访问器设置为指定的数值或函数,重新评估每个节点的强度访问器,并返回此力。strength 决定了增加节点 x 和 y 速度的幅度。例如,值为 0.1 表示每次应用时节点应从当前位置移动到圆上最近点的十分之一的距离。更高的值会使节点更快地移动到目标位置,但通常会以牺牲其他力或约束为代价。不推荐使用 [0,1] 范围之外的值。
如果未指定strength,则返回当前的强度访问器,默认值为:
🌐 If strength is not specified, returns the current strength accessor, which defaults to:
function strength() {
return 0.1;
}对于模拟中的每个node,都会调用强度访问器,并传递node及其从零开始的index。所得的数字随后会被内部存储,这样每个节点的强度只有在力被初始化或当此方法以新的strength调用时才会重新计算,而不是在每次施加力的时候都计算。
🌐 The strength accessor is invoked for each node in the simulation, being passed the node and its zero-based index. The resulting number is then stored internally, such that the strength of each node is only recomputed when the force is initialized or when this method is called with a new strength, and not on every application of the force.
径向.半径(radius)
🌐 radial.radius(radius)
来源 · 如果指定了 radius,则将圆的 radius 设置为指定的数字或函数,重新评估每个节点的 radius 访问器,并返回此力。如果未指定 radius,则返回当前的 radius 访问器。
在模拟中,每个 节点 都会调用 radius 访问器,并传入 node 及其从零开始的 index。生成的数值随后被内部存储,因此每个节点的目标半径仅在力被初始化时或在使用新的 radius 调用此方法时才重新计算,而不会在每次施加力时重新计算。
🌐 The radius accessor is invoked for each node in the simulation, being passed the node and its zero-based index. The resulting number is then stored internally, such that the target radius of each node is only recomputed when the force is initialized or when this method is called with a new radius, and not on every application of the force.
radial.x(x)
来源 · 如果指定了 x,则将圆心的 x 坐标设置为指定的数值,并返回此力。如果未指定 x,则返回圆心当前的 x 坐标,默认为零。
radial.y(y)
来源 · 如果指定了 y,则将圆心的 y 坐标设置为指定的数字,并返回此力。如果未指定 y,则返回当前圆心的 y 坐标,默认值为零。