简体   繁体   English

D3 Bubble示例:bubble.nodes()有什么作用?

[英]D3 Bubble Example: What does bubble.nodes() do?

Example: http://mbostock.github.com/d3/ex/bubble.html 示例: http//mbostock.github.com/d3/ex/bubble.html

在此输入图像描述

I'm having a hard time understanding what goes on with line 16: 我很难理解第16行发生的事情:

.data(bubble.nodes(classes(json))

And why, or where within the classes() function, the variable classes[] gets x,y,r values defined for each of its objects. 为什么,或者在classes()函数中,变量classes []获取为其每个对象定义的x,y,r值。 Also, bubble.nodes() doesn't appear to be an actual function? 另外,bubble.nodes()似乎不是一个实际的函数?

If I add a 如果我添加一个

console.log(classes)

between lines 44 and 45 - every object inside seems to be populated with x,y,r already -- but it is not apparent why this happens. 第44行和第45行之间 - 内部的每个对象似乎都已经填充了x,y,r - 但是为什么会发生这种情况并不明显。

The call to bubble.nodes() boils down to a call to d3.layout.pack().nodes() since bubble=d3.layout.pack() . bubble.nodes()的调用归结为对d3.layout.pack().nodes()的调用,因为bubble=d3.layout.pack() The trick is that pack.nodes() is hardcoded to use the value key of the input's children (in this case all the packages) to size the nodes (add r ) and determine position (add x and y ). 诀窍是pack.nodes()被硬编码以使用输入children节点的value键(在本例中为所有包)来调整节点的大小(添加r )并确定位置(添加xy )。

In essence, 在本质上,

 var root = {"children": [
              {"packageName":"cluster","className":"AgglomerativeCluster","value":3938},
              {"packageName":"cluster","className":"CommunityStructure","value":3812},
              {"packageName":"cluster","className":"HierarchicalCluster","value":6714},
              {"packageName":"cluster","className":"MergeEdge","value":743}
         ]}; // This is an excerpt of the real data.

 var bubble = d3.layout.pack(); 

 // pack.nodes() assigns each element of "children" a r, x, and y based on value
 bubble.nodes(root); 

This tripped me up at first as well, you can see that classes() doesn't add r , x , and y since classes(root) doesn't have those attributes. 这也让我最初绊倒了,你可以看到classes()不会添加rxy因为classes(root)没有这些属性。 krasnaya's answer touched on most of this but I felt that it needed a bit more explaining (at least it did for me). krasnaya的回答触及了大部分内容,但我觉得需要更多解释(至少它对我有用)。

The classes() function doesn't add the attributes.. it just flattens the tree. classes()函数不添加属性..它只是使树变平。 The attributes are added inside bubble.nodes() (which is d3.layout.pack().nodes()) 这些属性添加在bubble.nodes()中(即d3.layout.pack()。nodes())

JSON.stringify(classes[0])
"{"packageName":"cluster","className":"AgglomerativeCluster","value":3938}"

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

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