相关疑难解决方法(0)

d3 (v4) 树布局和打字稿:类型“HierarchyNode<IOorgChartNode>”上不存在属性“x”

我试图在使用d3.hierarchy具有树布局的根时使用正确的 ts 类型,例如:

interface MyCustomGraphType {
  id: string;
  children?: MyCustomGraphType[]
}
let treeData: MyCustomGraphType = {/*...*/};
let root = d3.hierarchy(treeData);
let layout = d3.tree().size([500,500])(root);
let nodes = layout.descendants();

// BIND DATA (Node)
let node = this.nodesGroup.selectAll('g.node')
  .data(nodes, d => d.data.person.email); // <--- compile-time error
   // ^^^ Property 'data' does not exist on type '{} | HierarchyNode<MyCustomGraphType>'.

// ... later:
node.enter()
  .append('circle')
  .attr('transform', d => `translate(${d.x}, ${d.y})`); // <--- compile-time error
   // ^^^ Property 'y' does not exist on …
Run Code Online (Sandbox Code Playgroud)

d3.js typescript

6
推荐指数
1
解决办法
2846
查看次数

标签 统计

d3.js ×1

typescript ×1