javascript - D3.js 从节点构建链接?

标签 javascript d3.js

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 属性,该属性隐式指定连接了哪些节点。节点连接到它们的子节点,而这些节点又连接到它们的子节点和父节点。树布局使用此信息来计算链接。

documentation说:

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/

相关文章:

javascript - CSS 颜色叠加

javascript - 通过 property 或 setAttribute 设置属性

javascript - d3.js 中的缩放 donut 大小

javascript - 在 d3.js 中更改家谱中的家庭成员颜色?

svg - 如何在 SVG 中居中 90º 旋转文本

javascript - D3圆包: html in text attribute

javascript - D3 JS 根据条件导入不同的 csv 文件

javascript - 如何为基于 Web 的应用程序启用直接打印功能?

javascript - 访问未发送表单的值以用于remote_function

javascript - Eval 代码看起来很可疑,但没有它我的 WordPress slider 就无法工作