连接力
🌐 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)
来源 · 使用指定的 links 和默认参数创建一个新的链接力。如果未指定 links,则默认为空数组。
WARNING
此函数是非纯的;它可能会修改传入的 links。参见 link.links。
const link = d3.forceLink(links).id((d) => d.id);link.links(links)
来源 · 如果指定了 links,则设置与此力相关联的链接数组,重新计算每个链接的 distance 和 strength 参数,并返回此力。如果未指定 links,则返回当前的链接数组,默认值为空数组。
每个链接都是一个具有以下属性的对象:
🌐 Each link is an object with the following properties:
source- 该链接的源节点;参见 simulation.nodestarget- 该链接的目标节点;参见 simulation.nodesindex- 由此方法分配的 links 的零起始索引
为了方便,链接的源和目标属性可以使用数字或字符串标识符而不是对象引用来初始化;参见 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.
WARNING
此函数是不纯的;当链接力被初始化(或重新初始化,如节点或链接发生变化时)时,它可能会改变传入的 links。任何不是对象的 link.source 或 link.target 属性都会被替换为对应标识符的 node 对象引用。
如果指定的 links 数组被修改,例如当链接被添加到模拟中或从模拟中移除时,必须使用新的(或已更改的)数组再次调用此方法,以通知力的变化;该力不会对指定的数组进行防御性复制。
🌐 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)
来源 · 如果指定了 id,则将节点 id 访问器设置为指定的函数并返回该力。如果未指定 id,则返回当前的节点 id 访问器,默认值为数字 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)
来源 · 如果指定了 distance,则将距离访问器设置为指定的数字或函数,重新评估每个链接的距离访问器,并返回此力。如果未指定 distance,则返回当前的距离访问器,默认值为:
function distance() {
return 30;
}距离访问器会为每个 link 被调用,并传入 link 及其从零开始的 index。生成的数字随后会被内部存储,这样每个链接的距离仅在力被初始化时或在此方法被以新的 distance 调用时重新计算,而不是在每次应用力时都计算。
🌐 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)
来源 · 如果指定了 strength,则将强度访问器设置为指定的数字或函数,重新计算每个链接的强度访问器,并返回该力。如果未指定 strength,则返回当前的强度访问器,默认值为:
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及其从零开始的index。随后产生的数字会被内部存储,这样每个链接的强度只有在初始化力或在此方法被调用时传入新的strength时才会重新计算,而不会在每次应用力时重新计算。
🌐 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)
来源 · 如果指定了 iterations,则将每次应用的迭代次数设置为指定的数字,并返回该力。如果未指定 iterations,则返回当前迭代次数,默认值为 1。增加迭代次数会大大提高约束的刚性,对于如晶格等复杂结构非常有用,但也会增加计算该力的运行时间成本。