GoJs 是否可以在一张图中有多个布局

GoJs Is it possible to have multipe layouts in one diagram

我想要一个在同一个图表中有垂直和水平布局的图表。我怎样才能在 gojs 中实现这一点?

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

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

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

下面是一个示例,说明如何使用第三种技术将两个单独的图表合并为一个图表。我将从最小样本的两个副本开始,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" }
]);

再次强调,无论您使用哪种技术,这都是您需要做的工作。想必您已经这样做了,只是想知道如何处理两个布局。

我建议的第三种技术使用 Groups 来封装原本在整个 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" }
]);

现在我们只需要定义每个group/category/template:

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
              });

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

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

<!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>

我是 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>