Angular-4 d3 v4问题:属性' x'在类型' HierarchyNode'中不存在

时间:2017-11-03 18:59:18

标签: angular typescript d3.js

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

尝试访问节点x-y坐标时出现问题,我收到错误: 财产' x'类型' HierarchyNode'

上不存在

当我记录数据时,我可以看到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产生错误

2 个答案:

答案 0 :(得分:1)

TypeScript有contextual type inference,因为HierarchyNode没有导出' x'并且' y'这不会编译的属性。你可以使用任何'作为避免错误的输入类型:


des_links = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', des)
for i in des_links:
   tmps = "/".join(i.split('/')[0:-1])
   print(tmps)

答案 1 :(得分:0)

您可以明确指定要覆盖Contextual Typing的类型

.attr('cx', function (d: HierarchyPointNode<MyDatumInterface>) {
        return d.x // no TS error
    })