-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsigma.layout.fruchtermanReingold.js
399 lines (332 loc) · 11.5 KB
/
sigma.layout.fruchtermanReingold.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
;(function(undefined) {
'use strict';
if (typeof sigma === 'undefined')
throw new Error('sigma is not declared');
// Initialize package:
sigma.utils.pkg('sigma.layouts.fruchtermanReingold');
/**
* Sigma Fruchterman-Reingold
* ===============================
*
* Author: Sébastien Heymann @ Linkurious
* Version: 0.1
*/
var settings = {
autoArea: true,
area: 1,
gravity: 10,
speed: 0.1,
iterations: 1000
};
var _instance = {};
/**
* Event emitter Object
* ------------------
*/
var _eventEmitter = {};
/**
* Fruchterman Object
* ------------------
*/
function FruchtermanReingold() {
var self = this;
this.init = function (sigInst, options) {
options = options || {};
// Properties
this.sigInst = sigInst;
this.config = sigma.utils.extend(options, settings);
this.easing = options.easing;
this.duration = options.duration;
if (!sigma.plugins || typeof sigma.plugins.animate === 'undefined') {
throw new Error('sigma.plugins.animate is not declared');
}
// State
this.running = false;
};
/**
* Single layout iteration.
*/
this.atomicGo = function () {
if (!this.running || this.iterCount < 1) return false;
var nodes = this.sigInst.graph.nodes(),
edges = this.sigInst.graph.edges(),
i,
j,
n,
n2,
e,
xDist,
yDist,
dist,
repulsiveF,
nodesCount = nodes.length,
edgesCount = edges.length;
this.config.area = this.config.autoArea ? (nodesCount * nodesCount) : this.config.area;
this.iterCount--;
this.running = (this.iterCount > 0);
var maxDisplace = Math.sqrt(this.config.area) / 10,
k = Math.sqrt(this.config.area / (1 + nodesCount));
for (i = 0; i < nodesCount; i++) {
n = nodes[i];
// Init
if (!n.fr) {
n.fr_x = n.x;
n.fr_y = n.y;
n.fr = {
dx: 0,
dy: 0
};
}
for (j = 0; j < nodesCount; j++) {
n2 = nodes[j];
// Repulsion force
if (n.id != n2.id) {
xDist = n.fr_x - n2.fr_x;
yDist = n.fr_y - n2.fr_y;
dist = Math.sqrt(xDist * xDist + yDist * yDist) + 0.01;
// var dist = Math.sqrt(xDist * xDist + yDist * yDist) - n1.size - n2.size;
if (dist > 0) {
repulsiveF = k * k / dist;
n.fr.dx += xDist / dist * repulsiveF;
n.fr.dy += yDist / dist * repulsiveF;
}
}
}
}
var nSource,
nTarget,
attractiveF;
for (i = 0; i < edgesCount; i++) {
e = edges[i];
// Attraction force
nSource = self.sigInst.graph.nodes(e.source);
nTarget = self.sigInst.graph.nodes(e.target);
xDist = nSource.fr_x - nTarget.fr_x;
yDist = nSource.fr_y - nTarget.fr_y;
dist = Math.sqrt(xDist * xDist + yDist * yDist) + 0.01;
// dist = Math.sqrt(xDist * xDist + yDist * yDist) - nSource.size - nTarget.size;
attractiveF = dist * dist / k;
if (dist > 0) {
nSource.fr.dx -= xDist / dist * attractiveF;
nSource.fr.dy -= yDist / dist * attractiveF;
nTarget.fr.dx += xDist / dist * attractiveF;
nTarget.fr.dy += yDist / dist * attractiveF;
}
}
var d,
gf,
limitedDist;
for (i = 0; i < nodesCount; i++) {
n = nodes[i];
// Gravity
d = Math.sqrt(n.fr_x * n.fr_x + n.fr_y * n.fr_y);
gf = 0.01 * k * self.config.gravity * d;
n.fr.dx -= gf * n.fr_x / d;
n.fr.dy -= gf * n.fr_y / d;
// Speed
n.fr.dx *= self.config.speed;
n.fr.dy *= self.config.speed;
// Apply computed displacement
if (!n.fixed) {
xDist = n.fr.dx;
yDist = n.fr.dy;
dist = Math.sqrt(xDist * xDist + yDist * yDist);
if (dist > 0) {
limitedDist = Math.min(maxDisplace * self.config.speed, dist);
n.fr_x += xDist / dist * limitedDist;
n.fr_y += yDist / dist * limitedDist;
}
}
}
return this.running;
};
this.go = function () {
this.iterCount = this.config.iterations;
while (this.running) {
this.atomicGo();
};
this.stop();
};
this.start = function() {
if (this.running) return;
var nodes = this.sigInst.graph.nodes();
this.running = true;
// Init nodes
for (var i = 0; i < nodes.length; i++) {
nodes[i].fr_x = nodes[i].x;
nodes[i].fr_y = nodes[i].y;
nodes[i].fr = {
dx: 0,
dy: 0
};
}
_eventEmitter[self.sigInst.id].dispatchEvent('start');
this.go();
};
this.stop = function() {
var nodes = this.sigInst.graph.nodes();
this.running = false;
if (this.easing) {
_eventEmitter[self.sigInst.id].dispatchEvent('interpolate');
sigma.plugins.animate(
self.sigInst,
{
x: 'fr_x',
y: 'fr_y'
},
{
easing: self.easing,
onComplete: function() {
self.sigInst.refresh();
for (var i = 0; i < nodes.length; i++) {
nodes[i].fr = null;
nodes[i].fr_x = null;
nodes[i].fr_y = null;
}
_eventEmitter[self.sigInst.id].dispatchEvent('stop');
},
duration: self.duration
}
);
}
else {
// Apply changes
for (var i = 0; i < nodes.length; i++) {
nodes[i].x = nodes[i].fr_x;
nodes[i].y = nodes[i].fr_y;
}
this.sigInst.refresh();
for (var i = 0; i < nodes.length; i++) {
nodes[i].fr = null;
nodes[i].fr_x = null;
nodes[i].fr_y = null;
}
_eventEmitter[self.sigInst.id].dispatchEvent('stop');
}
};
this.kill = function() {
this.sigInst = null;
this.config = null;
this.easing = null;
};
};
/**
* Interface
* ----------
*/
/**
* Configure the layout algorithm.
* Recognized options:
* **********************
* Here is the exhaustive list of every accepted parameters in the settings
* object:
*
* {?boolean} autoArea If `true`, area will be computed as N².
* {?number} area The area of the graph.
* {?number} gravity This force attracts all nodes to the
* center to avoid dispersion of
* disconnected components.
* {?number} speed A greater value increases the
* convergence speed at the cost of precision loss.
* {?number} iterations The number of iterations to perform
* before the layout completes.
* {?(function|string)} easing Either the name of an easing in the
* sigma.utils.easings package or a
* function. If not specified, the
* quadraticInOut easing from this package
* will be used instead.
* {?number} duration The duration of the animation. If not
* specified, the "animationsTime" setting
* value of the sigma instance will be used
* instead.
*
*
* @param {sigma} sigInst The related sigma instance.
* @param {object} config The optional configuration object.
*
* @return {sigma.classes.dispatcher} Returns an event emitter.
*/
sigma.layouts.fruchtermanReingold.configure = function(sigInst, config) {
if (!sigInst) throw new Error('Missing argument: "sigInst"');
if (!config) throw new Error('Missing argument: "config"');
// Create instance if undefined
if (!_instance[sigInst.id]) {
_instance[sigInst.id] = new FruchtermanReingold();
_eventEmitter[sigInst.id] = {};
sigma.classes.dispatcher.extend(_eventEmitter[sigInst.id]);
// Binding on kill to clear the references
sigInst.bind('kill', function() {
_instance[sigInst.id].kill();
_instance[sigInst.id] = null;
_eventEmitter[sigInst.id] = null;
});
}
_instance[sigInst.id].init(sigInst, config);
return _eventEmitter[sigInst.id];
};
/**
* Start the layout algorithm. It will use the existing configuration if no
* new configuration is passed.
* Recognized options:
* **********************
* Here is the exhaustive list of every accepted parameters in the settings
* object:
*
* {?boolean} autoArea If `true`, area will be computed as N².
* {?number} area The area of the graph.
* {?number} gravity This force attracts all nodes to the
* center to avoid dispersion of
* disconnected components.
* {?number} speed A greater value increases the
* convergence speed at the cost of precision loss.
* {?number} iterations The number of iterations to perform
* before the layout completes.
* {?(function|string)} easing Either the name of an easing in the
* sigma.utils.easings package or a
* function. If not specified, the
* quadraticInOut easing from this package
* will be used instead.
* {?number} duration The duration of the animation. If not
* specified, the "animationsTime" setting
* value of the sigma instance will be used
* instead.
*
*
* @param {sigma} sigInst The related sigma instance.
* @param {?object} config The optional configuration object.
*
* @return {sigma.classes.dispatcher} Returns an event emitter.
*/
sigma.layouts.fruchtermanReingold.start = function(sigInst, config) {
if (!sigInst) throw new Error('Missing argument: "sigInst"');
if (config) {
this.configure(sigInst, config);
}
_instance[sigInst.id].start();
return _eventEmitter[sigInst.id];
};
/**
* Returns true if the layout has started and is not completed.
*
* @param {sigma} sigInst The related sigma instance.
*
* @return {boolean}
*/
sigma.layouts.fruchtermanReingold.isRunning = function(sigInst) {
if (!sigInst) throw new Error('Missing argument: "sigInst"');
return !!_instance[sigInst.id] && _instance[sigInst.id].running;
};
/**
* Returns the number of iterations done divided by the total number of
* iterations to perform.
*
* @param {sigma} sigInst The related sigma instance.
*
* @return {number} A value between 0 and 1.
*/
sigma.layouts.fruchtermanReingold.progress = function(sigInst) {
if (!sigInst) throw new Error('Missing argument: "sigInst"');
return (_instance[sigInst.id].config.iterations - _instance[sigInst.id].iterCount) /
_instance[sigInst.id].config.iterations;
};
}).call(this);