Shift all to nodes and its subnodes when inserting a node in between two nodes

Hey Walter,

I want that like in the image, if i programatically add a new node between two nodes (3 and 4 in this case), the new node should take the location of the node 4 and all the nodes after 4 should move its location maybe 300points to the right.

in the example, there are very few nodes after 4, but there could be 100s of nodes after that some with multiple child nodes. how to ensure all these nodes shift 300points to the right. also, i dont want nodes to overlap.

There are a lot of choices that you could reasonably make for this situation.

I think you want in a transaction to call Diagram.moveParts to actually shift some nodes towards the right. But which collection? Did you want to find all nodes that are connected (perhaps indirectly) via links with the newly placed Node that are also to the right of that Node? Or just all nodes that are on the right side of the node, even if they are not connected with the new Node? I guess it also depends on whether or not there are unrelated (i.e. not connected with the new node) nodes that might be occupying space farther towards the right side (beyond your screenshot) where you would naturally want to shift nodes into.

the idea is i want to add the new node at the location of node 4 since i added it between 3 and 4, and shift 4 and all its children to the right without changing the structure of the existing nodes. i hope ur getting what i mean.

Well, I don’t know what kind of graphs you have, but if your graphs are trees, it might work to do something like:

  const newnode = ...;  // already added to diagram, positioned, and linked
  const subparts = newnode.findTreeParts();
  subparts.remove(newnode);
  newnode.diagram.moveParts(subparts, new go.Point(200, 0));

Or maybe you want to compute subparts based on some other criteria. Perhaps even all nodes that are to the right of newnode. Something like:

  const subparts = new go.Set(newnode.diagram.nodes.filter(n => n.actualBounds.x >= newnode.actualBounds.x)));

I am using GraphLinksModel. The below solutions seems to partially work (as seen in Current Output in the screenshot).

  const subparts = new go.Set(newnode.diagram.nodes.filter(n => n.actualBounds.x >= newnode.actualBounds.x)));

i don’t want the distance nodes 8, 7 and 4 to increase as well as seen in the expected output of the screenshot.

You’ll need to figure out which nodes you want to move. I can imagine a number of reasonable policy choices. But you’ll need to consider all of the possible graphs that you might be inserting into.

I didn’t understand this. when you say a number of reasonable policy choices, could please you give me some examples so I could explain this better.

at the top of mind, i feel, doing something like identifying node 4 and all its children and sub-children (maybe NOT the nodes that go into 4) and shifting those nodes should possibly do the trick.

What about nodes going into 7 or 8, such as from 1, 2, or 3?

Or other nodes with no (indirect) connection with 4 that happen to be right of 4 that could cause an overlap with the nodes that you move?

Good catch, so …

  1. only the outgoing nodes of 4 and their sub-nodes/children,
  2. and all the other nodes on the right of the new node ( to avoid overlap due to step 1).

There isn’t any built-in behavior implementing what I am guessing you might want. You will need to find that collection of Nodes (and Links) yourself.

I am guessing that you want to include Nodes that are to the right of dependent/successor nodes that are also overlapping the area where the nodes will be moved.

yes that is correct