-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrees.html
250 lines (230 loc) · 8.05 KB
/
trees.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>GoJS Trees -- Northwoods Software</title>
<!-- Copyright 1998-2016 by Northwoods Software Corporation. -->
<script src="go.js"></script>
<script src="goIntro.js"></script>
</head>
<body onload="goIntro()">
<div id="container" class="container-fluid">
<div id="content">
<h2>Trees and TreeLayout</h2>
<p>
There is no limit to the kinds of graphs that you can build in <b>GoJS</b>.
But the most common kind of graph forms a "tree".
A tree is a graph where each node may have at most one "tree parent" and at most one link connecting to that parent node,
and where there are no cycles within the graph.
</p>
<p>
Because trees occur so frequently in diagrams,
there is also a tree layout that offers many customizations specifically for trees.
</p>
<h3>Manual layout of a tree structure</h3>
<p>
You can of course position the nodes manually, either by hand or programmatically.
In this first example, the node locations are stored in the node data,
and there is a Binding of <a>Part.location</a> to the node data property.
</p>
<pre data-language="javascript" id="tree">
diagram.nodeTemplate =
$(go.Node, "Auto",
new go.Binding("location", "loc", go.Point.parse),
$(go.Shape, "Ellipse", { fill: "white" }),
$(go.TextBlock,
new go.Binding("text", "key"))
);
diagram.linkTemplate =
$(go.Link,
{ routing: go.Link.Orthogonal, corner: 5 },
$(go.Shape));
var nodeDataArray = [
{ key: "Alpha", loc: "0 60" },
{ key: "Beta", loc: "100 15" },
{ key: "Gamma", loc: "200 0" },
{ key: "Delta", loc: "200 30" },
{ key: "Epsilon", loc: "100 90" },
{ key: "Zeta", loc: "200 60" },
{ key: "Eta", loc: "200 90" },
{ key: "Theta", loc: "200 120" }
];
var linkDataArray = [
{ from: "Alpha", to: "Beta" },
{ from: "Beta", to: "Gamma" },
{ from: "Beta", to: "Delta" },
{ from: "Alpha", to: "Epsilon" },
{ from: "Epsilon", to: "Zeta" },
{ from: "Epsilon", to: "Eta" },
{ from: "Epsilon", to: "Theta" }
];
diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
</pre>
<script>goCode("tree", 600, 200)</script>
<p>
You can also get the same results by using a <a>TreeModel</a>.
</p>
<pre data-language="javascript" id="tree2">
diagram.nodeTemplate =
$(go.Node, "Auto",
new go.Binding("location", "loc", go.Point.parse),
$(go.Shape, "Ellipse", { fill: "white" }),
$(go.TextBlock,
new go.Binding("text", "key"))
);
diagram.linkTemplate =
$(go.Link,
{ routing: go.Link.Orthogonal, corner: 5 },
$(go.Shape));
var nodeDataArray = [
{ key: "Alpha", loc: "0 60" },
{ key: "Beta", loc: "100 15", parent: "Alpha" },
{ key: "Gamma", loc: "200 0", parent: "Beta" },
{ key: "Delta", loc: "200 30", parent: "Beta" },
{ key: "Epsilon", loc: "100 90", parent: "Alpha" },
{ key: "Zeta", loc: "200 60", parent: "Epsilon" },
{ key: "Eta", loc: "200 90", parent: "Epsilon" },
{ key: "Theta", loc: "200 120", parent: "Epsilon" }
];
diagram.model = new go.TreeModel(nodeDataArray);
</pre>
<script>goCode("tree2", 600, 200)</script>
<h3>Automatic TreeLayout</h3>
<p>
It is most common to use <a>TreeLayout</a> for laying out trees.
Just assign <a>Diagram.layout</a> to a new instance of <a>TreeLayout</a>.
This example also defines the <code>setupTree</code> function that is used in later examples on this page.
</p>
<pre data-language="javascript" id="treeLayout">
function setupTree(diagram) {
diagram.nodeTemplate =
$(go.Node, "Auto",
$(go.Shape, "Ellipse", { fill: "white" }),
$(go.TextBlock,
new go.Binding("text", "key"))
);
diagram.linkTemplate =
$(go.Link,
{ routing: go.Link.Orthogonal, corner: 5 },
$(go.Shape));
var nodeDataArray = [
{ key: "Alpha" },
{ key: "Beta", parent: "Alpha" },
{ key: "Gamma", parent: "Beta" },
{ key: "Delta", parent: "Beta" },
{ key: "Epsilon", parent: "Alpha" },
{ key: "Zeta", parent: "Epsilon" },
{ key: "Eta", parent: "Epsilon" },
{ key: "Theta", parent: "Epsilon" }
];
diagram.model = new go.TreeModel(nodeDataArray);
}
setupTree(diagram);
diagram.layout = $(go.TreeLayout); // automatic tree layout
</pre>
<script>goCode("treeLayout", 600, 200)</script>
<script>
function setupTree(diagram) {
var $ = go.GraphObject.make;
diagram.nodeTemplate =
$(go.Node, "Auto",
$(go.Shape, "Ellipse", { fill: "white" }),
$(go.TextBlock,
new go.Binding("text", "key"))
);
diagram.linkTemplate =
$(go.Link,
{ routing: go.Link.Orthogonal, corner: 5 },
$(go.Shape));
var nodeDataArray = [
{ key: "Alpha" },
{ key: "Beta", parent: "Alpha" },
{ key: "Gamma", parent: "Beta" },
{ key: "Delta", parent: "Beta" },
{ key: "Epsilon", parent: "Alpha" },
{ key: "Zeta", parent: "Epsilon" },
{ key: "Eta", parent: "Epsilon" },
{ key: "Theta", parent: "Epsilon" }
];
diagram.model = new go.TreeModel(nodeDataArray);
}
</script>
<h3>Common TreeLayout properties</h3>
<p>
The <a>TreeLayout.angle</a> property controls the general direction of tree growth.
This must be zero (towards the right), 90 (downward), 180 (leftward), or 270 (upward).
</p>
<pre data-language="javascript" id="angle">
setupTree(diagram);
diagram.layout = $(go.TreeLayout, { angle: 90 });
</pre>
<script>goCode("angle", 600, 200)</script>
<p>
The <code>setupTree</code> function was defined above.
</p>
<p>
The <a>TreeLayout.alignment</a> property controls how the parent node is positioned relative to its children.
This must be one of the Alignment... constants defined on <a>TreeLayout</a>.
</p>
<pre data-language="javascript" id="alignment">
setupTree(diagram);
diagram.layout = $(go.TreeLayout, { angle: 90, alignment: go.TreeLayout.AlignmentStart });
</pre>
<script>goCode("alignment", 600, 200)</script>
<p>
For tree layouts, all of the nodes are placed into "layers" according to the length of the chain of links from the root node.
These layers are not to be confused with Diagram <a>Layer</a>s, which control the Z-ordering of the nodes.
The <a>TreeLayout.layerSpacing</a> property controls how close the layers are to each other.
The <a>TreeLayout.nodeSpacing</a> property controls how close nodes are to each other in the same layer.
</p>
<pre data-language="javascript" id="spacing">
setupTree(diagram);
diagram.layout = $(go.TreeLayout, { layerSpacing: 20, nodeSpacing: 0 });
</pre>
<script>goCode("spacing", 600, 200)</script>
<p>
The children of each node can be sorted. By default the <a>TreeLayout.comparer</a> function compares the
<a>Part.text</a> property. So if that property is data bound by the node template, and if you set the
<a>TreeLayout.sorting</a> property to sort in either ascending or descending order,
each parent node will have all of its children sorted in that order by their text strings.
(In this example that means alphabetical ordering of the English names of the letters of the Greek alphabet.)
</p>
<pre data-language="javascript" id="sort">
setupTree(diagram);
diagram.nodeTemplate =
$(go.Node, "Auto",
new go.Binding("text", "key"), // bind Part.text to support sorting
$(go.Shape, "Ellipse", { fill: "lightblue" }),
$(go.TextBlock,
new go.Binding("text", "key"))
);
diagram.layout = $(go.TreeLayout, { sorting: go.TreeLayout.SortingAscending });
</pre>
<script>goCode("sort", 600, 200)</script>
<p>
But you can provide your own function for ordering the children, such as:
</p>
<pre data-language="javascript">
$(go.Diagram, . . .,
{
layout:
$(go.TreeLayout,
{
sorting: go.TreeLayout.SortingAscending,
comparer: function(a, b) {
// A and B are TreeVertexes
var av = a.node.data.index;
var bv = b.node.data.index;
if (av < bv) return -1;
if (av > bv) return 1;
return 0;
},
. . .
})
. . .
})
</pre>
</div>
</div>
</body>
</html>