-
Notifications
You must be signed in to change notification settings - Fork 588
/
layout.js
405 lines (365 loc) · 11.9 KB
/
layout.js
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
"use strict";
let acyclic = require("./acyclic");
let normalize = require("./normalize");
let rank = require("./rank");
let normalizeRanks = require("./util").normalizeRanks;
let parentDummyChains = require("./parent-dummy-chains");
let removeEmptyRanks = require("./util").removeEmptyRanks;
let nestingGraph = require("./nesting-graph");
let addBorderSegments = require("./add-border-segments");
let coordinateSystem = require("./coordinate-system");
let order = require("./order");
let position = require("./position");
let util = require("./util");
let Graph = require("@dagrejs/graphlib").Graph;
module.exports = layout;
function layout(g, opts) {
let time = opts && opts.debugTiming ? util.time : util.notime;
time("layout", () => {
let layoutGraph =
time(" buildLayoutGraph", () => buildLayoutGraph(g));
time(" runLayout", () => runLayout(layoutGraph, time));
time(" updateInputGraph", () => updateInputGraph(g, layoutGraph));
});
}
function runLayout(g, time) {
time(" makeSpaceForEdgeLabels", () => makeSpaceForEdgeLabels(g));
time(" removeSelfEdges", () => removeSelfEdges(g));
time(" acyclic", () => acyclic.run(g));
time(" nestingGraph.run", () => nestingGraph.run(g));
time(" rank", () => rank(util.asNonCompoundGraph(g)));
time(" injectEdgeLabelProxies", () => injectEdgeLabelProxies(g));
time(" removeEmptyRanks", () => removeEmptyRanks(g));
time(" nestingGraph.cleanup", () => nestingGraph.cleanup(g));
time(" normalizeRanks", () => normalizeRanks(g));
time(" assignRankMinMax", () => assignRankMinMax(g));
time(" removeEdgeLabelProxies", () => removeEdgeLabelProxies(g));
time(" normalize.run", () => normalize.run(g));
time(" parentDummyChains", () => parentDummyChains(g));
time(" addBorderSegments", () => addBorderSegments(g));
time(" order", () => order(g));
time(" insertSelfEdges", () => insertSelfEdges(g));
time(" adjustCoordinateSystem", () => coordinateSystem.adjust(g));
time(" position", () => position(g));
time(" positionSelfEdges", () => positionSelfEdges(g));
time(" removeBorderNodes", () => removeBorderNodes(g));
time(" normalize.undo", () => normalize.undo(g));
time(" fixupEdgeLabelCoords", () => fixupEdgeLabelCoords(g));
time(" undoCoordinateSystem", () => coordinateSystem.undo(g));
time(" translateGraph", () => translateGraph(g));
time(" assignNodeIntersects", () => assignNodeIntersects(g));
time(" reversePoints", () => reversePointsForReversedEdges(g));
time(" acyclic.undo", () => acyclic.undo(g));
}
/*
* Copies final layout information from the layout graph back to the input
* graph. This process only copies whitelisted attributes from the layout graph
* to the input graph, so it serves as a good place to determine what
* attributes can influence layout.
*/
function updateInputGraph(inputGraph, layoutGraph) {
inputGraph.nodes().forEach(v => {
let inputLabel = inputGraph.node(v);
let layoutLabel = layoutGraph.node(v);
if (inputLabel) {
inputLabel.x = layoutLabel.x;
inputLabel.y = layoutLabel.y;
inputLabel.rank = layoutLabel.rank;
if (layoutGraph.children(v).length) {
inputLabel.width = layoutLabel.width;
inputLabel.height = layoutLabel.height;
}
}
});
inputGraph.edges().forEach(e => {
let inputLabel = inputGraph.edge(e);
let layoutLabel = layoutGraph.edge(e);
inputLabel.points = layoutLabel.points;
if (layoutLabel.hasOwnProperty("x")) {
inputLabel.x = layoutLabel.x;
inputLabel.y = layoutLabel.y;
}
});
inputGraph.graph().width = layoutGraph.graph().width;
inputGraph.graph().height = layoutGraph.graph().height;
}
let graphNumAttrs = ["nodesep", "edgesep", "ranksep", "marginx", "marginy"];
let graphDefaults = { ranksep: 50, edgesep: 20, nodesep: 50, rankdir: "tb" };
let graphAttrs = ["acyclicer", "ranker", "rankdir", "align"];
let nodeNumAttrs = ["width", "height"];
let nodeDefaults = { width: 0, height: 0 };
let edgeNumAttrs = ["minlen", "weight", "width", "height", "labeloffset"];
let edgeDefaults = {
minlen: 1, weight: 1, width: 0, height: 0,
labeloffset: 10, labelpos: "r"
};
let edgeAttrs = ["labelpos"];
/*
* Constructs a new graph from the input graph, which can be used for layout.
* This process copies only whitelisted attributes from the input graph to the
* layout graph. Thus this function serves as a good place to determine what
* attributes can influence layout.
*/
function buildLayoutGraph(inputGraph) {
let g = new Graph({ multigraph: true, compound: true });
let graph = canonicalize(inputGraph.graph());
g.setGraph(Object.assign({},
graphDefaults,
selectNumberAttrs(graph, graphNumAttrs),
util.pick(graph, graphAttrs)));
inputGraph.nodes().forEach(v => {
let node = canonicalize(inputGraph.node(v));
const newNode = selectNumberAttrs(node, nodeNumAttrs);
Object.keys(nodeDefaults).forEach(k => {
if (newNode[k] === undefined) {
newNode[k] = nodeDefaults[k];
}
});
g.setNode(v, newNode);
g.setParent(v, inputGraph.parent(v));
});
inputGraph.edges().forEach(e => {
let edge = canonicalize(inputGraph.edge(e));
g.setEdge(e, Object.assign({},
edgeDefaults,
selectNumberAttrs(edge, edgeNumAttrs),
util.pick(edge, edgeAttrs)));
});
return g;
}
/*
* This idea comes from the Gansner paper: to account for edge labels in our
* layout we split each rank in half by doubling minlen and halving ranksep.
* Then we can place labels at these mid-points between nodes.
*
* We also add some minimal padding to the width to push the label for the edge
* away from the edge itself a bit.
*/
function makeSpaceForEdgeLabels(g) {
let graph = g.graph();
graph.ranksep /= 2;
g.edges().forEach(e => {
let edge = g.edge(e);
edge.minlen *= 2;
if (edge.labelpos.toLowerCase() !== "c") {
if (graph.rankdir === "TB" || graph.rankdir === "BT") {
edge.width += edge.labeloffset;
} else {
edge.height += edge.labeloffset;
}
}
});
}
/*
* Creates temporary dummy nodes that capture the rank in which each edge's
* label is going to, if it has one of non-zero width and height. We do this
* so that we can safely remove empty ranks while preserving balance for the
* label's position.
*/
function injectEdgeLabelProxies(g) {
g.edges().forEach(e => {
let edge = g.edge(e);
if (edge.width && edge.height) {
let v = g.node(e.v);
let w = g.node(e.w);
let label = { rank: (w.rank - v.rank) / 2 + v.rank, e: e };
util.addDummyNode(g, "edge-proxy", label, "_ep");
}
});
}
function assignRankMinMax(g) {
let maxRank = 0;
g.nodes().forEach(v => {
let node = g.node(v);
if (node.borderTop) {
node.minRank = g.node(node.borderTop).rank;
node.maxRank = g.node(node.borderBottom).rank;
maxRank = Math.max(maxRank, node.maxRank);
}
});
g.graph().maxRank = maxRank;
}
function removeEdgeLabelProxies(g) {
g.nodes().forEach(v => {
let node = g.node(v);
if (node.dummy === "edge-proxy") {
g.edge(node.e).labelRank = node.rank;
g.removeNode(v);
}
});
}
function translateGraph(g) {
let minX = Number.POSITIVE_INFINITY;
let maxX = 0;
let minY = Number.POSITIVE_INFINITY;
let maxY = 0;
let graphLabel = g.graph();
let marginX = graphLabel.marginx || 0;
let marginY = graphLabel.marginy || 0;
function getExtremes(attrs) {
let x = attrs.x;
let y = attrs.y;
let w = attrs.width;
let h = attrs.height;
minX = Math.min(minX, x - w / 2);
maxX = Math.max(maxX, x + w / 2);
minY = Math.min(minY, y - h / 2);
maxY = Math.max(maxY, y + h / 2);
}
g.nodes().forEach(v => getExtremes(g.node(v)));
g.edges().forEach(e => {
let edge = g.edge(e);
if (edge.hasOwnProperty("x")) {
getExtremes(edge);
}
});
minX -= marginX;
minY -= marginY;
g.nodes().forEach(v => {
let node = g.node(v);
node.x -= minX;
node.y -= minY;
});
g.edges().forEach(e => {
let edge = g.edge(e);
edge.points.forEach(p => {
p.x -= minX;
p.y -= minY;
});
if (edge.hasOwnProperty("x")) { edge.x -= minX; }
if (edge.hasOwnProperty("y")) { edge.y -= minY; }
});
graphLabel.width = maxX - minX + marginX;
graphLabel.height = maxY - minY + marginY;
}
function assignNodeIntersects(g) {
g.edges().forEach(e => {
let edge = g.edge(e);
let nodeV = g.node(e.v);
let nodeW = g.node(e.w);
let p1, p2;
if (!edge.points) {
edge.points = [];
p1 = nodeW;
p2 = nodeV;
} else {
p1 = edge.points[0];
p2 = edge.points[edge.points.length - 1];
}
edge.points.unshift(util.intersectRect(nodeV, p1));
edge.points.push(util.intersectRect(nodeW, p2));
});
}
function fixupEdgeLabelCoords(g) {
g.edges().forEach(e => {
let edge = g.edge(e);
if (edge.hasOwnProperty("x")) {
if (edge.labelpos === "l" || edge.labelpos === "r") {
edge.width -= edge.labeloffset;
}
switch (edge.labelpos) {
case "l": edge.x -= edge.width / 2 + edge.labeloffset; break;
case "r": edge.x += edge.width / 2 + edge.labeloffset; break;
}
}
});
}
function reversePointsForReversedEdges(g) {
g.edges().forEach(e => {
let edge = g.edge(e);
if (edge.reversed) {
edge.points.reverse();
}
});
}
function removeBorderNodes(g) {
g.nodes().forEach(v => {
if (g.children(v).length) {
let node = g.node(v);
let t = g.node(node.borderTop);
let b = g.node(node.borderBottom);
let l = g.node(node.borderLeft[node.borderLeft.length - 1]);
let r = g.node(node.borderRight[node.borderRight.length - 1]);
node.width = Math.abs(r.x - l.x);
node.height = Math.abs(b.y - t.y);
node.x = l.x + node.width / 2;
node.y = t.y + node.height / 2;
}
});
g.nodes().forEach(v => {
if (g.node(v).dummy === "border") {
g.removeNode(v);
}
});
}
function removeSelfEdges(g) {
g.edges().forEach(e => {
if (e.v === e.w) {
var node = g.node(e.v);
if (!node.selfEdges) {
node.selfEdges = [];
}
node.selfEdges.push({ e: e, label: g.edge(e) });
g.removeEdge(e);
}
});
}
function insertSelfEdges(g) {
var layers = util.buildLayerMatrix(g);
layers.forEach(layer => {
var orderShift = 0;
layer.forEach((v, i) => {
var node = g.node(v);
node.order = i + orderShift;
(node.selfEdges || []).forEach(selfEdge => {
util.addDummyNode(g, "selfedge", {
width: selfEdge.label.width,
height: selfEdge.label.height,
rank: node.rank,
order: i + (++orderShift),
e: selfEdge.e,
label: selfEdge.label
}, "_se");
});
delete node.selfEdges;
});
});
}
function positionSelfEdges(g) {
g.nodes().forEach(v => {
var node = g.node(v);
if (node.dummy === "selfedge") {
var selfNode = g.node(node.e.v);
var x = selfNode.x + selfNode.width / 2;
var y = selfNode.y;
var dx = node.x - x;
var dy = selfNode.height / 2;
g.setEdge(node.e, node.label);
g.removeNode(v);
node.label.points = [
{ x: x + 2 * dx / 3, y: y - dy },
{ x: x + 5 * dx / 6, y: y - dy },
{ x: x + dx , y: y },
{ x: x + 5 * dx / 6, y: y + dy },
{ x: x + 2 * dx / 3, y: y + dy }
];
node.label.x = node.x;
node.label.y = node.y;
}
});
}
function selectNumberAttrs(obj, attrs) {
return util.mapValues(util.pick(obj, attrs), Number);
}
function canonicalize(attrs) {
var newAttrs = {};
if (attrs) {
Object.entries(attrs).forEach(([k, v]) => {
if (typeof k === "string") {
k = k.toLowerCase();
}
newAttrs[k] = v;
});
}
return newAttrs;
}