分组作为子图

有一些常见的方法来处理作为组成员的节点和链接,就好像它是自己的图一样。整理图表的一种方法是“折叠”一个组Group来隐藏它所包含的子图。

Group 有它自己的组布局Group.layout,用来负责成员节点Node定位和成员链接线Link的布局。就像图表Diagram的图表布局Diagram.layout一样.

Keep in mind that subgraphs are not separate Diagrams and that Groups are just one way of organizing Parts. Because subgraphs are collections of Nodes and Links in the same Diagram as the Group itself, it is possible to have Links that connect Nodes that are inside a Group with Nodes that are outside of the group. It is also possible to have links that connect nodes with the group of which they are a member.

子图的布局

可用通过Layout来设置子图的布局属性Group.layout .
This operates on the group's member nodes and links as if it were its own diagram. A diagram layout of nodes that include groups with their own layout will treat those groups as if they were simple nodes, albeit probably larger than normal nodes.

In this example the group has a different layout than the layout for the whole diagram. In this case the only difference is the direction in which the layout works, but you could use a completely different layout algorithm.

For simplicity these examples use the default templates for nodes and links.

  diagram.groupTemplate =
    $(go.Group, "Auto",
      // declare the Group.layout:
      { layout: $(go.LayeredDigraphLayout,
                  { direction: 0, columnSpacing: 10 }) },
      $(go.Shape, "RoundedRectangle",  // surrounds everything
        { parameter1: 10, fill: "rgba(128,128,128,0.33)" }),
      $(go.Panel, "Vertical",  // position header above the subgraph
        $(go.TextBlock,     // group title near top, next to button
          { font: "Bold 12pt Sans-Serif" },
          new go.Binding("text", "key")),
        $(go.Placeholder,     // represents area for all member parts
          { padding: 5, background: "white" })
      )
    );

  // declare the Diagram.layout:
  diagram.layout = $(go.LayeredDigraphLayout,
                     { direction: 90, layerSpacing: 10, isRealtime: false });

  var nodeDataArray = [
    { key: "Alpha" },
    { key: "Omega", isGroup: true },
    { key: "Beta", group: "Omega" },
    { key: "Gamma", group: "Omega" },
    { key: "Epsilon", group: "Omega" },
    { key: "Zeta", group: "Omega" },
    { key: "Delta" }
  ];
  var linkDataArray = [
    { from: "Alpha", to: "Omega" }, // from a Node to the Group
    { from: "Beta", to: "Gamma" },  // this link is a member of the Group
    { from: "Beta", to: "Epsilon" },  // this link is a member of the Group
    { from: "Gamma", to: "Zeta" },  // this link is a member of the Group
    { from: "Epsilon", to: "Zeta" },  // 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);

组的默认布局是 Layout的实例,就像图表 Diagram一样. 所以定义组布局Group.layout的情况下,组的默认布局将定位没有实际坐标Part.location的所有成员节点。.

If you explicitly set Group.layout to null, the Diagram will be responsible for laying out all of Nodes and Links as if the Group did not exist. This is possible because a subgraph is not another Diagram.

折叠和展开组

One common technique to visually simplify a diagram is to hide parts of them by "collapsing" them. In the case of Groups, it may make sense to be able to hide the subgraph.

折叠组将Group.isSubGraphExpanded设置false; 展开组则设置为true,默认为true

It is commonplace to provide a button on a group to allow users to collapse and expand groups as they wish. GoJS makes this easy to implement by providing a predefined kind of Panel, named "SubGraphExpanderButton", that acts as a button to collapse and expand Groups. This button changes the visibility of the member nodes and links but does not change the visibility of the group itself. When the group's visual tree includes a Placeholder, the placeholder will automatically shrink when the member parts become invisible and will inflate when the member parts become visible again.

Click on the expander button to collapse or expand the group. Changing the size of the group also invalidates the layout that is responsible for positioning the group as a single node. Often the size of the group changes greatly, so a layout usually needs to be performed again.

  diagram.groupTemplate =
    $(go.Group, "Auto",
      { layout: $(go.LayeredDigraphLayout,
                  { direction: 0, columnSpacing: 10 }) },
      $(go.Shape, "RoundedRectangle", // surrounds everything
        { parameter1: 10, fill: "rgba(128,128,128,0.33)" }),
      $(go.Panel, "Vertical",  // position header above the subgraph
        { defaultAlignment: go.Spot.Left },
        $(go.Panel, "Horizontal",  // the header
          { defaultAlignment: go.Spot.Top },
          $("SubGraphExpanderButton"),  // this Panel acts as a Button
          $(go.TextBlock,     // group title near top, next to button
            { font: "Bold 12pt Sans-Serif" },
            new go.Binding("text", "key"))
        ),
        $(go.Placeholder,     // represents area for all member parts
          { padding: new go.Margin(0, 10), background: "white" })
      )
    );

  diagram.layout = $(go.LayeredDigraphLayout,
                     { direction: 90, layerSpacing: 10, isRealtime: false });

  var nodeDataArray = [
    { key: "Alpha" },
    { key: "Omega", isGroup: true },
    { key: "Beta", group: "Omega" },
    { key: "Gamma", group: "Omega" },
    { key: "Epsilon", group: "Omega" },
    { key: "Zeta", group: "Omega" },
    { key: "Delta" }
  ];
  var linkDataArray = [
    { from: "Alpha", to: "Omega" }, // from a Node to the Group
    { from: "Beta", to: "Gamma" },  // this link is a member of the Group
    { from: "Beta", to: "Epsilon" },  // this link is a member of the Group
    { from: "Gamma", to: "Zeta" },  // this link is a member of the Group
    { from: "Epsilon", to: "Zeta" },  // 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);

如果不希望在组更改大小时再次执行布局,可以设置Part.layoutConditions属性来控制布局

Placeholders can be part of complex panels. The following example demonstrates a different way to have each Group have a header holding a button and some text.

  diagram.groupTemplate =
    $(go.Group, "Auto",
      { layout: $(go.TreeLayout) },
      $(go.Shape, "Rectangle", { fill: "orange", stroke: "darkorange" }),
      $(go.Panel, "Table",
        { margin: 0.5 },  // avoid overlapping border with table contents
        $(go.RowColumnDefinition, { row: 0, background: "white" }),  // header is white
        $("SubGraphExpanderButton", { row: 0, column: 0, margin: 3 }),
        $(go.TextBlock,  // title is centered in header
          { row: 0, column: 1, font: "bold 14px Sans-Serif", stroke: "darkorange",
            textAlign: "center", stretch: go.GraphObject.Horizontal },
          new go.Binding("text")),
        $(go.Placeholder,  // becomes zero-sized when Group.isSubGraphExpanded is false
          { row: 1, columnSpan: 2, padding: 10, alignment: go.Spot.TopLeft },
          new go.Binding("padding", "isSubGraphExpanded",
                         function(exp) { return exp ? 10 : 0; } ).ofObject())
      )
    );

  diagram.layout = $(go.TreeLayout, { isRealtime: false });

  diagram.model = new go.GraphLinksModel([
      { key: 1, text: "Alpha" },
      { key: 2, text: "GROUP", isGroup: true },
      { key: 3, text: "Beta", group: 2 },
      { key: 4, text: "Gamma", group: 2 },
      { key: 5, text: "Delta" }
    ], [
      { from: 1, to: 3 },
      { from: 3, to: 4 },
      { from: 1, to: 5 }
    ]);
加入 GoJS 交流群
GoJS 交流群 (769862113)