mxRadialTreeLayout.js
7.85 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
* Copyright (c) 2006-2015, JGraph Ltd
* Copyright (c) 2006-2015, Gaudenz Alder
*/
/**
* Class: mxRadialTreeLayout
*
* Extends <mxGraphLayout> to implement a radial tree algorithm. This
* layout is suitable for graphs that have no cycles (trees). Vertices that are
* not connected to the tree will be ignored by this layout.
*
* Example:
*
* (code)
* var layout = new mxRadialTreeLayout(graph);
* layout.execute(graph.getDefaultParent());
* (end)
*
* Constructor: mxRadialTreeLayout
*
* Constructs a new radial tree layout for the specified graph
*/
function mxRadialTreeLayout(graph)
{
mxCompactTreeLayout.call(this, graph , false);
};
/**
* Extends mxGraphLayout.
*/
mxUtils.extend(mxRadialTreeLayout, mxCompactTreeLayout);
/**
* Variable: angleOffset
*
* The initial offset to compute the angle position.
*/
mxRadialTreeLayout.prototype.angleOffset = 0.5;
/**
* Variable: rootx
*
* The X co-ordinate of the root cell
*/
mxRadialTreeLayout.prototype.rootx = 0;
/**
* Variable: rooty
*
* The Y co-ordinate of the root cell
*/
mxRadialTreeLayout.prototype.rooty = 0;
/**
* Variable: levelDistance
*
* Holds the levelDistance. Default is 120.
*/
mxRadialTreeLayout.prototype.levelDistance = 120;
/**
* Variable: nodeDistance
*
* Holds the nodeDistance. Default is 10.
*/
mxRadialTreeLayout.prototype.nodeDistance = 10;
/**
* Variable: autoRadius
*
* Specifies if the radios should be computed automatically
*/
mxRadialTreeLayout.prototype.autoRadius = false;
/**
* Variable: sortEdges
*
* Specifies if edges should be sorted according to the order of their
* opposite terminal cell in the model.
*/
mxRadialTreeLayout.prototype.sortEdges = false;
/**
* Variable: rowMinX
*
* Array of leftmost x coordinate of each row
*/
mxRadialTreeLayout.prototype.rowMinX = [];
/**
* Variable: rowMaxX
*
* Array of rightmost x coordinate of each row
*/
mxRadialTreeLayout.prototype.rowMaxX = [];
/**
* Variable: rowMinCenX
*
* Array of x coordinate of leftmost vertex of each row
*/
mxRadialTreeLayout.prototype.rowMinCenX = [];
/**
* Variable: rowMaxCenX
*
* Array of x coordinate of rightmost vertex of each row
*/
mxRadialTreeLayout.prototype.rowMaxCenX = [];
/**
* Variable: rowRadi
*
* Array of y deltas of each row behind root vertex, also the radius in the tree
*/
mxRadialTreeLayout.prototype.rowRadi = [];
/**
* Variable: row
*
* Array of vertices on each row
*/
mxRadialTreeLayout.prototype.row = [];
/**
* Function: isVertexIgnored
*
* Returns a boolean indicating if the given <mxCell> should be ignored as a
* vertex. This returns true if the cell has no connections.
*
* Parameters:
*
* vertex - <mxCell> whose ignored state should be returned.
*/
mxRadialTreeLayout.prototype.isVertexIgnored = function(vertex)
{
return mxGraphLayout.prototype.isVertexIgnored.apply(this, arguments) ||
this.graph.getConnections(vertex).length == 0;
};
/**
* Function: execute
*
* Implements <mxGraphLayout.execute>.
*
* If the parent has any connected edges, then it is used as the root of
* the tree. Else, <mxGraph.findTreeRoots> will be used to find a suitable
* root node within the set of children of the given parent.
*
* Parameters:
*
* parent - <mxCell> whose children should be laid out.
* root - Optional <mxCell> that will be used as the root of the tree.
*/
mxRadialTreeLayout.prototype.execute = function(parent, root)
{
this.parent = parent;
this.useBoundingBox = false;
this.edgeRouting = false;
//this.horizontal = false;
mxCompactTreeLayout.prototype.execute.apply(this, arguments);
var bounds = null;
var rootBounds = this.getVertexBounds(this.root);
this.centerX = rootBounds.x + rootBounds.width / 2;
this.centerY = rootBounds.y + rootBounds.height / 2;
// Calculate the bounds of the involved vertices directly from the values set in the compact tree
for (var vertex in this.visited)
{
var vertexBounds = this.getVertexBounds(this.visited[vertex]);
bounds = (bounds != null) ? bounds : vertexBounds.clone();
bounds.add(vertexBounds);
}
this.calcRowDims([this.node], 0);
var maxLeftGrad = 0;
var maxRightGrad = 0;
// Find the steepest left and right gradients
for (var i = 0; i < this.row.length; i++)
{
var leftGrad = (this.centerX - this.rowMinX[i] - this.nodeDistance) / this.rowRadi[i];
var rightGrad = (this.rowMaxX[i] - this.centerX - this.nodeDistance) / this.rowRadi[i];
maxLeftGrad = Math.max (maxLeftGrad, leftGrad);
maxRightGrad = Math.max (maxRightGrad, rightGrad);
}
// Extend out row so they meet the maximum gradient and convert to polar co-ords
for (var i = 0; i < this.row.length; i++)
{
var xLeftLimit = this.centerX - this.nodeDistance - maxLeftGrad * this.rowRadi[i];
var xRightLimit = this.centerX + this.nodeDistance + maxRightGrad * this.rowRadi[i];
var fullWidth = xRightLimit - xLeftLimit;
for (var j = 0; j < this.row[i].length; j ++)
{
var row = this.row[i];
var node = row[j];
var vertexBounds = this.getVertexBounds(node.cell);
var xProportion = (vertexBounds.x + vertexBounds.width / 2 - xLeftLimit) / (fullWidth);
var theta = 2 * Math.PI * xProportion;
node.theta = theta;
}
}
// Post-process from outside inwards to try to align parents with children
for (var i = this.row.length - 2; i >= 0; i--)
{
var row = this.row[i];
for (var j = 0; j < row.length; j++)
{
var node = row[j];
var child = node.child;
var counter = 0;
var totalTheta = 0;
while (child != null)
{
totalTheta += child.theta;
counter++;
child = child.next;
}
if (counter > 0)
{
var averTheta = totalTheta / counter;
if (averTheta > node.theta && j < row.length - 1)
{
var nextTheta = row[j+1].theta;
node.theta = Math.min (averTheta, nextTheta - Math.PI/10);
}
else if (averTheta < node.theta && j > 0 )
{
var lastTheta = row[j-1].theta;
node.theta = Math.max (averTheta, lastTheta + Math.PI/10);
}
}
}
}
// Set locations
for (var i = 0; i < this.row.length; i++)
{
for (var j = 0; j < this.row[i].length; j ++)
{
var row = this.row[i];
var node = row[j];
var vertexBounds = this.getVertexBounds(node.cell);
this.setVertexLocation(node.cell,
this.centerX - vertexBounds.width / 2 + this.rowRadi[i] * Math.cos(node.theta),
this.centerY - vertexBounds.height / 2 + this.rowRadi[i] * Math.sin(node.theta));
}
}
};
/**
* Function: calcRowDims
*
* Recursive function to calculate the dimensions of each row
*
* Parameters:
*
* row - Array of internal nodes, the children of which are to be processed.
* rowNum - Integer indicating which row is being processed.
*/
mxRadialTreeLayout.prototype.calcRowDims = function(row, rowNum)
{
if (row == null || row.length == 0)
{
return;
}
// Place root's children proportionally around the first level
this.rowMinX[rowNum] = this.centerX;
this.rowMaxX[rowNum] = this.centerX;
this.rowMinCenX[rowNum] = this.centerX;
this.rowMaxCenX[rowNum] = this.centerX;
this.row[rowNum] = [];
var rowHasChildren = false;
for (var i = 0; i < row.length; i++)
{
var child = row[i] != null ? row[i].child : null;
while (child != null)
{
var cell = child.cell;
var vertexBounds = this.getVertexBounds(cell);
this.rowMinX[rowNum] = Math.min(vertexBounds.x, this.rowMinX[rowNum]);
this.rowMaxX[rowNum] = Math.max(vertexBounds.x + vertexBounds.width, this.rowMaxX[rowNum]);
this.rowMinCenX[rowNum] = Math.min(vertexBounds.x + vertexBounds.width / 2, this.rowMinCenX[rowNum]);
this.rowMaxCenX[rowNum] = Math.max(vertexBounds.x + vertexBounds.width / 2, this.rowMaxCenX[rowNum]);
this.rowRadi[rowNum] = vertexBounds.y - this.getVertexBounds(this.root).y;
if (child.child != null)
{
rowHasChildren = true;
}
this.row[rowNum].push(child);
child = child.next;
}
}
if (rowHasChildren)
{
this.calcRowDims(this.row[rowNum], rowNum + 1);
}
};