我在vis.js中有一个包含许多节点的网络图.当选择某个组时,我想平移和缩放图形,以便该组的所有节点都适合屏幕.
我遍历图中的每个节点并计算我感兴趣的所有节点的边界框,然后我使用该moveTo方法移动并将图形缩放到该边界框的中心.伪代码:
var allNodes = data.nodes.get({
returnType: "Object"
});
var bounds;
for (n in allNodes) {
if (matchesCondition(allNodes[n])) {
bounds = extendBounds(bounds, graph.getBoundingBox(allNodes[n]));
}
}
var newViewport = {
position: {
x: (bounds.x1+bounds.x2)/2;
y: (bounds.y1+bounds.y2)/2;
},
// What is the visible width, where do I get it from?
scale: Math.min(??? / (bounds.x2-bounds.x1), ??? / (bounds.y2-bounds.y1))
}
graph.moveTo(newViewport);
Run Code Online (Sandbox Code Playgroud)
问题是:我如何计算比例,即我该怎么替换??? 用在上面的伪代码?