Groups Without Placeholders

Although it is very common to use a Placeholder inside a Group, it is not required. Using a Shape, for example, instead of a Placeholder permits features such as having a group maintain a fixed size, independent of the sizes and positions of its member nodes, and even when there are no member nodes at all. It also may allow the user to resize the "area" if that functionality is desired.

Fixed size Groups

Not using a Placeholder in a Group means that you have to maintain the size and position of the group, because it cannot depend on the size and position of its member nodes. In these examples we will explicitly set and/or bind the Part.location of the nodes, including the groups. The Shape that replaces the Placeholder in the group's template should also get its GraphObject.desiredSize set or bound.


  diagram.nodeTemplate =
    $(go.Node,
      new go.Binding("location", "loc", go.Point.parse),
      $(go.TextBlock,
        new go.Binding("text", "key"))
    );

  diagram.groupTemplate =
    $(go.Group, "Vertical",
      { selectionObjectName: "PH",
        locationObjectName: "PH" },
      new go.Binding("location", "loc", go.Point.parse),
      $(go.TextBlock,  // group title
        { font: "Bold 12pt Sans-Serif" },
        new go.Binding("text", "key")),
      $(go.Shape,  // using a Shape instead of a Placeholder
        { name: "PH",
          fill: "lightyellow" },
        new go.Binding("desiredSize", "size", go.Size.parse))
    );

  var nodeDataArray = [
    { key: "Alpha", loc: "0 0" },
    { key: "Beta", group: "Omega", loc: "75 75" },
    { key: "Gamma", group: "Omega", loc: "125 75" },
    { key: "Omega", isGroup: true, loc: "50 50", size: "150 50" },
    { key: "Delta", loc: "200 0" }
  ];
  var linkDataArray = [
    { from: "Alpha", to: "Beta" },  // from outside the Group to inside it
    { from: "Beta", to: "Gamma" },  // this link is a member of the Group
    { from: "Omega", to: "Delta" }  // from the Group to a Node
  ];
  diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
  diagram.select(diagram.findNodeForKey("Omega"));

Note that moving the "Beta" or "Gamma" nodes does not change the size or position of the "Omega" group. However moving or copying or deleting the group includes those member nodes in the operation.

One can control where the user may drag member nodes. For example, the Swim Lanes sample demonstrates a custom Part.dragComputation function that limits the motion of a member node to stay within its containing group.

Resizable Groups

You can make the main shape resizable by the user. (At the current time groups are not rotatable.)

This example also makes the Part.location and GraphObject.desiredSize data bindings TwoWay, so that as the user moves groups or resizes their main shapes, the data in the model is updated automatically.


  diagram.nodeTemplate =
    $(go.Node,
      new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
      $(go.TextBlock,
        new go.Binding("text", "key"))
    );

  diagram.groupTemplate =
    $(go.Group, "Vertical",
      { selectionObjectName: "PH",
        locationObjectName: "PH",
        resizable: true,
        resizeObjectName: "PH" },
      new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
      $(go.TextBlock,  // group title
        { font: "Bold 12pt Sans-Serif" },
        new go.Binding("text", "key")),
      $(go.Shape,  // using a Shape instead of a Placeholder
        { name: "PH",
          fill: "lightyellow" },
        new go.Binding("desiredSize", "size", go.Size.parse).makeTwoWay(go.Size.stringify))
    );

  var nodeDataArray = [
    { key: "Alpha", loc: "0 0" },
    { key: "Beta", group: "Omega", loc: "75 75" },
    { key: "Gamma", group: "Omega", loc: "125 75" },
    { key: "Omega", isGroup: true, loc: "50 50", size: "150 50" },
    { key: "Delta", loc: "200 0" }
  ];
  var linkDataArray = [
    { from: "Alpha", to: "Beta" },  // from outside the Group to inside it
    { from: "Beta", to: "Gamma" },  // this link is a member of the Group
    { from: "Omega", to: "Delta" }  // from the Group to a Node
  ];
  diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
  diagram.select(diagram.findNodeForKey("Omega"));

It is also possible to control how the user resizes a group. For example, the Swim Lanes sample demonstrates a custom ResizingTool that limits how small each lane can go. It also demonstrates a custom Adornment that has only two resize handles.

Containers that are not Groups

You do not have to use Groups as the only mechanism by which to organize a collection of Parts. For example, the Layer Bands sample demonstrates how some Layouts can be customized to automatically maintain the positions and sizes of special parts that are in the background, appearing to surround the nodes that belong to each layout layer.

Not using Groups also means that it becomes possible to avoid some of the restrictions inherent in Groups, such as the limitation that each Part can have at most one Part.containingGroup. The Shared States sample demonstrates how one can make it appear that more than one "group" can contain a node. However, this requires some additional custom Tools and custom Layouts, or always explicitly setting/binding the location and size of every node and "group".

GoJS