d3.js collapsible force layout with all the nodes collapsed

我怕爱的太早我们不能终老 提交于 2019-12-02 11:16:29

Most likely the problem is that index is a reserved property of nodes in d3's force layout, and it's getting overwritten when the nodes property is updated.

force.nodes([nodes])

If nodes is specified, sets the layout's associated nodes to the specified array. If nodes is not specified, returns the current array, which defaults to the empty array. Each node has the following attributes:

index - the zero-based index of the node within the nodes array.

x - the x-coordinate of the current node position.

y - the y-coordinate of the current node position.

px - the x-coordinate of the previous node position.

py - the y-coordinate of the previous node position.

fixed - a boolean indicating whether node position is locked.

weight - the node weight; the number of associated links.

These attributes do not need to be set before passing the nodes to the layout; if they are not set, suitable defaults will be initialized by the layout when start is called. However, be aware that if you are storing other data on your nodes, your data attributes should not conflict with the above properties used by the layout.

Documentation

To prevent the conflict, rename the index property in your json to something else. In my own project I'm referring to the distance of a given node from the root as it's depth.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!