gpt4 book ai didi

javascript - 如何使用 d3 的力布局平移到节点

转载 作者:行者123 更新时间:2023-12-05 06:44:05 33 4
gpt4 key购买 nike

我想在按名称搜索节点时关注它。我正在尝试使用最近的方法来做到这一点....

zoom = d3.behavior.zoom()
.scaleExtent([.05, 10])
.on("zoom", zoomed);
svg = d3.select("#graph")
.append("svg")
.attr("height", height)
.attr("width", width)
.call(zoom);
....
function zoomed(sel) {
zoomBase(d3.event.translate , d3.event.scale);
}
function zoomBase(translate, scale){
zoom.scale(scale);
zoom.translate(translate);
container.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
}
function recenter(node){
var node = findNodeByName(node);
if(zoom.scale() < 0.5){
zoom.scale(0.5);
}
zoom.translate([node.x, node.y]); // Math seems to be wrong here
container.attr("transform", "translate(" + translate + ")scale(" + zoom.scale() + ")");
}

问题是,当我越过有问题的节点并搜索它时,我的位置显示为 [-2246.3690822841745, -846.6411913027562] 但是当我得到实际的 x 和 y 时node 我得到 [4346.868560310511, 1950.790521658118] 考虑到我在节点之上,这里有一些数学或我需要的东西吗?

最佳答案

拉斯是对的,这就是答案......

zoom.translate([width / 2 - zoom.scale() * node.x, height / 2 - zoom.scale() * node.y])

稍微打破一下

width / 2 (go to middle) 
-
zoom.scale() * node.x (move middle to the scaled x)

关于javascript - 如何使用 d3 的力布局平移到节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31011004/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com