简体   繁体   English

了解d3.js强制布局的更新过程

[英]Understanding the update process of d3.js force layout

For quiet a bit of time now I'm trying to update my d3.js force layout graph. 为了安静一点时间,我正在尝试更新我的d3.js力布局图。 Sadly every hint I can find leads to this example . 可悲的是,我能找到的每一个提示都会导致这个例子 I don't fully understand it and now have questions to different parts of this process. 我不完全理解它,现在对这个过程的不同部分有疑问。

setTimeout(function() {
  var a = {id: "a"}, b = {id: "b"}, c = {id: "c"};
  nodes.push(a, b, c);
  links.push({source: a, target: b}, {source: a, target: c}, {source: b, target: c});
  start();
}, 0);

Assuming the setTimeout function is only there to provide some action to happen with some seconds inbetween: 假设setTimeout函数只是在那里提供一些动作,中间有几秒钟:

Q 1: Could I basically remove the setTimeout() and write it as function initializeGraph(){/*do stuff*/} ? 问1:我可以基本上删除setTimeout()并将其写为function initializeGraph(){/*do stuff*/}吗?
Q 2: nodes.push() and links.push() do only push these elements to the node & link array but don't make any changes in the graph yet? 问题2: nodes.push()links.push()只将这些元素推送到nodelink数组,但是图中没有进行任何更改吗?

1) yes question 1 you can do it that way 1)是问题1你可以这样做

working code here 这里工作代码

2) D3 mean data driven document. 2)D3意味着数据驱动文档。 So when you change the data the DOM get updated. 因此,当您更改数据时,DOM会更新。 Thus the reason why you are updating nodes which hold the node data. 因此,为什么要更新的原因nodes持有该节点的数据。 The node( var node = svg.selectAll(".node"), ) is holding the node related DOM. 节点( var node = svg.selectAll(".node"), )保存与node相关的DOM。

Q1. Q1。 You could, but it wouldn't happen at a set time, the example you link to is set up to do 3 changes to the data and hence the layout at 3-second intervals just so it's apparent what's happening to anyone watching the demo 您可以,但它不会在设定的时间发生,您链接到的示例设置为对数据进行3次更改,因此布局以3秒的间隔进行,这样看起来很明显发生在观看演示的人身上发生了什么

Thinking of initialising a d3 layout through that timeout function or whatever you rename it is probably awry. 考虑通过超时功能初始化d3布局或者重命名它可能是错误的。 The force graph declaration is the initialization in my head. 力图声明是我头脑中的初始化。 In the full example again, the update/start function takes your data and binds it to dom elements, whether it's the first time it's run or the 100th time it's the same code, just that in the first case the data join will produce new elements only through the .enter() qualifier. 再次在完整示例中,更新/启动函数获取数据并将其绑定到dom元素,无论是第一次运行还是第100次它是相同的代码,只是在第一种情况下数据连接将生成新元素只能通过.enter()限定符。

Q2. Q2。 I found d3 difficult at first because it uses the same/near names for different things. 我发现d3起初很难,因为它使用相同/接近的名称来表示不同的东西。

In the full example you link to, nodes is an array that is passed to a separate force.nodes() function, and node is a selection of dom elements that is made when joined to force.nodes() ( force.nodes() presumably returns the nodes array mentioned first) 在链接到的完整示例中,nodes是一个传递给单独的force.nodes()函数的数组,node是连接到force.nodes()时生成的dom元素的选择(force.nodes()可能会返回首先提到的节点数组)

Changes occur in the start function where this data-join happens and dom elements are added/updated/removed as required. 更改发生在启动此数据连接的启动函数中,并根据需要添加/更新/删除dom元素。

1: yes the timeout doesnt have to be there, you could append this to button click. 1:是的,超时不必存在,您可以将其附加到按钮单击。

2: yes they only get pushed to those arrays, the layout only gets updated when you call update, ie run the initialization of the force graph with the new data. 2:是的,它们只被推送到那些数组,只有在调用update时才会更新布局,即使用新数据运行强制图的初始化。 I've just been on this question which may help you understand : 我刚刚谈到这个可能有助于你理解的问题:

https://stackoverflow.com/users/3382204/cyril https://stackoverflow.com/users/3382204/cyril

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

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