Angular-4 d3 v4 问题 : Property 'x' does not exist on type 'HierarchyNode'

标签 angular typescript d3.js

我正在尝试在我的 Angular 应用程序中创建一个 d3 树,我正在尝试这个例子: https://bl.ocks.org/d3noob/08ecb6ea9bb68ba0d9a7e89f344acec8

尝试访问节点 x-y 坐标时存在问题,我得到错误: 类型“HierarchyNode”上不存在属性“x”

当我记录数据时,我可以看到 x-y 坐标在那里。 Screenshot showing the logged data

  // declares a tree layout and assigns the size
  var treemap = d3.tree()
    .size([this.height, this.width]);

  //  assigns the data to a hierarchy using parent-child relationships
  var nodes = d3.hierarchy(this.treeData, function(d) {
    return d.children;
    });
  // maps the node data to the tree layout
  nodes = treemap(nodes);
console.log(nodes.descendants());
  // adds each node as a group
  var node = g.selectAll(".node")
    .data(nodes.descendants())
    .enter().append("g")
    .attr("class", function(d) { 
      return "node" + 
        (d.children ? " node--internal" : " node--leaf"); })
    .attr("transform", function(d) { 
      return "translate(" + d.x + "," + d.y + ")"; });

d.x & d.y 产生错误

最佳答案

TypeScript 有 contextual type inference ,并且由于 HierarchyNode 不导出 'x' 和 'y' 属性,因此无法编译。您可以使用 'any' 作为输入类型来避免错误:

...
.attr("transform", function(d: any) { 
  return "translate(" + d.x + "," + d.y + ")"; });

关于Angular-4 d3 v4 问题 : Property 'x' does not exist on type 'HierarchyNode' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47102767/

相关文章:

javascript - Node typescript 项目中的 Jest 覆盖总是返回空

javascript - 通过 D3 绘制 HTML 表格不会更新现有数据

angular - 运行 "Invalid options provided"时获取 "yarn affected-depgraph"

javascript - en-IN 区域设置不适用于 Angular 货币管道

javascript - 我可以使用 express.js 创建 web api 而没有安装 node.js 吗?

javascript - Angular 域模型

typescript - 是否可以对联合类型进行更严格的类型检查?

javascript - 在弧形内弯曲折线图

javascript - 在 Angular 中构建一个宽度为 100% 的可重用 D3.js 图表?

javascript - Firebase 获取单个项目