连接力
¥Link force
链接力根据所需的 链接距离 将链接的节点推到一起或分开。力的强度与链接节点的距离和目标距离之差成正比,类似于弹簧力。
¥The link force pushes linked nodes together or apart according to the desired link distance. The strength of the force is proportional to the difference between the linked nodes’ distance and the target distance, similar to a spring force.
forceLink(links)
源代码 · 使用指定的链接和默认参数创建一个新的链接力。如果未指定链接,则默认为空数组。
¥Source · Creates a new link force with the specified links and default parameters. If links is not specified, it defaults to the empty array.
警告
此函数不纯;它可能会改变传入的链接。请参阅 link.links。
¥This function is impure; it may mutate the passed-in links. See link.links.
const link = d3.forceLink(links).id((d) => d.id);
link.links(links) {#link_links}
源代码 · 如果指定了 links,则设置与此力关联的链接数组,重新计算每个链接的 distance 和 strength 参数,并返回此力。如果未指定链接,则返回当前的链接数组,默认为空数组。
¥Source · If links is specified, sets the array of links associated with this force, recomputes the distance and strength parameters for each link, and returns this force. If links is not specified, returns the current array of links, which defaults to the empty array.
每个链接都是一个具有以下属性的对象:
¥Each link is an object with the following properties:
source
- 链接的源节点;参见 simulation.nodes¥
source
- the link’s source node; see simulation.nodestarget
- 链接的目标节点;参见 simulation.nodes¥
target
- the link’s target node; see simulation.nodesindex
- 由此方法分配的从零开始的链接索引¥
index
- the zero-based index into links, assigned by this method
为方便起见,可以使用数字或字符串标识符(而非对象引用)来初始化链接的源和目标属性;参见 link.id。
¥For convenience, a link’s source and target properties may be initialized using numeric or string identifiers rather than object references; see link.id.
警告
此函数不纯;当链接力为 initialized(或重新初始化,例如节点或链接发生变化时)时,它可能会改变传入的链接。任何非对象的 link.source 或 link.target 属性都会被替换为指向具有给定标识符的相应节点的对象引用。
¥This function is impure; it may mutate the passed-in links when the link force is initialized (or re-initialized, as when the nodes or links change). Any link.source or link.target property which is not an object is replaced by an object reference to the corresponding node with the given identifier.
如果指定的链接数组被修改(例如,在模拟中添加或移除链接时),则必须使用新的(或更改后的)数组再次调用此方法,以通知更改的强制执行;强制不会对指定的数组进行防御性复制。
¥If the specified array of links is modified, such as when links are added to or removed from the simulation, this method must be called again with the new (or changed) array to notify the force of the change; the force does not make a defensive copy of the specified array.
link.id(id) {#link_id}
源代码 · 如果指定了 id,则将节点 id 访问器设置为指定的函数并返回此 force。如果未指定 id,则返回当前节点 id 访问器,默认为数字节点索引:
¥Source · If id is specified, sets the node id accessor to the specified function and returns this force. If id is not specified, returns the current node id accessor, which defaults to the numeric node.index:
function id(d) {
return d.index;
}
默认 ID 访问器允许将每个链接的源和目标指定为 nodes 数组中从零开始的索引。例如:
¥The default id accessor allows each link’s source and target to be specified as a zero-based index into the nodes array. For example:
const nodes = [
{"id": "Alice"},
{"id": "Bob"},
{"id": "Carol"}
];
const links = [
{"source": 0, "target": 1}, // Alice → Bob
{"source": 1, "target": 2} // Bob → Carol
];
现在考虑一个返回字符串的不同的 id 访问器:
¥Now consider a different id accessor that returns a string:
function id(d) {
return d.id;
}
使用此访问器,你可以使用命名的源和目标:
¥With this accessor, you can use named sources and targets:
const nodes = [
{"id": "Alice"},
{"id": "Bob"},
{"id": "Carol"}
];
const links = [
{"source": "Alice", "target": "Bob"},
{"source": "Bob", "target": "Carol"}
];
这在使用 JSON 表示图表时尤其有用,因为 JSON 不允许引用。请参阅 此示例。
¥This is particularly useful when representing graphs in JSON, as JSON does not allow references. See this example.
每当初始化力时(例如当 nodes 或 links 发生变化时),都会为每个节点调用 id 访问器,并传递节点及其从零开始的索引。
¥The id accessor is invoked for each node whenever the force is initialized, as when the nodes or links change, being passed the node and its zero-based index.
link.distance(distance) {#link_distance}
源代码 · 如果指定了距离,则将距离访问器设置为指定的数字或函数,重新评估每个链接的距离访问器,并返回此力。如果未指定距离,则返回当前距离访问器,默认为:
¥Source · If distance is specified, sets the distance accessor to the specified number or function, re-evaluates the distance accessor for each link, and returns this force. If distance is not specified, returns the current distance accessor, which defaults to:
function distance() {
return 30;
}
距离访问器会为每个 link 调用,并传递链接及其从零开始的索引。生成的数字随后存储在内部,这样,只有在初始化力或以新的距离调用此方法时,才会重新计算每个链接的距离,而不是每次施加力时都重新计算。
¥The distance accessor is invoked for each link, being passed the link and its zero-based index. The resulting number is then stored internally, such that the distance of each link is only recomputed when the force is initialized or when this method is called with a new distance, and not on every application of the force.
link.strength(strength) {#link_strength}
源代码 · 如果指定了 strength,则将 strength 访问器设置为指定的数字或函数,重新评估每个链接的 strength 访问器,并返回此力。如果未指定强度,则返回当前强度访问器,默认为:
¥Source · If strength is specified, sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each link, and returns this force. If strength is not specified, returns the current strength accessor, which defaults to:
function strength(link) {
return 1 / Math.min(count(link.source), count(link.target));
}
其中 count(node) 是一个函数,它返回以给定节点作为源或目标的链接数。之所以选择此默认值,是因为它会自动降低连接到密集连接节点的链接强度,从而提高稳定性。
¥Where count(node) is a function that returns the number of links with the given node as a source or target. This default was chosen because it automatically reduces the strength of links connected to heavily-connected nodes, improving stability.
每个 link 都会调用强度访问器,并传递链接及其从零开始的索引。生成的数字随后存储在内部,这样,只有在初始化力或以新的强度调用此方法时,才会重新计算每个链接的强度,而不是每次施加力时都重新计算。
¥The strength accessor is invoked for each link, being passed the link and its zero-based index. The resulting number is then stored internally, such that the strength of each link 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.
link.iterations(iterations) {#link_iterations}
源代码 · 如果指定了 iterations,则将每次应用的迭代次数设置为指定次数并返回此力。如果未指定迭代次数,则返回当前迭代次数,默认为 1。增加迭代次数可以大大提高约束的刚性,这对 复杂结构,例如格子 很有用,但也会增加评估力的运行时成本。
¥Source · If iterations is specified, sets the number of iterations per application to the specified number and returns this force. If iterations is not specified, returns the current iteration count which defaults to 1. Increasing the number of iterations greatly increases the rigidity of the constraint and is useful for complex structures such as lattices, but also increases the runtime cost to evaluate the force.