简体   繁体   English

d3.js v4将视口移至特定节点

[英]d3.js v4 move viewport to specific node

I use d3.js v4 to build force-directed graph just like in the basic example. 就像在基本示例中一样,我使用d3.js v4来构建力导向图。 I've also created input field, where user can type the node name. 我还创建了输入字段,用户可以在其中输入节点名称。 Then I search the matching circle element like so: 然后我搜索匹配的circle元素,如下所示:

findNode(name) {
    let theNode = this.d3Svg.selectAll('circle').filter(function(d) {
      return d.name == name;
    });
}

After that I want to move viewport to the foundNode . 之后,我想将视口移至foundNode How can I do that? 我怎样才能做到这一点?

Tried this: this.d3.zoom().translateTo(theNode, theNode.x, theNode.y); 尝试过这个: this.d3.zoom().translateTo(theNode, theNode.x, theNode.y); but nothing happens. 但什么也没发生。 Here is what console.log('theNode: ',theNode) says: 这是console.log('theNode: ',theNode)所说的:

thisNode

I've also found similar question , but some older version of d3 is used there. 我也找到了类似的问题 ,但是在那里使用了较旧版本的d3。

Here is the solution. 是解决方案。 The following code brings the viewport to the center: 以下代码将视口置于中心:

svg.transition()
          .duration(750)
          .call(zoom.transform, d3.zoomIdentity.translate([width / 2, height / 2]));

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

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