Change viewport to include multiple nodes

Is it possible to change the camera/viewport to include multiple nodes? Zoom to fit and similar calls look like they only support a single node as an argument. Specifically, I am finding multiple nodes using findNodesByExample() and want to change the viewport to include all of the found nodes on the screen. Thanks.

Yes, you’ll probably want to call Diagram.zoomToRect. Here’s an example:

  $(go.Diagram, . . .,
    {
      "InitialLayoutCompleted": e => {
        const node = ...  // somehow choose a Node
        const coll = new go.Set().add(node);
        coll.addAll(node.findNodesConnected());  // add all Nodes directly connected with it
        const area = e.diagram.computePartsBounds(coll);
        e.diagram.zoomToRect(area);
      },
      . . .
    }

Somehow you need to choose a collection of Parts so you can call Diagram.computePartsBounds to determine the union of their bounds.

Notice that I put the code in an “InitialLayoutCompleted” DiagramEvent listener, which you’ll need to do if you want to zoom and scroll initially. Otherwise, once the diagram has been shown to the user, you could call Diagram.zoomToRect at any time.