简体   繁体   English

新版D3如何获取节点?

[英]How to get nodes in the new version of D3?

The D3 examples I've seen use an old version of D3 with tree.nodes(root).reverse() to retrieve nodes array.我见过的 D3 示例使用带有tree.nodes(root).reverse()的旧版本 D3 来检索节点数组。 It does not work with the latest version of D3.它不适用于最新版本的 D3。 How do I get nodes array in the new version?如何在新版本中获取节点数组?

Create a tree layout object:创建树布局对象:

const layout = d3.tree();

Build a root object from your data:从您的数据构建一个根对象:

const root = d3.hierarchy(data, d => d.children);

Extract nodes from the tree:从树中提取节点:

const tree = layout(root);
const nodes = tree.descendants();

Should work for D3 V7 (see a working example here )应该适用于D3 V7 (请参阅此处的工作示例)

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

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