chore: version inicial del codigo fuente del LMS Estafeta
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
/* *
|
||||
*
|
||||
* Networkgraph series
|
||||
*
|
||||
* (c) 2010-2020 Paweł Fus
|
||||
*
|
||||
* License: www.highcharts.com/license
|
||||
*
|
||||
* !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
|
||||
*
|
||||
* */
|
||||
import Chart from '../../Core/Chart/Chart.js';
|
||||
import H from '../../Core/Globals.js';
|
||||
import U from '../../Core/Utilities.js';
|
||||
var addEvent = U.addEvent;
|
||||
/* eslint-disable no-invalid-this, valid-jsdoc */
|
||||
H.dragNodesMixin = {
|
||||
/**
|
||||
* Mouse down action, initializing drag&drop mode.
|
||||
*
|
||||
* @private
|
||||
* @param {Highcharts.Point} point The point that event occured.
|
||||
* @param {Highcharts.PointerEventObject} event Browser event, before normalization.
|
||||
* @return {void}
|
||||
*/
|
||||
onMouseDown: function (point, event) {
|
||||
var normalizedEvent = this.chart.pointer.normalize(event);
|
||||
point.fixedPosition = {
|
||||
chartX: normalizedEvent.chartX,
|
||||
chartY: normalizedEvent.chartY,
|
||||
plotX: point.plotX,
|
||||
plotY: point.plotY
|
||||
};
|
||||
point.inDragMode = true;
|
||||
},
|
||||
/**
|
||||
* Mouse move action during drag&drop.
|
||||
*
|
||||
* @private
|
||||
*
|
||||
* @param {global.Event} event Browser event, before normalization.
|
||||
* @param {Highcharts.Point} point The point that event occured.
|
||||
*
|
||||
* @return {void}
|
||||
*/
|
||||
onMouseMove: function (point, event) {
|
||||
if (point.fixedPosition && point.inDragMode) {
|
||||
var series = this, chart = series.chart, normalizedEvent = chart.pointer.normalize(event), diffX = point.fixedPosition.chartX - normalizedEvent.chartX, diffY = point.fixedPosition.chartY - normalizedEvent.chartY, newPlotX, newPlotY, graphLayoutsLookup = chart.graphLayoutsLookup;
|
||||
// At least 5px to apply change (avoids simple click):
|
||||
if (Math.abs(diffX) > 5 || Math.abs(diffY) > 5) {
|
||||
newPlotX = point.fixedPosition.plotX - diffX;
|
||||
newPlotY = point.fixedPosition.plotY - diffY;
|
||||
if (chart.isInsidePlot(newPlotX, newPlotY)) {
|
||||
point.plotX = newPlotX;
|
||||
point.plotY = newPlotY;
|
||||
point.hasDragged = true;
|
||||
this.redrawHalo(point);
|
||||
graphLayoutsLookup.forEach(function (layout) {
|
||||
layout.restartSimulation();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Mouse up action, finalizing drag&drop.
|
||||
*
|
||||
* @private
|
||||
* @param {Highcharts.Point} point The point that event occured.
|
||||
* @return {void}
|
||||
*/
|
||||
onMouseUp: function (point, event) {
|
||||
if (point.fixedPosition && point.hasDragged) {
|
||||
if (this.layout.enableSimulation) {
|
||||
this.layout.start();
|
||||
}
|
||||
else {
|
||||
this.chart.redraw();
|
||||
}
|
||||
point.inDragMode = point.hasDragged = false;
|
||||
if (!this.options.fixedDraggable) {
|
||||
delete point.fixedPosition;
|
||||
}
|
||||
}
|
||||
},
|
||||
// Draggable mode:
|
||||
/**
|
||||
* Redraw halo on mousemove during the drag&drop action.
|
||||
*
|
||||
* @private
|
||||
* @param {Highcharts.Point} point The point that should show halo.
|
||||
* @return {void}
|
||||
*/
|
||||
redrawHalo: function (point) {
|
||||
if (point && this.halo) {
|
||||
this.halo.attr({
|
||||
d: point.haloPath(this.options.states.hover.halo.size)
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
/*
|
||||
* Draggable mode:
|
||||
*/
|
||||
addEvent(Chart, 'load', function () {
|
||||
var chart = this, mousedownUnbinder, mousemoveUnbinder, mouseupUnbinder;
|
||||
if (chart.container) {
|
||||
mousedownUnbinder = addEvent(chart.container, 'mousedown', function (event) {
|
||||
var point = chart.hoverPoint;
|
||||
if (point &&
|
||||
point.series &&
|
||||
point.series.hasDraggableNodes &&
|
||||
point.series.options.draggable) {
|
||||
point.series.onMouseDown(point, event);
|
||||
mousemoveUnbinder = addEvent(chart.container, 'mousemove', function (e) {
|
||||
return point &&
|
||||
point.series &&
|
||||
point.series.onMouseMove(point, e);
|
||||
});
|
||||
mouseupUnbinder = addEvent(chart.container.ownerDocument, 'mouseup', function (e) {
|
||||
mousemoveUnbinder();
|
||||
mouseupUnbinder();
|
||||
return point &&
|
||||
point.series &&
|
||||
point.series.onMouseUp(point, e);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
addEvent(chart, 'destroy', function () {
|
||||
mousedownUnbinder();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,362 @@
|
||||
/* *
|
||||
*
|
||||
* Networkgraph series
|
||||
*
|
||||
* (c) 2010-2020 Paweł Fus
|
||||
*
|
||||
* License: www.highcharts.com/license
|
||||
*
|
||||
* !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
|
||||
*
|
||||
* */
|
||||
'use strict';
|
||||
import H from '../../Core/Globals.js';
|
||||
/* eslint-disable no-invalid-this, valid-jsdoc */
|
||||
H.networkgraphIntegrations = {
|
||||
verlet: {
|
||||
/**
|
||||
* Attractive force funtion. Can be replaced by API's
|
||||
* `layoutAlgorithm.attractiveForce`
|
||||
*
|
||||
* @private
|
||||
* @param {number} d current distance between two nodes
|
||||
* @param {number} k expected distance between two nodes
|
||||
* @return {number} force
|
||||
*/
|
||||
attractiveForceFunction: function (d, k) {
|
||||
// Used in API:
|
||||
return (k - d) / d;
|
||||
},
|
||||
/**
|
||||
* Repulsive force funtion. Can be replaced by API's
|
||||
* `layoutAlgorithm.repulsiveForce`
|
||||
*
|
||||
* @private
|
||||
* @param {number} d current distance between two nodes
|
||||
* @param {number} k expected distance between two nodes
|
||||
* @return {number} force
|
||||
*/
|
||||
repulsiveForceFunction: function (d, k) {
|
||||
// Used in API:
|
||||
return (k - d) / d * (k > d ? 1 : 0); // Force only for close nodes
|
||||
},
|
||||
/**
|
||||
* Barycenter force. Calculate and applys barycenter forces on the
|
||||
* nodes. Making them closer to the center of their barycenter point.
|
||||
*
|
||||
* In Verlet integration, force is applied on a node immidatelly to it's
|
||||
* `plotX` and `plotY` position.
|
||||
*
|
||||
* @private
|
||||
* @return {void}
|
||||
*/
|
||||
barycenter: function () {
|
||||
var gravitationalConstant = this.options.gravitationalConstant, xFactor = this.barycenter.xFactor, yFactor = this.barycenter.yFactor;
|
||||
// To consider:
|
||||
xFactor = (xFactor - (this.box.left + this.box.width) / 2) *
|
||||
gravitationalConstant;
|
||||
yFactor = (yFactor - (this.box.top + this.box.height) / 2) *
|
||||
gravitationalConstant;
|
||||
this.nodes.forEach(function (node) {
|
||||
if (!node.fixedPosition) {
|
||||
node.plotX -=
|
||||
xFactor / node.mass / node.degree;
|
||||
node.plotY -=
|
||||
yFactor / node.mass / node.degree;
|
||||
}
|
||||
});
|
||||
},
|
||||
/**
|
||||
* Repulsive force.
|
||||
*
|
||||
* In Verlet integration, force is applied on a node immidatelly to it's
|
||||
* `plotX` and `plotY` position.
|
||||
*
|
||||
* @private
|
||||
* @param {Highcharts.Point} node
|
||||
* Node that should be translated by force.
|
||||
* @param {number} force
|
||||
* Force calcualated in `repulsiveForceFunction`
|
||||
* @param {Highcharts.PositionObject} distance
|
||||
* Distance between two nodes e.g. `{x, y}`
|
||||
* @return {void}
|
||||
*/
|
||||
repulsive: function (node, force, distanceXY) {
|
||||
var factor = force * this.diffTemperature / node.mass / node.degree;
|
||||
if (!node.fixedPosition) {
|
||||
node.plotX += distanceXY.x * factor;
|
||||
node.plotY += distanceXY.y * factor;
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Attractive force.
|
||||
*
|
||||
* In Verlet integration, force is applied on a node immidatelly to it's
|
||||
* `plotX` and `plotY` position.
|
||||
*
|
||||
* @private
|
||||
* @param {Highcharts.Point} link
|
||||
* Link that connects two nodes
|
||||
* @param {number} force
|
||||
* Force calcualated in `repulsiveForceFunction`
|
||||
* @param {Highcharts.PositionObject} distance
|
||||
* Distance between two nodes e.g. `{x, y}`
|
||||
* @return {void}
|
||||
*/
|
||||
attractive: function (link, force, distanceXY) {
|
||||
var massFactor = link.getMass(), translatedX = -distanceXY.x * force * this.diffTemperature, translatedY = -distanceXY.y * force * this.diffTemperature;
|
||||
if (!link.fromNode.fixedPosition) {
|
||||
link.fromNode.plotX -=
|
||||
translatedX * massFactor.fromNode / link.fromNode.degree;
|
||||
link.fromNode.plotY -=
|
||||
translatedY * massFactor.fromNode / link.fromNode.degree;
|
||||
}
|
||||
if (!link.toNode.fixedPosition) {
|
||||
link.toNode.plotX +=
|
||||
translatedX * massFactor.toNode / link.toNode.degree;
|
||||
link.toNode.plotY +=
|
||||
translatedY * massFactor.toNode / link.toNode.degree;
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Integration method.
|
||||
*
|
||||
* In Verlet integration, forces are applied on node immidatelly to it's
|
||||
* `plotX` and `plotY` position.
|
||||
*
|
||||
* Verlet without velocity:
|
||||
*
|
||||
* x(n+1) = 2 * x(n) - x(n-1) + A(T) * deltaT ^ 2
|
||||
*
|
||||
* where:
|
||||
* - x(n+1) - new position
|
||||
* - x(n) - current position
|
||||
* - x(n-1) - previous position
|
||||
*
|
||||
* Assuming A(t) = 0 (no acceleration) and (deltaT = 1) we get:
|
||||
*
|
||||
* x(n+1) = x(n) + (x(n) - x(n-1))
|
||||
*
|
||||
* where:
|
||||
* - (x(n) - x(n-1)) - position change
|
||||
*
|
||||
* TO DO:
|
||||
* Consider Verlet with velocity to support additional
|
||||
* forces. Or even Time-Corrected Verlet by Jonathan
|
||||
* "lonesock" Dummer
|
||||
*
|
||||
* @private
|
||||
* @param {Highcharts.NetworkgraphLayout} layout layout object
|
||||
* @param {Highcharts.Point} node node that should be translated
|
||||
* @return {void}
|
||||
*/
|
||||
integrate: function (layout, node) {
|
||||
var friction = -layout.options.friction, maxSpeed = layout.options.maxSpeed, prevX = node.prevX, prevY = node.prevY,
|
||||
// Apply friciton:
|
||||
diffX = ((node.plotX + node.dispX -
|
||||
prevX) * friction), diffY = ((node.plotY + node.dispY -
|
||||
prevY) * friction), abs = Math.abs, signX = abs(diffX) / (diffX || 1), // need to deal with 0
|
||||
signY = abs(diffY) / (diffY || 1);
|
||||
// Apply max speed:
|
||||
diffX = signX * Math.min(maxSpeed, Math.abs(diffX));
|
||||
diffY = signY * Math.min(maxSpeed, Math.abs(diffY));
|
||||
// Store for the next iteration:
|
||||
node.prevX = node.plotX + node.dispX;
|
||||
node.prevY = node.plotY + node.dispY;
|
||||
// Update positions:
|
||||
node.plotX += diffX;
|
||||
node.plotY += diffY;
|
||||
node.temperature = layout.vectorLength({
|
||||
x: diffX,
|
||||
y: diffY
|
||||
});
|
||||
},
|
||||
/**
|
||||
* Estiamte the best possible distance between two nodes, making graph
|
||||
* readable.
|
||||
*
|
||||
* @private
|
||||
* @param {Highcharts.NetworkgraphLayout} layout layout object
|
||||
* @return {number}
|
||||
*/
|
||||
getK: function (layout) {
|
||||
return Math.pow(layout.box.width * layout.box.height / layout.nodes.length, 0.5);
|
||||
}
|
||||
},
|
||||
euler: {
|
||||
/**
|
||||
* Attractive force funtion. Can be replaced by API's
|
||||
* `layoutAlgorithm.attractiveForce`
|
||||
*
|
||||
* Other forces that can be used:
|
||||
*
|
||||
* basic, not recommended:
|
||||
* `function (d, k) { return d / k }`
|
||||
*
|
||||
* @private
|
||||
* @param {number} d current distance between two nodes
|
||||
* @param {number} k expected distance between two nodes
|
||||
* @return {number} force
|
||||
*/
|
||||
attractiveForceFunction: function (d, k) {
|
||||
return d * d / k;
|
||||
},
|
||||
/**
|
||||
* Repulsive force funtion. Can be replaced by API's
|
||||
* `layoutAlgorithm.repulsiveForce`.
|
||||
*
|
||||
* Other forces that can be used:
|
||||
*
|
||||
* basic, not recommended:
|
||||
* `function (d, k) { return k / d }`
|
||||
*
|
||||
* standard:
|
||||
* `function (d, k) { return k * k / d }`
|
||||
*
|
||||
* grid-variant:
|
||||
* `function (d, k) { return k * k / d * (2 * k - d > 0 ? 1 : 0) }`
|
||||
*
|
||||
* @private
|
||||
* @param {number} d current distance between two nodes
|
||||
* @param {number} k expected distance between two nodes
|
||||
* @return {number} force
|
||||
*/
|
||||
repulsiveForceFunction: function (d, k) {
|
||||
return k * k / d;
|
||||
},
|
||||
/**
|
||||
* Barycenter force. Calculate and applys barycenter forces on the
|
||||
* nodes. Making them closer to the center of their barycenter point.
|
||||
*
|
||||
* In Euler integration, force is stored in a node, not changing it's
|
||||
* position. Later, in `integrate()` forces are applied on nodes.
|
||||
*
|
||||
* @private
|
||||
* @return {void}
|
||||
*/
|
||||
barycenter: function () {
|
||||
var gravitationalConstant = this.options.gravitationalConstant, xFactor = this.barycenter.xFactor, yFactor = this.barycenter.yFactor;
|
||||
this.nodes.forEach(function (node) {
|
||||
if (!node.fixedPosition) {
|
||||
var degree = node.getDegree(), phi = degree * (1 + degree / 2);
|
||||
node.dispX += ((xFactor - node.plotX) *
|
||||
gravitationalConstant *
|
||||
phi / node.degree);
|
||||
node.dispY += ((yFactor - node.plotY) *
|
||||
gravitationalConstant *
|
||||
phi / node.degree);
|
||||
}
|
||||
});
|
||||
},
|
||||
/**
|
||||
* Repulsive force.
|
||||
*
|
||||
* @private
|
||||
* @param {Highcharts.Point} node
|
||||
* Node that should be translated by force.
|
||||
* @param {number} force
|
||||
* Force calcualated in `repulsiveForceFunction`
|
||||
* @param {Highcharts.PositionObject} distanceXY
|
||||
* Distance between two nodes e.g. `{x, y}`
|
||||
* @return {void}
|
||||
*/
|
||||
repulsive: function (node, force, distanceXY, distanceR) {
|
||||
node.dispX +=
|
||||
(distanceXY.x / distanceR) * force / node.degree;
|
||||
node.dispY +=
|
||||
(distanceXY.y / distanceR) * force / node.degree;
|
||||
},
|
||||
/**
|
||||
* Attractive force.
|
||||
*
|
||||
* In Euler integration, force is stored in a node, not changing it's
|
||||
* position. Later, in `integrate()` forces are applied on nodes.
|
||||
*
|
||||
* @private
|
||||
* @param {Highcharts.Point} link
|
||||
* Link that connects two nodes
|
||||
* @param {number} force
|
||||
* Force calcualated in `repulsiveForceFunction`
|
||||
* @param {Highcharts.PositionObject} distanceXY
|
||||
* Distance between two nodes e.g. `{x, y}`
|
||||
* @param {number} distanceR
|
||||
* @return {void}
|
||||
*/
|
||||
attractive: function (link, force, distanceXY, distanceR) {
|
||||
var massFactor = link.getMass(), translatedX = (distanceXY.x / distanceR) * force, translatedY = (distanceXY.y / distanceR) * force;
|
||||
if (!link.fromNode.fixedPosition) {
|
||||
link.fromNode.dispX -=
|
||||
translatedX * massFactor.fromNode / link.fromNode.degree;
|
||||
link.fromNode.dispY -=
|
||||
translatedY * massFactor.fromNode / link.fromNode.degree;
|
||||
}
|
||||
if (!link.toNode.fixedPosition) {
|
||||
link.toNode.dispX +=
|
||||
translatedX * massFactor.toNode / link.toNode.degree;
|
||||
link.toNode.dispY +=
|
||||
translatedY * massFactor.toNode / link.toNode.degree;
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Integration method.
|
||||
*
|
||||
* In Euler integration, force were stored in a node, not changing it's
|
||||
* position. Now, in the integrator method, we apply changes.
|
||||
*
|
||||
* Euler:
|
||||
*
|
||||
* Basic form: `x(n+1) = x(n) + v(n)`
|
||||
*
|
||||
* With Rengoild-Fruchterman we get:
|
||||
* `x(n+1) = x(n) + v(n) / length(v(n)) * min(v(n), temperature(n))`
|
||||
* where:
|
||||
* - `x(n+1)`: next position
|
||||
* - `x(n)`: current position
|
||||
* - `v(n)`: velocity (comes from net force)
|
||||
* - `temperature(n)`: current temperature
|
||||
*
|
||||
* Known issues:
|
||||
* Oscillations when force vector has the same magnitude but opposite
|
||||
* direction in the next step. Potentially solved by decreasing force by
|
||||
* `v * (1 / node.degree)`
|
||||
*
|
||||
* Note:
|
||||
* Actually `min(v(n), temperature(n))` replaces simulated annealing.
|
||||
*
|
||||
* @private
|
||||
* @param {Highcharts.NetworkgraphLayout} layout
|
||||
* Layout object
|
||||
* @param {Highcharts.Point} node
|
||||
* Node that should be translated
|
||||
* @return {void}
|
||||
*/
|
||||
integrate: function (layout, node) {
|
||||
var distanceR;
|
||||
node.dispX +=
|
||||
node.dispX * layout.options.friction;
|
||||
node.dispY +=
|
||||
node.dispY * layout.options.friction;
|
||||
distanceR = node.temperature = layout.vectorLength({
|
||||
x: node.dispX,
|
||||
y: node.dispY
|
||||
});
|
||||
if (distanceR !== 0) {
|
||||
node.plotX += (node.dispX / distanceR *
|
||||
Math.min(Math.abs(node.dispX), layout.temperature));
|
||||
node.plotY += (node.dispY / distanceR *
|
||||
Math.min(Math.abs(node.dispY), layout.temperature));
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Estiamte the best possible distance between two nodes, making graph
|
||||
* readable.
|
||||
*
|
||||
* @private
|
||||
* @param {object} layout layout object
|
||||
* @return {number}
|
||||
*/
|
||||
getK: function (layout) {
|
||||
return Math.pow(layout.box.width * layout.box.height / layout.nodes.length, 0.3);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,553 @@
|
||||
/* *
|
||||
*
|
||||
* Networkgraph series
|
||||
*
|
||||
* (c) 2010-2020 Paweł Fus
|
||||
*
|
||||
* License: www.highcharts.com/license
|
||||
*
|
||||
* !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
|
||||
*
|
||||
* */
|
||||
'use strict';
|
||||
import Chart from '../../Core/Chart/Chart.js';
|
||||
import H from '../../Core/Globals.js';
|
||||
import U from '../../Core/Utilities.js';
|
||||
var addEvent = U.addEvent, clamp = U.clamp, defined = U.defined, extend = U.extend, isFunction = U.isFunction, pick = U.pick, setAnimation = U.setAnimation;
|
||||
import './Integrations.js';
|
||||
import './QuadTree.js';
|
||||
/* eslint-disable no-invalid-this, valid-jsdoc */
|
||||
H.layouts = {
|
||||
'reingold-fruchterman': function () {
|
||||
}
|
||||
};
|
||||
extend(
|
||||
/**
|
||||
* Reingold-Fruchterman algorithm from
|
||||
* "Graph Drawing by Force-directed Placement" paper.
|
||||
* @private
|
||||
*/
|
||||
H.layouts['reingold-fruchterman'].prototype, {
|
||||
init: function (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 =
|
||||
H.networkgraphIntegrations[options.integration];
|
||||
this.enableSimulation = options.enableSimulation;
|
||||
this.attractiveForce = pick(options.attractiveForce, this.integration.attractiveForceFunction);
|
||||
this.repulsiveForce = pick(options.repulsiveForce, this.integration.repulsiveForceFunction);
|
||||
this.approximation = options.approximation;
|
||||
},
|
||||
updateSimulation: function (enable) {
|
||||
this.enableSimulation = pick(enable, this.options.enableSimulation);
|
||||
},
|
||||
start: function () {
|
||||
var layout = this, series = this.series, options = this.options;
|
||||
layout.currentStep = 0;
|
||||
layout.forces = series[0] && series[0].forces || [];
|
||||
layout.chart = series[0] && series[0].chart;
|
||||
if (layout.initialRendering) {
|
||||
layout.initPositions();
|
||||
// Render elements in initial positions:
|
||||
series.forEach(function (s) {
|
||||
s.finishedAnimating = true; // #13169
|
||||
s.render();
|
||||
});
|
||||
}
|
||||
layout.setK();
|
||||
layout.resetSimulation(options);
|
||||
if (layout.enableSimulation) {
|
||||
layout.step();
|
||||
}
|
||||
},
|
||||
step: function () {
|
||||
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 (layout.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) {
|
||||
H.win.cancelAnimationFrame(layout.simulation);
|
||||
}
|
||||
layout.simulation = H.win.requestAnimationFrame(function () {
|
||||
layout.step();
|
||||
});
|
||||
}
|
||||
else {
|
||||
layout.simulation = false;
|
||||
}
|
||||
}
|
||||
},
|
||||
stop: function () {
|
||||
if (this.simulation) {
|
||||
H.win.cancelAnimationFrame(this.simulation);
|
||||
}
|
||||
},
|
||||
setArea: function (x, y, w, h) {
|
||||
this.box = {
|
||||
left: x,
|
||||
top: y,
|
||||
width: w,
|
||||
height: h
|
||||
};
|
||||
},
|
||||
setK: function () {
|
||||
// Optimal distance between nodes,
|
||||
// available space around the node:
|
||||
this.k = this.options.linkLength || this.integration.getK(this);
|
||||
},
|
||||
addElementsToCollection: function (elements, collection) {
|
||||
elements.forEach(function (elem) {
|
||||
if (collection.indexOf(elem) === -1) {
|
||||
collection.push(elem);
|
||||
}
|
||||
});
|
||||
},
|
||||
removeElementFromCollection: function (element, collection) {
|
||||
var index = collection.indexOf(element);
|
||||
if (index !== -1) {
|
||||
collection.splice(index, 1);
|
||||
}
|
||||
},
|
||||
clear: function () {
|
||||
this.nodes.length = 0;
|
||||
this.links.length = 0;
|
||||
this.series.length = 0;
|
||||
this.resetSimulation();
|
||||
},
|
||||
resetSimulation: function () {
|
||||
this.forcedStop = false;
|
||||
this.systemTemperature = 0;
|
||||
this.setMaxIterations();
|
||||
this.setTemperature();
|
||||
this.setDiffTemperature();
|
||||
},
|
||||
restartSimulation: function () {
|
||||
if (!this.simulation) {
|
||||
// When dragging nodes, we don't need to calculate
|
||||
// initial positions and rendering nodes:
|
||||
this.setInitialRendering(false);
|
||||
// Start new simulation:
|
||||
if (!this.enableSimulation) {
|
||||
// Run only one iteration to speed things up:
|
||||
this.setMaxIterations(1);
|
||||
}
|
||||
else {
|
||||
this.start();
|
||||
}
|
||||
if (this.chart) {
|
||||
this.chart.redraw();
|
||||
}
|
||||
// Restore defaults:
|
||||
this.setInitialRendering(true);
|
||||
}
|
||||
else {
|
||||
// Extend current simulation:
|
||||
this.resetSimulation();
|
||||
}
|
||||
},
|
||||
setMaxIterations: function (maxIterations) {
|
||||
this.maxIterations = pick(maxIterations, this.options.maxIterations);
|
||||
},
|
||||
setTemperature: function () {
|
||||
this.temperature = this.startTemperature =
|
||||
Math.sqrt(this.nodes.length);
|
||||
},
|
||||
setDiffTemperature: function () {
|
||||
this.diffTemperature = this.startTemperature /
|
||||
(this.options.maxIterations + 1);
|
||||
},
|
||||
setInitialRendering: function (enable) {
|
||||
this.initialRendering = enable;
|
||||
},
|
||||
createQuadTree: function () {
|
||||
this.quadTree = new H.QuadTree(this.box.left, this.box.top, this.box.width, this.box.height);
|
||||
this.quadTree.insertNodes(this.nodes);
|
||||
},
|
||||
initPositions: function () {
|
||||
var initialPositions = this.options.initialPositions;
|
||||
if (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 () {
|
||||
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 () {
|
||||
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 (name) {
|
||||
this.integration[name].apply(this, Array.prototype.slice.call(arguments, 1));
|
||||
},
|
||||
barycenterForces: function () {
|
||||
this.getBarycenter();
|
||||
this.force('barycenter');
|
||||
},
|
||||
getBarycenter: function () {
|
||||
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 (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 () {
|
||||
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 () {
|
||||
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 () {
|
||||
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 (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 = clamp(node.plotX, box.left + radius, box.width - radius);
|
||||
// Limit Y-coordinates:
|
||||
node.plotY = clamp(node.plotY, box.top + radius, box.height - radius);
|
||||
},
|
||||
/**
|
||||
* From "A comparison of simulated annealing cooling strategies" by
|
||||
* Nourani and Andresen work.
|
||||
* @private
|
||||
*/
|
||||
coolDown: function (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 () {
|
||||
return Math.abs(this.systemTemperature -
|
||||
this.prevSystemTemperature) < 0.00001 || this.temperature <= 0;
|
||||
},
|
||||
getSystemTemperature: function () {
|
||||
return this.nodes.reduce(function (value, node) {
|
||||
return value + node.temperature;
|
||||
}, 0);
|
||||
},
|
||||
vectorLength: function (vector) {
|
||||
return Math.sqrt(vector.x * vector.x + vector.y * vector.y);
|
||||
},
|
||||
getDistR: function (nodeA, nodeB) {
|
||||
var distance = this.getDistXY(nodeA, nodeB);
|
||||
return this.vectorLength(distance);
|
||||
},
|
||||
getDistXY: function (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.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) {
|
||||
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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
// disable simulation before print if enabled
|
||||
addEvent(Chart, 'beforePrint', function () {
|
||||
if (this.graphLayoutsLookup) {
|
||||
this.graphLayoutsLookup.forEach(function (layout) {
|
||||
layout.updateSimulation(false);
|
||||
});
|
||||
this.redraw();
|
||||
}
|
||||
});
|
||||
// re-enable simulation after print
|
||||
addEvent(Chart, 'afterPrint', function () {
|
||||
if (this.graphLayoutsLookup) {
|
||||
this.graphLayoutsLookup.forEach(function (layout) {
|
||||
// return to default simulation
|
||||
layout.updateSimulation();
|
||||
});
|
||||
}
|
||||
this.redraw();
|
||||
});
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,354 @@
|
||||
/* *
|
||||
*
|
||||
* Networkgraph series
|
||||
*
|
||||
* (c) 2010-2020 Paweł Fus
|
||||
*
|
||||
* License: www.highcharts.com/license
|
||||
*
|
||||
* !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
|
||||
*
|
||||
* */
|
||||
'use strict';
|
||||
import H from '../../Core/Globals.js';
|
||||
import U from '../../Core/Utilities.js';
|
||||
var extend = U.extend;
|
||||
/* eslint-disable no-invalid-this, valid-jsdoc */
|
||||
/**
|
||||
* The QuadTree node class. Used in Networkgraph chart as a base for Barnes-Hut
|
||||
* approximation.
|
||||
*
|
||||
* @private
|
||||
* @class
|
||||
* @name Highcharts.QuadTreeNode
|
||||
*
|
||||
* @param {Highcharts.Dictionary<number>} box Available space for the node
|
||||
*/
|
||||
var QuadTreeNode = H.QuadTreeNode = function (box) {
|
||||
/**
|
||||
* Read only. The available space for node.
|
||||
*
|
||||
* @name Highcharts.QuadTreeNode#box
|
||||
* @type {Highcharts.Dictionary<number>}
|
||||
*/
|
||||
this.box = box;
|
||||
/**
|
||||
* Read only. The minium of width and height values.
|
||||
*
|
||||
* @name Highcharts.QuadTreeNode#boxSize
|
||||
* @type {number}
|
||||
*/
|
||||
this.boxSize = Math.min(box.width, box.height);
|
||||
/**
|
||||
* Read only. Array of subnodes. Empty if QuadTreeNode has just one Point.
|
||||
* When added another Point to this QuadTreeNode, array is filled with four
|
||||
* subnodes.
|
||||
*
|
||||
* @name Highcharts.QuadTreeNode#nodes
|
||||
* @type {Array<Highcharts.QuadTreeNode>}
|
||||
*/
|
||||
this.nodes = [];
|
||||
/**
|
||||
* Read only. Flag to determine if QuadTreeNode is internal (and has
|
||||
* subnodes with mass and central position) or external (bound to Point).
|
||||
*
|
||||
* @name Highcharts.QuadTreeNode#isInternal
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.isInternal = false;
|
||||
/**
|
||||
* Read only. If QuadTreeNode is an external node, Point is stored in
|
||||
* `this.body`.
|
||||
*
|
||||
* @name Highcharts.QuadTreeNode#body
|
||||
* @type {boolean|Highcharts.Point}
|
||||
*/
|
||||
this.body = false;
|
||||
/**
|
||||
* Read only. Internal nodes when created are empty to reserve the space. If
|
||||
* Point is added to this QuadTreeNode, QuadTreeNode is no longer empty.
|
||||
*
|
||||
* @name Highcharts.QuadTreeNode#isEmpty
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.isEmpty = true;
|
||||
};
|
||||
extend(QuadTreeNode.prototype,
|
||||
/** @lends Highcharts.QuadTreeNode.prototype */
|
||||
{
|
||||
/**
|
||||
* Insert recursively point(node) into the QuadTree. If the given
|
||||
* quadrant is already occupied, divide it into smaller quadrants.
|
||||
*
|
||||
* @param {Highcharts.Point} point
|
||||
* Point/node to be inserted
|
||||
* @param {number} depth
|
||||
* Max depth of the QuadTree
|
||||
*/
|
||||
insert: function (point, depth) {
|
||||
var newQuadTreeNode;
|
||||
if (this.isInternal) {
|
||||
// Internal node:
|
||||
this.nodes[this.getBoxPosition(point)].insert(point, depth - 1);
|
||||
}
|
||||
else {
|
||||
this.isEmpty = false;
|
||||
if (!this.body) {
|
||||
// First body in a quadrant:
|
||||
this.isInternal = false;
|
||||
this.body = point;
|
||||
}
|
||||
else {
|
||||
if (depth) {
|
||||
// Every other body in a quadrant:
|
||||
this.isInternal = true;
|
||||
this.divideBox();
|
||||
// Reinsert main body only once:
|
||||
if (this.body !== true) {
|
||||
this.nodes[this.getBoxPosition(this.body)]
|
||||
.insert(this.body, depth - 1);
|
||||
this.body = true;
|
||||
}
|
||||
// Add second body:
|
||||
this.nodes[this.getBoxPosition(point)]
|
||||
.insert(point, depth - 1);
|
||||
}
|
||||
else {
|
||||
// We are below max allowed depth. That means either:
|
||||
// - really huge number of points
|
||||
// - falling two points into exactly the same position
|
||||
// In this case, create another node in the QuadTree.
|
||||
//
|
||||
// Alternatively we could add some noise to the
|
||||
// position, but that could result in different
|
||||
// rendered chart in exporting.
|
||||
newQuadTreeNode = new QuadTreeNode({
|
||||
top: point.plotX,
|
||||
left: point.plotY,
|
||||
// Width/height below 1px
|
||||
width: 0.1,
|
||||
height: 0.1
|
||||
});
|
||||
newQuadTreeNode.body = point;
|
||||
newQuadTreeNode.isInternal = false;
|
||||
this.nodes.push(newQuadTreeNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Each quad node requires it's mass and center position. That mass and
|
||||
* position is used to imitate real node in the layout by approximation.
|
||||
*/
|
||||
updateMassAndCenter: function () {
|
||||
var mass = 0, plotX = 0, plotY = 0;
|
||||
if (this.isInternal) {
|
||||
// Calcualte weightened mass of the quad node:
|
||||
this.nodes.forEach(function (pointMass) {
|
||||
if (!pointMass.isEmpty) {
|
||||
mass += pointMass.mass;
|
||||
plotX +=
|
||||
pointMass.plotX * pointMass.mass;
|
||||
plotY +=
|
||||
pointMass.plotY * pointMass.mass;
|
||||
}
|
||||
});
|
||||
plotX /= mass;
|
||||
plotY /= mass;
|
||||
}
|
||||
else if (this.body) {
|
||||
// Just one node, use coordinates directly:
|
||||
mass = this.body.mass;
|
||||
plotX = this.body.plotX;
|
||||
plotY = this.body.plotY;
|
||||
}
|
||||
// Store details:
|
||||
this.mass = mass;
|
||||
this.plotX = plotX;
|
||||
this.plotY = plotY;
|
||||
},
|
||||
/**
|
||||
* When inserting another node into the box, that already hove one node,
|
||||
* divide the available space into another four quadrants.
|
||||
*
|
||||
* Indexes of quadrants are:
|
||||
* ```
|
||||
* ------------- -------------
|
||||
* | | | | |
|
||||
* | | | 0 | 1 |
|
||||
* | | divide() | | |
|
||||
* | 1 | -----------> -------------
|
||||
* | | | | |
|
||||
* | | | 3 | 2 |
|
||||
* | | | | |
|
||||
* ------------- -------------
|
||||
* ```
|
||||
*/
|
||||
divideBox: function () {
|
||||
var halfWidth = this.box.width / 2, halfHeight = this.box.height / 2;
|
||||
// Top left
|
||||
this.nodes[0] = new QuadTreeNode({
|
||||
left: this.box.left,
|
||||
top: this.box.top,
|
||||
width: halfWidth,
|
||||
height: halfHeight
|
||||
});
|
||||
// Top right
|
||||
this.nodes[1] = new QuadTreeNode({
|
||||
left: this.box.left + halfWidth,
|
||||
top: this.box.top,
|
||||
width: halfWidth,
|
||||
height: halfHeight
|
||||
});
|
||||
// Bottom right
|
||||
this.nodes[2] = new QuadTreeNode({
|
||||
left: this.box.left + halfWidth,
|
||||
top: this.box.top + halfHeight,
|
||||
width: halfWidth,
|
||||
height: halfHeight
|
||||
});
|
||||
// Bottom left
|
||||
this.nodes[3] = new QuadTreeNode({
|
||||
left: this.box.left,
|
||||
top: this.box.top + halfHeight,
|
||||
width: halfWidth,
|
||||
height: halfHeight
|
||||
});
|
||||
},
|
||||
/**
|
||||
* Determine which of the quadrants should be used when placing node in
|
||||
* the QuadTree. Returned index is always in range `< 0 , 3 >`.
|
||||
*
|
||||
* @param {Highcharts.Point} point
|
||||
* @return {number}
|
||||
*/
|
||||
getBoxPosition: function (point) {
|
||||
var left = point.plotX < this.box.left + this.box.width / 2, top = point.plotY < this.box.top + this.box.height / 2, index;
|
||||
if (left) {
|
||||
if (top) {
|
||||
// Top left
|
||||
index = 0;
|
||||
}
|
||||
else {
|
||||
// Bottom left
|
||||
index = 3;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (top) {
|
||||
// Top right
|
||||
index = 1;
|
||||
}
|
||||
else {
|
||||
// Bottom right
|
||||
index = 2;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
});
|
||||
/**
|
||||
* The QuadTree class. Used in Networkgraph chart as a base for Barnes-Hut
|
||||
* approximation.
|
||||
*
|
||||
* @private
|
||||
* @class
|
||||
* @name Highcharts.QuadTree
|
||||
*
|
||||
* @param {number} x left position of the plotting area
|
||||
* @param {number} y top position of the plotting area
|
||||
* @param {number} width width of the plotting area
|
||||
* @param {number} height height of the plotting area
|
||||
*/
|
||||
var QuadTree = H.QuadTree = function (x, y, width, height) {
|
||||
// Boundary rectangle:
|
||||
this.box = {
|
||||
left: x,
|
||||
top: y,
|
||||
width: width,
|
||||
height: height
|
||||
};
|
||||
this.maxDepth = 25;
|
||||
this.root = new QuadTreeNode(this.box, '0');
|
||||
this.root.isInternal = true;
|
||||
this.root.isRoot = true;
|
||||
this.root.divideBox();
|
||||
};
|
||||
extend(QuadTree.prototype,
|
||||
/** @lends Highcharts.QuadTree.prototype */
|
||||
{
|
||||
/**
|
||||
* Insert nodes into the QuadTree
|
||||
*
|
||||
* @param {Array<Highcharts.Point>} points
|
||||
*/
|
||||
insertNodes: function (points) {
|
||||
points.forEach(function (point) {
|
||||
this.root.insert(point, this.maxDepth);
|
||||
}, this);
|
||||
},
|
||||
/**
|
||||
* Depfth first treversal (DFS). Using `before` and `after` callbacks,
|
||||
* we can get two results: preorder and postorder traversals, reminder:
|
||||
*
|
||||
* ```
|
||||
* (a)
|
||||
* / \
|
||||
* (b) (c)
|
||||
* / \
|
||||
* (d) (e)
|
||||
* ```
|
||||
*
|
||||
* DFS (preorder): `a -> b -> d -> e -> c`
|
||||
*
|
||||
* DFS (postorder): `d -> e -> b -> c -> a`
|
||||
*
|
||||
* @param {Highcharts.QuadTreeNode|null} node
|
||||
* @param {Function} [beforeCallback] function to be called before
|
||||
* visiting children nodes
|
||||
* @param {Function} [afterCallback] function to be called after
|
||||
* visiting children nodes
|
||||
*/
|
||||
visitNodeRecursive: function (node, beforeCallback, afterCallback) {
|
||||
var goFurther;
|
||||
if (!node) {
|
||||
node = this.root;
|
||||
}
|
||||
if (node === this.root && beforeCallback) {
|
||||
goFurther = beforeCallback(node);
|
||||
}
|
||||
if (goFurther === false) {
|
||||
return;
|
||||
}
|
||||
node.nodes.forEach(function (qtNode) {
|
||||
if (qtNode.isInternal) {
|
||||
if (beforeCallback) {
|
||||
goFurther = beforeCallback(qtNode);
|
||||
}
|
||||
if (goFurther === false) {
|
||||
return;
|
||||
}
|
||||
this.visitNodeRecursive(qtNode, beforeCallback, afterCallback);
|
||||
}
|
||||
else if (qtNode.body) {
|
||||
if (beforeCallback) {
|
||||
beforeCallback(qtNode.body);
|
||||
}
|
||||
}
|
||||
if (afterCallback) {
|
||||
afterCallback(qtNode);
|
||||
}
|
||||
}, this);
|
||||
if (node === this.root && afterCallback) {
|
||||
afterCallback(node);
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Calculate mass of the each QuadNode in the tree.
|
||||
*/
|
||||
calculateMassAndCenter: function () {
|
||||
this.visitNodeRecursive(null, null, function (node) {
|
||||
node.updateMassAndCenter();
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user