简体   繁体   English

D3.js从节点构造链接?

[英]D3.js constructing links from nodes?

I am referring to example shown there . 我指的是那里显示的示例

I can not understand how d3.js creates links from nodes. 我不明白d3.js如何从节点创建链接。 Let me explain what I understand: We have a root that js object read from file representing json; 让我解释一下我的理解:我们有一个js对象从表示json的文件中读取的根; We get nodes recursively and give ids. 我们递归获取节点并给出id。 But then occurs part that I dont understand. 但是随后发生了我不理解的部分。 How d3.layout.tree().links function returns links? d3.layout.tree()。​​links函数如何返回链接? We only pass nodes to function, that is just ids, and coordinates of nodes? 我们只将节点传递给函数,就是id和节点的坐标?

    d3.json("graph.json", function(error, json) {
    root = json;
    update();
    });
    function update() {
    var nodes = flatten(root), 
    links = d3.layout.tree().links(nodes);

The tree layout constructs the links by taking a hierarchical data structure (specified in the JSON) and making the links explicit. 树形布局通过采用分层数据结构(在JSON中指定)并明确显示链接来构造链接。 The nodes in the JSON have a children attribute that specifies, implicitly, which nodes are connected. JSON中的节点具有children属性,该属性隐式指定要连接的节点。 Nodes are connected to their children and those are, in turn, connected to their children and to their parent. 节点连接到其子节点,而节点又连接到其子节点和父节点。 It is this information that the tree layout uses to compute the links. 树布局使用此信息来计算链接。

The documentation says: 文件说:

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). source-父节点(如上所述)。

target - the child node. target-子节点。

This method is useful for retrieving a set of link descriptions suitable for display. 此方法对于检索适合于显示的一组链接描述很有用。

Note that using the children attribute to store the children is merely the default that you can change if you like. 请注意,使用children属性存储子项仅仅是您可以更改的默认值。

In the update() function, links = d3.layout.tree().links(nodes); update()函数中, links = d3.layout.tree().links(nodes); is 'selecting' the nodes in the tree and their specified links in the JSON file. 正在“选择”树中的节点及其在JSON文件中的指定链接。

I think the key here is that in the JSON file, there should be not only the nodes but also the links specified . 我认为关键是在JSON文件中,不仅应该有节点,还应该有指定的链接 Otherwise, one would get merely the nodes. 否则,将仅获得节点。 I believe that this page should help as well. 我相信此页面也应该有所帮助。

As for what's going on later in the code, if it's helpful, please read on... 至于代码后面的内容,如果有帮助,请继续阅读...

Later on, link = link.data(links, function(d) { return d.target.id; }); 稍后, link = link.data(links, function(d) { return d.target.id; }); updates existing links to the tree. 更新到树的现有链接。

And later on, this block of code below enters new links not currently present in the visualization. 然后,下面的代码块将输入可视化中当前不存在的新链接。

// Enter any new links.
  link.enter().insert("line", ".node")
      .attr("class", "link")
      .attr("x1", function(d) { return d.source.x; })
      .attr("y1", function(d) { return d.source.y; })
      .attr("x2", function(d) { return d.target.x; })
      .attr("y2", function(d) { return d.target.y; });

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM