d3-force-clustering
Version:
A clustering attraction force to group nodes for the d3-force simulation engine.
185 lines (176 loc) • 6.72 kB
JavaScript
// Version 1.0.0 d3-force-clustering - https://github.com/vasturiano/d3-force-clustering
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}));
})(this, (function (exports) { 'use strict';
function _arrayLikeToArray(r, a) {
(null == a || a > r.length) && (a = r.length);
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
return n;
}
function _arrayWithHoles(r) {
if (Array.isArray(r)) return r;
}
function _arrayWithoutHoles(r) {
if (Array.isArray(r)) return _arrayLikeToArray(r);
}
function _iterableToArray(r) {
if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
}
function _iterableToArrayLimit(r, l) {
var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
if (null != t) {
var e,
n,
i,
u,
a = [],
f = true,
o = false;
try {
if (i = (t = t.call(r)).next, 0 === l) ; else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
} catch (r) {
o = true, n = r;
} finally {
try {
if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
} finally {
if (o) throw n;
}
}
return a;
}
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _slicedToArray(r, e) {
return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
}
function _toConsumableArray(r) {
return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
}
function _unsupportedIterableToArray(r, a) {
if (r) {
if ("string" == typeof r) return _arrayLikeToArray(r, a);
var t = {}.toString.call(r).slice(8, -1);
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
}
}
function cluster () {
var clusterId = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function (node) {
return node.cluster;
};
var nDim,
nodes = [],
weight = constant(1),
// accessor: node weight to calculate cluster centroid
strength = constant(0.2),
// accessor: attraction strength per cluster (clusterId and nodes are included as arguments)
distanceMin = 0;
var clusterNodes = new Map();
function force(alpha) {
var centroids = new Map(_toConsumableArray(clusterNodes.entries()).map(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
cluster = _ref2[0],
nodes = _ref2[1];
return [cluster, getCentroid(nodes, weight, nDim)];
}));
var strengths = new Map(_toConsumableArray(clusterNodes.entries()).map(function (_ref3) {
var _ref4 = _slicedToArray(_ref3, 2),
cluster = _ref4[0],
nodes = _ref4[1];
return [cluster, strength(cluster, nodes)];
}));
var dims = ['x', nDim > 1 && 'y', nDim > 2 && 'z'].filter(function (d) {
return d;
});
var vDims = dims.map(function (dim) {
return "v".concat(dim);
});
nodes.forEach(function (node) {
var cluster = clusterId(node);
if (!centroids.has(cluster)) return; // unknown cluster
var centroid = centroids.get(cluster);
var d = dims.map(function (dim) {
return centroid[dim] - node[dim];
});
if (calcDist.apply(void 0, _toConsumableArray(d)) <= distanceMin) return; // Too close to centroid
var acceleration = strengths.get(cluster) * alpha;
vDims.forEach(function (vDim, idx) {
return node[vDim] += d[idx] * acceleration;
});
});
}
function initialize() {
clusterNodes.clear();
nodes.forEach(function (node) {
var cluster = clusterId(node);
if (!clusterNodes.has(cluster)) clusterNodes.set(cluster, []);
clusterNodes.get(cluster).push(node);
});
// Ignore clusters with a single element
clusterNodes.forEach(function (nodes, cluster) {
return nodes.length <= 1 && clusterNodes["delete"](cluster);
});
}
force.initialize = function (initNodes) {
nodes = initNodes;
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
nDim = args.find(function (arg) {
return [1, 2, 3].includes(arg);
}) || 2;
initialize();
};
force.clusterId = function (_) {
return arguments.length ? (clusterId = _, initialize(), force) : clusterId;
};
force.weight = function (_) {
return arguments.length ? (weight = typeof _ === 'function' ? _ : constant(+_), force) : weight;
};
force.strength = function (_) {
return arguments.length ? (strength = typeof _ === 'function' ? _ : constant(+_), force) : strength;
};
force.distanceMin = function (_) {
return arguments.length ? (distanceMin = _, force) : distanceMin;
};
return force;
}
//
function calcDist(x) {
var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var z = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
return Math.sqrt(x * x + y * y + z * z);
}
function constant(x) {
return function () {
return x;
};
}
function getCentroid(nodes, weightFn, nDim) {
var k = 0;
var x = 0;
var y = 0;
var z = 0;
nodes.forEach(function (node) {
var w = weightFn(node);
x += node.x * w;
nDim > 1 && (y += node.y * w);
nDim > 2 && (z += node.z * w);
k += w;
});
var res = {};
res.x = x / k;
nDim > 1 && (res.y = y / k);
nDim > 2 && (res.z = z / k);
return res;
}
exports.forceClustering = cluster;
}));
//# sourceMappingURL=d3-force-clustering.js.map