【问题标题】:GoJs Is it possible to have multipe layouts in one diagramGoJs 是否可以在一个图中有多个布局
【发布时间】:2016-04-22 21:58:18
【问题描述】:

我想要一个在同一个图表中具有垂直和水平布局的图表。如何在 gojs 中实现这一点?

【问题讨论】:

    标签: javascript gojs


    【解决方案1】:

    您可以通过多种方式实现目标。我能想到三个副手。按工作降序排列:

    • 创建一个自定义的布局来满足您的需求。这是最通用的,并且与您已与您想要合并的两个现有图表一起使用的数据结构最兼容。
    • 在您的情况下,您可以使用扩展目录中的 TableLayout:http://gojs.net/latest/extensions/Table.html。您可能可以继续使用 Group,但将它们的 Group.layout 设置为 null,以便在执行布局时完全忽略它们。
    • 将现有图表中的所有内容放入一个组中,并将其他现有图表中的所有内容放入另一个组中。第一个图的 Diagram.layout 将是第一组的 Group.layout,第二个图的 Diagram.layout 将是第二组的Group.layout。

    请注意,每个 Diagram 只能有一个 Model(Diagram.model),因此您需要添加所有三种技术将所有数据整合到一个模型中,而不会混淆它们之间的引用。这意味着您需要确保节点的键都是唯一的。

    这是一个示例,说明如何使用第三种技术将两个单独的图表放入一个图表中。我将从 Minimal 示例的两个副本开始,http://gojs.net/latest/samples/minimal.html,其中唯一的变化是一个具有 ForceDirectedLayout,另一个具有 LayeredDigraphLayout。因此将定义一个:

    myDiagram = $(go.Diagram, "myDiagramDiv",
                  {
                    initialContentAlignment: go.Spot.Center,
                    layout: $(go.ForceDirectedLayout),
                    "undoManager.isEnabled": true
                  });
    

    另一个将被定义:

    myDiagram = $(go.Diagram, "myDiagramDiv",
                  {
                    initialContentAlignment: go.Spot.Center,
                    layout: $(go.LayeredDigraphLayout),
                    "undoManager.isEnabled": true
                  });
    

    但除此之外,这两个图表与最小样本完全相同。

    Minimal 的每个模型最初由以下人员创建:

    myDiagram.model = new go.GraphLinksModel(
    [
      { key: "Alpha", color: "lightblue" },
      { key: "Beta", color: "orange" },
      { key: "Gamma", color: "lightgreen" },
      { key: "Delta", color: "pink" }
    ],
    [
      { from: "Alpha", to: "Beta" },
      { from: "Alpha", to: "Gamma" },
      { from: "Beta", to: "Beta" },
      { from: "Gamma", to: "Delta" },
      { from: "Delta", to: "Alpha" }
    ]);
    

    因此,我们首先需要创建一个组合模型,即两个模型相加。将它们放在一起的一种方法是:

    myDiagram.model = new go.GraphLinksModel(
    [
      { key: "Alpha", color: "lightblue" },
      { key: "Beta", color: "orange" },
      { key: "Gamma", color: "lightgreen" },
      { key: "Delta", color: "pink" },
      { key: "Alpha2", color: "lightblue" },
      { key: "Beta2", color: "orange" },
      { key: "Gamma2", color: "lightgreen" },
      { key: "Delta2", color: "pink" }
    ],
    [
      { from: "Alpha", to: "Beta" },
      { from: "Alpha", to: "Gamma" },
      { from: "Beta", to: "Beta" },
      { from: "Gamma", to: "Delta" },
      { from: "Delta", to: "Alpha" },
      { from: "Alpha2", to: "Beta2" },
      { from: "Alpha2", to: "Gamma2" },
      { from: "Beta2", to: "Beta2" },
      { from: "Gamma2", to: "Delta2" },
      { from: "Delta2", to: "Alpha2" }
    ]);
    

    我再次提到,无论您使用哪种技术,这都是您需要完成的工作。大概您已经这样做了,只是想知道如何处理两种布局。

    我建议的第三种技术使用 Group 将最初的内容封装在整个 Diagram 中。所以让我们在模型中添加两​​个组,并将每个原始节点分配给相应的组:

    myDiagram.model = new go.GraphLinksModel(
    [
      { key: "FD", isGroup: true, category: "FD" },  // NEW
      { key: "LD", isGroup: true, category: "LD" },  // NEW
      { key: "Alpha", color: "lightblue", group: "FD" },
      { key: "Beta", color: "orange", group: "FD" },
      { key: "Gamma", color: "lightgreen", group: "FD" },
      { key: "Delta", color: "pink", group: "FD" },
      { key: "Alpha2", color: "lightblue", group: "LD" },
      { key: "Beta2", color: "orange", group: "LD" },
      { key: "Gamma2", color: "lightgreen", group: "LD" },
      { key: "Delta2", color: "pink", group: "LD" }
    ],
    [
      { from: "Alpha", to: "Beta" },
      { from: "Alpha", to: "Gamma" },
      { from: "Beta", to: "Beta" },
      { from: "Gamma", to: "Delta" },
      { from: "Delta", to: "Alpha" },
      { from: "Alpha2", to: "Beta2" },
      { from: "Alpha2", to: "Gamma2" },
      { from: "Beta2", to: "Beta2" },
      { from: "Gamma2", to: "Delta2" },
      { from: "Delta2", to: "Alpha2" }
    ]);
    

    现在我们只需要定义每个组/类别/模板:

    myDiagram.groupTemplateMap.add("FD",
      $(go.Group, "Auto",
        { layout: $(go.ForceDirectedLayout) },
        $(go.Shape, { fill: "white", stroke: "lightgray" }),
        $(go.Placeholder, { padding: 10 })
      ));
    
    myDiagram.groupTemplateMap.add("LD",
      $(go.Group, "Auto",
        { layout: $(go.LayeredDigraphLayout) },
        $(go.Shape, { fill: "white", stroke: "lightgray" }),
        $(go.Placeholder, { padding: 10 })
      ));
    

    就本演示而言,每种组的视觉外观细节无关紧要,就像节点和链接的外观细节无关紧要一样。对你来说重要的是一个组模板使用一种布局,另一个使用不同的布局,有两个组数据对象,所有节点数据都分配给适当的组。

    在这种情况下,每个组模板都被用作单例,但也许您的要求会导致使用多个组模板中的任何一个或所有模板。

    现在您只需要指定 Diagram.layout 来控制两组相对于彼此的排列方式。也许最简单的方法是使用 GridLayout:

    myDiagram = $(go.Diagram, "myDiagramDiv",
                  {
                    initialContentAlignment: go.Spot.Center,
                    layout: $(go.GridLayout, { wrappingColumn: 1 }),
                    "undoManager.isEnabled": true
                  });
    

    您当然可以根据需要以任何方式自定义布局,包括使用完全不同的布局或自定义布局。

    这是完整的代码。为简洁起见,我从原始 Minimal 样本中删除了一堆 cmets:

    <!DOCTYPE html>
    <html>
    <head>
    <title>Combining 2 Diagrams with Different Layouts</title>
    <!-- Copyright 1998-2016 by Northwoods Software Corporation. -->
    <meta charset="UTF-8">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gojs/1.6.5/go.js"></script>
    <script id="code">
      function init() {
        var $ = go.GraphObject.make;  // for conciseness in defining templates
    
        myDiagram = $(go.Diagram, "myDiagramDiv",
                      {
                        initialContentAlignment: go.Spot.Center,
                        layout: $(go.GridLayout, { wrappingColumn: 1 }),
                        "undoManager.isEnabled": true
                      });
    
        myDiagram.nodeTemplate =
          $(go.Node, "Auto",
            $(go.Shape, "RoundedRectangle",
              new go.Binding("fill", "color")),
            $(go.TextBlock, { margin: 3 },
              new go.Binding("text", "key"))
          );
    
        myDiagram.groupTemplateMap.add("FD",
          $(go.Group, "Auto",
            { layout: $(go.ForceDirectedLayout) },
            $(go.Shape, { fill: "white", stroke: "lightgray" }),
            $(go.Placeholder, { padding: 10 })
          ));
    
        myDiagram.groupTemplateMap.add("LD",
          $(go.Group, "Auto",
            { layout: $(go.LayeredDigraphLayout) },
            $(go.Shape, { fill: "white", stroke: "lightgray" }),
            $(go.Placeholder, { padding: 10 })
          ));
    
        myDiagram.model = new go.GraphLinksModel(
        [
          { key: "FD", isGroup: true, category: "FD" },
          { key: "LD", isGroup: true, category: "LD" },
          { key: "Alpha", color: "lightblue", group: "FD" },
          { key: "Beta", color: "orange", group: "FD" },
          { key: "Gamma", color: "lightgreen", group: "FD" },
          { key: "Delta", color: "pink", group: "FD" },
          { key: "Alpha2", color: "lightblue", group: "LD" },
          { key: "Beta2", color: "orange", group: "LD" },
          { key: "Gamma2", color: "lightgreen", group: "LD" },
          { key: "Delta2", color: "pink", group: "LD" }
        ],
        [
          { from: "Alpha", to: "Beta" },
          { from: "Alpha", to: "Gamma" },
          { from: "Beta", to: "Beta" },
          { from: "Gamma", to: "Delta" },
          { from: "Delta", to: "Alpha" },
          { from: "Alpha2", to: "Beta2" },
          { from: "Alpha2", to: "Gamma2" },
          { from: "Beta2", to: "Beta2" },
          { from: "Gamma2", to: "Delta2" },
          { from: "Delta2", to: "Alpha2" }
        ]);
      }
    </script>
    </head>
    <body onload="init();">
    <div id="sample">
      <div id="myDiagramDiv" style="border: solid 1px black; width:400px; height:600px"></div>
    </div>
    </body>
    </html>

    【讨论】:

      【解决方案2】:

      我是 GoJs 的新手,但我试过这个。

      希望对你有帮助:)

      var $ = go.GraphObject.make;
      //model
      
      var myDiagram1 =
        $(go.Diagram, "myNormal", {
          initialContentAlignment: go.Spot.Center,
          "undoManager.isEnabled": true // enable Ctrl-Z to undo and Ctrl-Y to redo
        });
      
      // define a simple Node template
      myDiagram1.nodeTemplate =
        $(go.Node, "Horizontal",
          // the entire node will have a light-blue background
          {
            background: "#44CCFF"
          },
          $(go.Picture,
            // Pictures should normally have an explicit width and height.
            // This picture has a red background, only visible when there is no source set
            // or when the image is partially transparent.
            {
              margin: 10,
              width: 50,
              height: 50,
              background: "red"
            },
            // Picture.source is data bound to the "source" attribute of the model data
            new go.Binding("source")),
          $(go.TextBlock,
            "Default Text", // the initial value for TextBlock.text
            // some room around the text, a larger font, and a white stroke:
            {
              margin: 12,
              stroke: "white",
              font: "bold 16px sans-serif"
            },
            // TextBlock.text is data bound to the "name" attribute of the model data
            new go.Binding("text", "name"))
        );
      
      var model = $(go.Model);
      model.nodeDataArray = [ // note that each node data object holds whatever properties it needs;
        // for this app we add the "name" and "source" properties
        {
          name: "Don Meow",
          source: "cat1.png"
        },
        {
          name: "Copricat",
          source: "cat2.png"
        },
        {
          name: "Demeter",
          source: "cat3.png"
        },
        { /* Empty node data */ }
      ];
      myDiagram1.model = model;
      
      // graph link model
      
      
      var myDiagram2 =
        $(go.Diagram, "graphlinksmodel", {
          initialContentAlignment: go.Spot.Right,
          "undoManager.isEnabled": true, // enable Ctrl-Z to undo and Ctrl-Y to redo
          layout: $(go.TreeLayout, // specify a Diagram.layout that arranges trees
            {
              angle: 90,
              layerSpacing: 35
            })
        });
      
      // the template we defined earlier
      myDiagram2.nodeTemplate =
        $(go.Node, "Horizontal", {
            background: "#44CCFF"
          },
          $(go.Picture, {
              margin: 10,
              width: 50,
              height: 50,
              background: "red"
            },
            new go.Binding("source")),
          $(go.TextBlock, "Default Text", {
              margin: 12,
              stroke: "white",
              font: "bold 16px sans-serif"
            },
            new go.Binding("text", "name"))
        );
      
      // define a Link template that routes orthogonally, with no arrowhead
      myDiagram2.linkTemplate =
        $(go.Link, {
            routing: go.Link.Orthogonal,
            corner: 5
          },
          $(go.Shape, {
            strokeWidth: 3,
            stroke: "#555"
          })); // the link shape
      
      var model = $(go.GraphLinksModel);
      model.nodeDataArray = [{
          key: "1",
          name: "Don Meow",
          source: "cat1.png"
        },
        {
          key: "2",
          name: "Demeter",
          source: "cat2.png"
        },
        {
          key: "3",
          name: "Copricat",
          source: "cat3.png"
        },
        {
          key: "4",
          name: "Jellylorum",
          source: "cat4.png"
        },
        {
          key: "5",
          name: "Alonzo",
          source: "cat5.png"
        },
        {
          key: "6",
          name: "Munkustrap",
          source: "cat6.png"
        }
      ];
      model.linkDataArray = [{
          from: "1",
          to: "2"
        },
        {
          from: "1",
          to: "3"
        },
        {
          from: "3",
          to: "4"
        },
        {
          from: "3",
          to: "5"
        },
        {
          from: "2",
          to: "6"
        }
      ];
      myDiagram2.model = model;
      
      
      
      //tree model
      var myDiagram =
        $(go.Diagram, "treemodel", {
          "undoManager.isEnabled": true, // enable Ctrl-Z to undo and Ctrl-Y to redo
          layout: $(go.TreeLayout, // specify a Diagram.layout that arranges trees
            {
              angle: 90,
              layerSpacing: 35
            })
        });
      
      // the template we defined earlier
      myDiagram.nodeTemplate =
        $(go.Node, "Horizontal", {
            background: "#44CCFF"
          },
          $(go.Picture, {
              margin: 10,
              width: 50,
              height: 50,
              background: "red"
            },
            new go.Binding("source")),
          $(go.TextBlock, "Default Text", {
              margin: 12,
              stroke: "white",
              font: "bold 16px sans-serif"
            },
            new go.Binding("text", "name"))
        );
      
      // define a Link template that routes orthogonally, with no arrowhead
      myDiagram.linkTemplate =
        $(go.Link, {
            routing: go.Link.Orthogonal,
            corner: 5
          },
          $(go.Shape, {
            strokeWidth: 3,
            stroke: "#555"
          })); // the link shape
      
      var model = $(go.TreeModel);
      model.nodeDataArray = [{
          key: "1",
          name: "Don Meow",
          source: "cat1.png"
        },
        {
          key: "2",
          parent: "1",
          name: "Demeter",
          source: "cat2.png"
        },
        {
          key: "3",
          parent: "1",
          name: "Copricat",
          source: "cat3.png"
        },
        {
          key: "4",
          parent: "3",
          name: "Jellylorum",
          source: "cat4.png"
        },
        {
          key: "5",
          parent: "3",
          name: "Alonzo",
          source: "cat5.png"
        },
        {
          key: "6",
          parent: "2",
          name: "Munkustrap",
          source: "cat6.png"
        }
      ];
      myDiagram.model = model;
      <!DOCTYPE html>
      <!-- HTML5 document type -->
      <html>
      
      <head>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
      </head>
      
      <body>
      
        <!-- The DIV for a Diagram needs an explicit size or else we will not see anything.
           In this case we also add a background color so we can see that area. -->
        <div class="container-fluid">
          <div class="row">
            <div class="col align-self-center" id="myNormal" style="width:400px; height:200px; background-color: #DAE4E4;"></div>
          </div>
          <div class="row">
            <div class="col" id="graphlinksmodel" style="width:600px; height:300px; background-color: rgb(142, 236, 101);"></div>
            <div class="col" id="treemodel" style="width:600px; height:300px; background-color: rgb(231, 243, 177);"></div>
          </div>
        </div>
      
        <!-- use go-debug.js when developing and go.js when deploying -->
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/gojs/1.8.21/go-debug.js"></script>
        <script src="./base.js"></script>
      </body>
      
      </html>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-31
        • 1970-01-01
        • 1970-01-01
        • 2011-12-16
        • 2021-06-15
        • 1970-01-01
        • 2010-12-10
        • 2011-08-05
        相关资源
        最近更新 更多