位置力
¥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。
¥Source · Creates a new position force along the x-axis towards the given position x. If x is not specified, it defaults to 0.
const x = d3.forceX(width / 2);
x.strength(strength) {#x_strength}
源代码 · 如果指定了 strength,则将 strength 访问器设置为指定的数字或函数,重新评估每个节点的 strength 访问器,并返回此力。强度决定了节点 x 方向速度的增量:(x - node.x) × strength.例如,值为 0.1 表示节点在每次应用中应从其当前 x 位置移动到目标 x 位置的十分之一。更高的值会更快地将节点移动到目标位置,这通常会以牺牲其他力或约束。不建议使用 [0,1] 范围之外的值。
¥Source · If strength is specified, sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each node, and returns this force. The strength determines how much to increment the node’s x-velocity: (x - node.x) × strength. For example, a value of 0.1 indicates that the node should move a tenth of the way from its current x-position to the target x-position with each application. Higher values moves nodes more quickly to the target position, often at the expense of other forces or constraints. A value outside the range [0,1] is not recommended.
如果未指定强度,则返回当前强度访问器,默认为:
¥If strength is not specified, returns the current strength accessor, which defaults to:
function strength() {
return 0.1;
}
在模拟中,每个 node 都会调用强度访问器,并传递节点及其从零开始的索引。然后将结果数字存储在内部,以便仅在初始化力或使用新的力强度调用此方法时重新计算每个节点的强度,而不是每次施加力时都重新计算。
¥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 访问器,然后返回力。如果未指定 x,则返回当前的 x 访问器,默认为:
¥Source · If x is specified, sets the x-coordinate accessor to the specified number or function, re-evaluates the x-accessor for each node, and returns this force. If x is not specified, returns the current x-accessor, which defaults to:
function x() {
return 0;
}
在模拟过程中,每个 node 都会调用 x 访问器,并传递节点及其从零开始的索引。然后将结果数字存储在内部,以便仅在初始化力或使用新的 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。
¥Source · Creates a new position force along the y-axis towards the given position y. If y is not specified, it defaults to 0.
const y = d3.forceY(height / 2);
y.strength(strength) {#y_strength}
源代码 · 如果指定了 strength,则将 strength 访问器设置为指定的数字或函数,重新评估每个节点的 strength 访问器,并返回此力。强度决定了节点 y 方向速度的增量:(y - node.y) × strength.例如,值为 0.1 表示节点在每次应用中应从其当前 y 位置移动到目标 y 位置的十分之一。更高的值会更快地将节点移动到目标位置,这通常会以牺牲其他力或约束。不建议使用 [0,1] 范围之外的值。
¥Source · If strength is specified, sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each node, and returns this force. The strength determines how much to increment the node’s y-velocity: (y - node.y) × strength. For example, a value of 0.1 indicates that the node should move a tenth of the way from its current y-position to the target y-position with each application. Higher values moves nodes more quickly to the target position, often at the expense of other forces or constraints. A value outside the range [0,1] is not recommended.
如果未指定强度,则返回当前强度访问器,默认为:
¥If strength is not specified, returns the current strength accessor, which defaults to:
function strength() {
return 0.1;
}
在模拟中,每个 node 都会调用强度访问器,并传递节点及其从零开始的索引。然后将结果数字存储在内部,以便仅在初始化力或使用新的力强度调用此方法时重新计算每个节点的强度,而不是每次施加力时都重新计算。
¥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 访问器,并返回此力。如果未指定 y,则返回当前的 y 访问器,默认为:
¥Source · If y is specified, sets the y-coordinate accessor to the specified number or function, re-evaluates the y-accessor for each node, and returns this force. If y is not specified, returns the current y-accessor, which defaults to:
function y() {
return 0;
}
模拟中每个 node 都会调用 y 访问器,并传递节点及其从零开始的索引。然后将结果数字存储在内部,以便仅在初始化力或使用新的 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⟩ 为圆心,指定 radius 的圆的新位置力。如果未指定 x 和 y,则默认为 ⟨0,0⟩。
¥Source · Creates a new position force towards a circle of the specified radius centered at ⟨x,y⟩. If x and y are not specified, they default to ⟨0,0⟩.
const radial = d3.forceRadial(r, width / 2, height / 2);
radial.strength(strength) {#radial_strength}
源代码 · 如果指定了 strength,则将 strength 访问器设置为指定的数字或函数,重新评估每个节点的 strength 访问器,并返回此力。强度决定了节点 x 和 y 方向速度的增量。例如,值为 0.1 表示节点在每次应用中应从其当前位置移动到圆上最近点的十分之一。更高的值会更快地将节点移动到目标位置,这通常会以牺牲其他力或约束。不建议使用 [0,1] 范围之外的值。
¥Source · If strength is specified, sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each node, and returns this force. The strength determines how much to increment the node’s x- and y-velocity. For example, a value of 0.1 indicates that the node should move a tenth of the way from its current position to the closest point on the circle with each application. Higher values moves nodes more quickly to the target position, often at the expense of other forces or constraints. A value outside the range [0,1] is not recommended.
如果未指定强度,则返回当前强度访问器,默认为:
¥If strength is not specified, returns the current strength accessor, which defaults to:
function strength() {
return 0.1;
}
在模拟中,每个 node 都会调用强度访问器,并传递节点及其从零开始的索引。然后将结果数字存储在内部,以便仅在初始化力或使用新的力强度调用此方法时重新计算每个节点的强度,而不是每次施加力时都重新计算。
¥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.
radial.radius(radius) {#radial_radius}
源代码 · 如果指定了半径,则将圆半径设置为指定的数字或函数,重新计算每个节点的半径访问器,并返回此力。如果未指定半径,则返回当前半径访问器。
¥Source · If radius is specified, sets the circle radius to the specified number or function, re-evaluates the radius accessor for each node, and returns this force. If radius is not specified, returns the current radius accessor.
在模拟过程中,每个 node 都会调用半径访问器,并传递节点及其从零开始的索引。然后将结果数字存储在内部,以便仅在初始化力或使用新的半径调用此方法时重新计算每个节点的目标半径,而不是每次施加力时都重新计算。
¥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) {#radial_x}
源代码 · 如果指定了 x,则将圆心的 x 坐标设置为指定的数字并返回此力。如果未指定 x,则返回中心的当前 x 坐标,默认为 0。
¥Source · If x is specified, sets the x-coordinate of the circle center to the specified number and returns this force. If x is not specified, returns the current x-coordinate of the center, which defaults to zero.
radial.y(y) {#radial_y}
源代码 · 如果指定了 y,则将圆心的 y 坐标设置为指定的数字,并返回此力。如果未指定 y,则返回中心的当前 y 坐标,默认为零。
¥Source · If y is specified, sets the y coordinate of the circle center to the specified number and returns this force. If y is not specified, returns the current y coordinate of the center, which defaults to zero.