I have a network graph in vis.js with many nodes. When choosing a specific group, I would like to pan and scale the graph so that all the nodes of this group fit the screen.
I look at each node in the graph and compute the bounding box for all the nodes that interest me, then I use the moveTo method to move and scale the graph to the center of this bounding box. Pseudo Code:
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; },
Question: how can I calculate the scale, that is, what will I replace ??? with in the pseudocode above?
source share