gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:09:09 30 4
gpt4 key购买 nike

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

enter image description here

最佳答案

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

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

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

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

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

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

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

关于javascript - GoJs 是否可以在一张图中有多个布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36803939/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com