I am referring to example shown there .
我无法理解 d3.js 如何从节点创建链接。让我解释一下我的理解: 我们有一个 js 对象从代表 json 的文件中读取的根;我们递归地获取节点并给出 id。 但随后发生了我不明白的部分。 d3.layout.tree().links 函数如何返回链接?我们只传递节点给函数,也就是节点的 ID 和坐标?
d3.json("graph.json", function(error, json) {
root = json;
update();
});
function update() {
var nodes = flatten(root),
links = d3.layout.tree().links(nodes);
最佳答案
树形布局通过采用分层数据结构(在 JSON 中指定)并使链接显式来构造链接。 JSON 中的节点有一个 children
属性,该属性隐式指定连接了哪些节点。节点连接到它们的子节点,而这些节点又连接到它们的子节点和父节点。树布局使用此信息来计算链接。
Given the specified array of nodes, such as those returned by nodes, returns an array of objects representing the links from parent to child for each node. Leaf nodes will not have any links. Each link is an object with two attributes:
source - the parent node (as described above).
target - the child node.
This method is useful for retrieving a set of link descriptions suitable for display.
请注意,使用 children
属性来存储子项只是默认设置,您可以根据需要进行更改。
关于javascript - D3.js 从节点构建链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21771899/