d3js tree.nodes()不是函数

Mop*_*ath 8 d3.js

虽然下面的代码可以在d3v3中找到,但它在v4中失败了.

 var nodes = tree.nodes(root).reverse(),
      links = tree.links(nodes);
Run Code Online (Sandbox Code Playgroud)

未捕获的TypeError:tree.nodes不是函数

它在v4中的替代方案是什么?

Pau*_*l S 11

import { hierarchy, tree } from 'd3-hierarchy'

// create a hierarchy from the root
const treeRoot = hierarchy(root)
tree(treeRoot)
// nodes
const nodes = treeRoot.descendants()
// links
const links = treeRoot.links()
Run Code Online (Sandbox Code Playgroud)

  • 回答问题是参与者的真正尝试。毫无理由地拒绝投票是不道德的。因此,我赞成废除那个愚蠢的反对意见。 (5认同)
  • 对于纯 javascript,跳过导入并写入 `d3.hierarchy(root)` 而不是 `hierarchy(root)` 等。 (4认同)