簡體   English   中英

使用 GoJS 創建層次結構

[英]Create a hierarchical structure using GoJS

我希望使用 GoJs 創建一個具有 3 級層次結構(如下所示)的樹圖。

所需層次結構示例供參考

要求:

  1. 子節點可以是串行的(圖中的子節點 1 和子節點 3),也可以是並行的(圖中的子節點 1 和子節點 2)。
  2. 在任何時候,都可以在圖中以串行或並行方式將新節點作為子節點添加到級別 1 或級別 2 的任何節點。
  3. 縮進用於描述層次結構級別的變化。 [在創建的圖像中沿級別添加縮進]
  4. 不應允許用戶自由移動任何節點。 我計划在未來提供限制性的拖放功能。

問題:如何從任何父節點創建第二個分支(如上圖)?

我嘗試為此使用 GoJS 的樹布局,但無法使其按預期工作。 這是我使用的布局:

 layout: $(go.TreeLayout, {alignment: go.TreeLayout.AlignmentStart, angle: 90});

請推薦一個 GoJs 布局以及所需的配置,它可以解決上述所有要求。

這是一個完整的示例:

<!DOCTYPE html>
<html>
<head>
  <title>Dual Tree Layout</title>
  <!-- Copyright 1998-2021 by Northwoods Software Corporation. -->
  <script src="https://unpkg.com/gojs"></script>
  <script id="code">

// decide whether a Node is an "assistant" node or a regular one
function isAssistant(n) { return n && n.data && !!n.data.assistant; }

// this custom TreeLayout was adapted from the Org Chart Assistants sample
function DualTreeLayout() {
  go.TreeLayout.call(this);
}
go.Diagram.inherit(DualTreeLayout, go.TreeLayout);

DualTreeLayout.prototype.makeNetwork = function(coll) {
  var net = go.TreeLayout.prototype.makeNetwork.call(this, coll);
  // copy the collection of TreeVertexes, because we will modify the network
  var vertexcoll = new go.Set(/*go.TreeVertex*/);
  vertexcoll.addAll(net.vertexes);
  for (var it = vertexcoll.iterator; it.next();) {
    var parent = it.value;
    // count the number of assistants
    var acount = 0;
    var ait = parent.destinationVertexes;
    while (ait.next()) {
      if (isAssistant(ait.value.node)) acount++;
    }
    // if a vertex has some number of children that should be assistants
    if (acount > 0) {
      parent._hasDualChildren = true;
      // remember the assistant edges and the regular child edges
      var asstedges = new go.Set(/*go.TreeEdge*/);
      var childedges = new go.Set(/*go.TreeEdge*/);
      var eit = parent.destinationEdges;
      while (eit.next()) {
        var e = eit.value;
        if (isAssistant(e.toVertex.node)) {
          asstedges.add(e);
        } else {
          childedges.add(e);
        }
      }
      // first remove all edges from PARENT
      eit = asstedges.iterator;
      while (eit.next()) { parent.deleteDestinationEdge(eit.value); }
      eit = childedges.iterator;
      while (eit.next()) { parent.deleteDestinationEdge(eit.value); }

      // create substitute vertex to be new parent of all regular children
      var subst = net.createVertex();
      subst._forRegulars = true;
      net.addVertex(subst);
      // reparent regular children to the new substitute vertex
      eit = childedges.iterator;
      while (eit.next()) {
        eit.value.fromVertex = subst;
        subst.addDestinationEdge(eit.value);
      }
      net.linkVertexes(parent, subst, null);

      // create substitute vertex to be new parent of all assistant children
      var subst2 = net.createVertex();
      subst2._forAssistants = true;
      net.addVertex(subst2);
      // reparent all assistant children to the new substitute vertex
      eit = asstedges.iterator;
      while (eit.next()) {
        eit.value.fromVertex = subst2;
        subst2.addDestinationEdge(eit.value);
      }
      net.linkVertexes(parent, subst2, null);
    }
  }
  return net;
};

DualTreeLayout.prototype.assignTreeVertexValues = function(v) {
  if (v._hasDualChildren) {
    v.nodeIndent = 0;
    v.layerSpacing = 0;
    v.layerSpacingParentOverlap = 1;
    v.breadthLimit = 0;
  } else if (v._forAssistants) {
    // this is the substitute parent for the assistant(s)
    v.width = v.parent.width;
    v.height = v.parent.height;
    v.breadthLimit = 0;
  } else if (v._forRegulars) {
    // found the substitute parent for non-assistant children
    v.width = v.parent.width;
    v.height = v.parent.height;
    v.breadthLimit = 180;
  } else {
    v.breadthLimit = 180;
  }
};  // end of DualTreeLayout


  function init() {
    var $ = go.GraphObject.make;

    myDiagram =
      $(go.Diagram, "myDiagramDiv",
        {
          layout:
            $(DualTreeLayout,
              {
                angle: 90, isRouting: true,
                setsPortSpot: false, setsChildPortSpot: false,
                alignment: go.TreeLayout.AlignmentStart
              }),
          "undoManager.isEnabled": true
        });

    myDiagram.nodeTemplate =
      $(go.Node, "Auto",
        { width: 100, height: 60 },
        $(go.Shape, "RoundedRectangle", { fill: "white", portId: "" },
          new go.Binding("fill", "color")),
        $(go.TextBlock, { textAlign: "center" },
          new go.Binding("text")),
        $("TreeExpanderButton", { alignment: go.Spot.Bottom }),
        $("Button", $(go.TextBlock, "S"),
          { alignment: new go.Spot(0.5, 1, -20, 0) },
          { click: (e, button) => { e.diagram.model.commit(m => m.addNodeData({ text: "S", parent: button.part.key })) } }),
        $("Button", $(go.TextBlock, "P"),
          { alignment: new go.Spot(0.5, 1, 20, 0) },
          { click: (e, button) => { e.diagram.model.commit(m => m.addNodeData({ text: "P", parent: button.part.key, assistant: true })) } }),
      );

    myDiagram.linkTemplate =
      $(go.Link,
        {
          layerName: "Background", routing: go.Link.Orthogonal, corner: 10,
          toSpot: new go.Spot(0.001, 0, 20, 0)
        },
        new go.Binding("fromSpot", "toNode", function(n) {
          return (n && n.data.assistant) ? new go.Spot(0.5, 1, 20, 0) : new go.Spot(0.5, 1, -20, 0);
        }).ofObject(),
        $(go.Shape),
        $(go.Shape, { toArrow: "Standard" })
      );

    myDiagram.model = new go.TreeModel(
    [
      { key: 1, text: "Root" },
      { key: 2, text: "Child 1 serial", parent: 1 },
      { key: 3, text: "Child 2 parallel", parent: 1, assistant: true },
      { key: 4, text: "Child 3 serial", parent: 1 },
      { key: 5, text: "Grand 1 1 parallel", parent: 2, assistant: true },
      { key: 6, text: "Grand 1 2 parallel", parent: 2, assistant: true },
      { key: 7, text: "Grand 2 1 serial", parent: 3 },
      { key: 8, text: "Grand 2 2 serial", parent: 3 }
    ]);
  }
  </script>
</head>
<body onload="init()">
  <div id="myDiagramDiv" style="border: solid 1px black; width:100%; height:600px"></div>
</body>
</html>

結果: 在此處輸入圖片說明

我在每個節點上添加了“S”和“P”按鈕,以便用戶向任何節點添加“串行”或“並行”子節點是微不足道的,這樣您就可以看到布局是如何工作的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM