whychdw
2019-09-09 c0b9ce437d409b89ca0e5388344ca9c5a56d70a4
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/* *
 *
 *  Networkgraph series
 *
 *  (c) 2010-2019 Paweł Fus
 *
 *  License: www.highcharts.com/license
 *
 * */
'use strict';
 
var _Globals = require('../../parts/Globals.js');
 
var _Globals2 = _interopRequireDefault(_Globals);
 
var _Utilities = require('../../parts/Utilities.js');
 
var _Utilities2 = _interopRequireDefault(_Utilities);
 
require('integrations.js');
 
require('QuadTree.js');
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
var defined = _Utilities2.default.defined;
 
var pick = _Globals2.default.pick,
    addEvent = _Globals2.default.addEvent,
    Chart = _Globals2.default.Chart;
/* eslint-disable no-invalid-this, valid-jsdoc */
_Globals2.default.layouts = {
    'reingold-fruchterman': function reingoldFruchterman() {}
};
_Globals2.default.extend(
/**
 * Reingold-Fruchterman algorithm from
 * "Graph Drawing by Force-directed Placement" paper.
 * @private
 */
_Globals2.default.layouts['reingold-fruchterman'].prototype, {
    init: function init(options) {
        this.options = options;
        this.nodes = [];
        this.links = [];
        this.series = [];
        this.box = {
            x: 0,
            y: 0,
            width: 0,
            height: 0
        };
        this.setInitialRendering(true);
        this.integration = _Globals2.default.networkgraphIntegrations[options.integration];
        this.attractiveForce = pick(options.attractiveForce, this.integration.attractiveForceFunction);
        this.repulsiveForce = pick(options.repulsiveForce, this.integration.repulsiveForceFunction);
        this.approximation = options.approximation;
    },
    start: function start() {
        var layout = this,
            series = this.series,
            options = this.options;
        layout.currentStep = 0;
        layout.forces = series[0] && series[0].forces || [];
        if (layout.initialRendering) {
            layout.initPositions();
            // Render elements in initial positions:
            series.forEach(function (s) {
                s.render();
            });
        }
        layout.setK();
        layout.resetSimulation(options);
        if (options.enableSimulation) {
            layout.step();
        }
    },
    step: function step() {
        var layout = this,
            series = this.series,
            options = this.options;
        // Algorithm:
        layout.currentStep++;
        if (layout.approximation === 'barnes-hut') {
            layout.createQuadTree();
            layout.quadTree.calculateMassAndCenter();
        }
        layout.forces.forEach(function (forceName) {
            layout[forceName + 'Forces'](layout.temperature);
        });
        // Limit to the plotting area and cool down:
        layout.applyLimits(layout.temperature);
        // Cool down the system:
        layout.temperature = layout.coolDown(layout.startTemperature, layout.diffTemperature, layout.currentStep);
        layout.prevSystemTemperature = layout.systemTemperature;
        layout.systemTemperature = layout.getSystemTemperature();
        if (options.enableSimulation) {
            series.forEach(function (s) {
                // Chart could be destroyed during the simulation
                if (s.chart) {
                    s.render();
                }
            });
            if (layout.maxIterations-- && isFinite(layout.temperature) && !layout.isStable()) {
                if (layout.simulation) {
                    _Globals2.default.win.cancelAnimationFrame(layout.simulation);
                }
                layout.simulation = _Globals2.default.win.requestAnimationFrame(function () {
                    layout.step();
                });
            } else {
                layout.simulation = false;
            }
        }
    },
    stop: function stop() {
        if (this.simulation) {
            _Globals2.default.win.cancelAnimationFrame(this.simulation);
        }
    },
    setArea: function setArea(x, y, w, h) {
        this.box = {
            left: x,
            top: y,
            width: w,
            height: h
        };
    },
    setK: function setK() {
        // Optimal distance between nodes,
        // available space around the node:
        this.k = this.options.linkLength || this.integration.getK(this);
    },
    addNodes: function addNodes(nodes) {
        nodes.forEach(function (node) {
            if (this.nodes.indexOf(node) === -1) {
                this.nodes.push(node);
            }
        }, this);
    },
    removeNode: function removeNode(node) {
        var index = this.nodes.indexOf(node);
        if (index !== -1) {
            this.nodes.splice(index, 1);
        }
    },
    removeLink: function removeLink(link) {
        var index = this.links.indexOf(link);
        if (index !== -1) {
            this.links.splice(index, 1);
        }
    },
    addLinks: function addLinks(links) {
        links.forEach(function (link) {
            if (this.links.indexOf(link) === -1) {
                this.links.push(link);
            }
        }, this);
    },
    addSeries: function addSeries(series) {
        if (this.series.indexOf(series) === -1) {
            this.series.push(series);
        }
    },
    clear: function clear() {
        this.nodes.length = 0;
        this.links.length = 0;
        this.series.length = 0;
        this.resetSimulation();
    },
    resetSimulation: function resetSimulation() {
        this.forcedStop = false;
        this.systemTemperature = 0;
        this.setMaxIterations();
        this.setTemperature();
        this.setDiffTemperature();
    },
    setMaxIterations: function setMaxIterations(maxIterations) {
        this.maxIterations = pick(maxIterations, this.options.maxIterations);
    },
    setTemperature: function setTemperature() {
        this.temperature = this.startTemperature = Math.sqrt(this.nodes.length);
    },
    setDiffTemperature: function setDiffTemperature() {
        this.diffTemperature = this.startTemperature / (this.options.maxIterations + 1);
    },
    setInitialRendering: function setInitialRendering(enable) {
        this.initialRendering = enable;
    },
    createQuadTree: function createQuadTree() {
        this.quadTree = new _Globals2.default.QuadTree(this.box.left, this.box.top, this.box.width, this.box.height);
        this.quadTree.insertNodes(this.nodes);
    },
    initPositions: function initPositions() {
        var initialPositions = this.options.initialPositions;
        if (_Globals2.default.isFunction(initialPositions)) {
            initialPositions.call(this);
            this.nodes.forEach(function (node) {
                if (!defined(node.prevX)) {
                    node.prevX = node.plotX;
                }
                if (!defined(node.prevY)) {
                    node.prevY = node.plotY;
                }
                node.dispX = 0;
                node.dispY = 0;
            });
        } else if (initialPositions === 'circle') {
            this.setCircularPositions();
        } else {
            this.setRandomPositions();
        }
    },
    setCircularPositions: function setCircularPositions() {
        var box = this.box,
            nodes = this.nodes,
            nodesLength = nodes.length + 1,
            angle = 2 * Math.PI / nodesLength,
            rootNodes = nodes.filter(function (node) {
            return node.linksTo.length === 0;
        }),
            sortedNodes = [],
            visitedNodes = {},
            radius = this.options.initialPositionRadius;
        /**
         * @private
         */
        function addToNodes(node) {
            node.linksFrom.forEach(function (link) {
                if (!visitedNodes[link.toNode.id]) {
                    visitedNodes[link.toNode.id] = true;
                    sortedNodes.push(link.toNode);
                    addToNodes(link.toNode);
                }
            });
        }
        // Start with identified root nodes an sort the nodes by their
        // hierarchy. In trees, this ensures that branches don't cross
        // eachother.
        rootNodes.forEach(function (rootNode) {
            sortedNodes.push(rootNode);
            addToNodes(rootNode);
        });
        // Cyclic tree, no root node found
        if (!sortedNodes.length) {
            sortedNodes = nodes;
            // Dangling, cyclic trees
        } else {
            nodes.forEach(function (node) {
                if (sortedNodes.indexOf(node) === -1) {
                    sortedNodes.push(node);
                }
            });
        }
        // Initial positions are laid out along a small circle, appearing
        // as a cluster in the middle
        sortedNodes.forEach(function (node, index) {
            node.plotX = node.prevX = pick(node.plotX, box.width / 2 + radius * Math.cos(index * angle));
            node.plotY = node.prevY = pick(node.plotY, box.height / 2 + radius * Math.sin(index * angle));
            node.dispX = 0;
            node.dispY = 0;
        });
    },
    setRandomPositions: function setRandomPositions() {
        var box = this.box,
            nodes = this.nodes,
            nodesLength = nodes.length + 1;
        /**
         * Return a repeatable, quasi-random number based on an integer
         * input. For the initial positions
         * @private
         */
        function unrandom(n) {
            var rand = n * n / Math.PI;
            rand = rand - Math.floor(rand);
            return rand;
        }
        // Initial positions:
        nodes.forEach(function (node, index) {
            node.plotX = node.prevX = pick(node.plotX, box.width * unrandom(index));
            node.plotY = node.prevY = pick(node.plotY, box.height * unrandom(nodesLength + index));
            node.dispX = 0;
            node.dispY = 0;
        });
    },
    force: function force(name) {
        this.integration[name].apply(this, Array.prototype.slice.call(arguments, 1));
    },
    barycenterForces: function barycenterForces() {
        this.getBarycenter();
        this.force('barycenter');
    },
    getBarycenter: function getBarycenter() {
        var systemMass = 0,
            cx = 0,
            cy = 0;
        this.nodes.forEach(function (node) {
            cx += node.plotX * node.mass;
            cy += node.plotY * node.mass;
            systemMass += node.mass;
        });
        this.barycenter = {
            x: cx,
            y: cy,
            xFactor: cx / systemMass,
            yFactor: cy / systemMass
        };
        return this.barycenter;
    },
    barnesHutApproximation: function barnesHutApproximation(node, quadNode) {
        var layout = this,
            distanceXY = layout.getDistXY(node, quadNode),
            distanceR = layout.vectorLength(distanceXY),
            goDeeper,
            force;
        if (node !== quadNode && distanceR !== 0) {
            if (quadNode.isInternal) {
                // Internal node:
                if (quadNode.boxSize / distanceR < layout.options.theta && distanceR !== 0) {
                    // Treat as an external node:
                    force = layout.repulsiveForce(distanceR, layout.k);
                    layout.force('repulsive', node, force * quadNode.mass, distanceXY, distanceR);
                    goDeeper = false;
                } else {
                    // Go deeper:
                    goDeeper = true;
                }
            } else {
                // External node, direct force:
                force = layout.repulsiveForce(distanceR, layout.k);
                layout.force('repulsive', node, force * quadNode.mass, distanceXY, distanceR);
            }
        }
        return goDeeper;
    },
    repulsiveForces: function repulsiveForces() {
        var layout = this;
        if (layout.approximation === 'barnes-hut') {
            layout.nodes.forEach(function (node) {
                layout.quadTree.visitNodeRecursive(null, function (quadNode) {
                    return layout.barnesHutApproximation(node, quadNode);
                });
            });
        } else {
            layout.nodes.forEach(function (node) {
                layout.nodes.forEach(function (repNode) {
                    var force, distanceR, distanceXY;
                    if (
                    // Node can not repulse itself:
                    node !== repNode &&
                    // Only close nodes affect each other:
                    /* layout.getDistR(node, repNode) < 2 * k && */
                    // Not dragged:
                    !node.fixedPosition) {
                        distanceXY = layout.getDistXY(node, repNode);
                        distanceR = layout.vectorLength(distanceXY);
                        if (distanceR !== 0) {
                            force = layout.repulsiveForce(distanceR, layout.k);
                            layout.force('repulsive', node, force * repNode.mass, distanceXY, distanceR);
                        }
                    }
                });
            });
        }
    },
    attractiveForces: function attractiveForces() {
        var layout = this,
            distanceXY,
            distanceR,
            force;
        layout.links.forEach(function (link) {
            if (link.fromNode && link.toNode) {
                distanceXY = layout.getDistXY(link.fromNode, link.toNode);
                distanceR = layout.vectorLength(distanceXY);
                if (distanceR !== 0) {
                    force = layout.attractiveForce(distanceR, layout.k);
                    layout.force('attractive', link, force, distanceXY, distanceR);
                }
            }
        });
    },
    applyLimits: function applyLimits() {
        var layout = this,
            nodes = layout.nodes;
        nodes.forEach(function (node) {
            if (node.fixedPosition) {
                return;
            }
            layout.integration.integrate(layout, node);
            layout.applyLimitBox(node, layout.box);
            // Reset displacement:
            node.dispX = 0;
            node.dispY = 0;
        });
    },
    /**
     * External box that nodes should fall. When hitting an edge, node
     * should stop or bounce.
     * @private
     */
    applyLimitBox: function applyLimitBox(node, box) {
        var radius = node.radius;
        /*
        TO DO: Consider elastic collision instead of stopping.
        o' means end position when hitting plotting area edge:
         - "inelastic":
        o
         \
        ______
        |  o'
        |   \
        |    \
         - "elastic"/"bounced":
        o
         \
        ______
        |  ^
        | / \
        |o'  \
         Euler sample:
        if (plotX < 0) {
            plotX = 0;
            dispX *= -1;
        }
         if (plotX > box.width) {
            plotX = box.width;
            dispX *= -1;
        }
         */
        // Limit X-coordinates:
        node.plotX = Math.max(Math.min(node.plotX, box.width - radius), box.left + radius);
        // Limit Y-coordinates:
        node.plotY = Math.max(Math.min(node.plotY, box.height - radius), box.top + radius);
    },
    /**
     * From "A comparison of simulated annealing cooling strategies" by
     * Nourani and Andresen work.
     * @private
     */
    coolDown: function coolDown(temperature, temperatureStep, currentStep) {
        // Logarithmic:
        /*
        return Math.sqrt(this.nodes.length) -
            Math.log(
                currentStep * layout.diffTemperature
            );
        */
        // Exponential:
        /*
        var alpha = 0.1;
        layout.temperature = Math.sqrt(layout.nodes.length) *
            Math.pow(alpha, layout.diffTemperature);
        */
        // Linear:
        return temperature - temperatureStep * currentStep;
    },
    isStable: function isStable() {
        return Math.abs(this.systemTemperature - this.prevSystemTemperature) < 0.00001 || this.temperature <= 0;
    },
    getSystemTemperature: function getSystemTemperature() {
        return this.nodes.reduce(function (value, node) {
            return value + node.temperature;
        }, 0);
    },
    vectorLength: function vectorLength(vector) {
        return Math.sqrt(vector.x * vector.x + vector.y * vector.y);
    },
    getDistR: function getDistR(nodeA, nodeB) {
        var distance = this.getDistXY(nodeA, nodeB);
        return this.vectorLength(distance);
    },
    getDistXY: function getDistXY(nodeA, nodeB) {
        var xDist = nodeA.plotX - nodeB.plotX,
            yDist = nodeA.plotY - nodeB.plotY;
        return {
            x: xDist,
            y: yDist,
            absX: Math.abs(xDist),
            absY: Math.abs(yDist)
        };
    }
});
/* ************************************************************************** *
 * Multiple series support:
 * ************************************************************************** */
// Clear previous layouts
addEvent(Chart, 'predraw', function () {
    if (this.graphLayoutsLookup) {
        this.graphLayoutsLookup.forEach(function (layout) {
            layout.stop();
        });
    }
});
addEvent(Chart, 'render', function () {
    var systemsStable,
        afterRender = false;
    /**
     * @private
     */
    function layoutStep(layout) {
        if (layout.maxIterations-- && isFinite(layout.temperature) && !layout.isStable() && !layout.options.enableSimulation) {
            // Hook similar to build-in addEvent, but instead of
            // creating whole events logic, use just a function.
            // It's faster which is important for rAF code.
            // Used e.g. in packed-bubble series for bubble radius
            // calculations
            if (layout.beforeStep) {
                layout.beforeStep();
            }
            layout.step();
            systemsStable = false;
            afterRender = true;
        }
    }
    if (this.graphLayoutsLookup) {
        _Globals2.default.setAnimation(false, this);
        // Start simulation
        this.graphLayoutsLookup.forEach(function (layout) {
            layout.start();
        });
        // Just one sync step, to run different layouts similar to
        // async mode.
        while (!systemsStable) {
            systemsStable = true;
            this.graphLayoutsLookup.forEach(layoutStep);
        }
        if (afterRender) {
            this.series.forEach(function (s) {
                if (s && s.layout) {
                    s.render();
                }
            });
        }
    }
});