Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": [ "es2015" ]
"presets": [ "env" ]
}
32 changes: 23 additions & 9 deletions lib/forceCluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ function cluster (centers) {

let nodes,
centerpoints = [],
strength = 0.1,
strength = () => 0.1,
strengths = [],
centerInertia = 0.0;

// coerce centers accessor into a function
if (typeof centers !== 'function') centers = () => centers;

function force (alpha) {
// scale + curve alpha value
alpha *= strength * alpha;

let c, x, y, l, r;
let a, c, x, y, l, r;
nodes.forEach((d, i) => {
// scale + curve alpha value
a = alpha * strengths[i]
c = centerpoints[i];
if (!c || c === d) return;

x = d.x - c.x,
y = d.y - c.y,
l = Math.sqrt(x * x + y * y),
r = d.radius + (c.radius || 0);
r = (d.radius || 0) + (c.radius || 0);

if (l && l != r) {
l = (l - r) / l * alpha;
l = (l - r) / l * a;
d.x -= x *= l;
d.y -= y *= l;
c.x += (1 - centerInertia) * x;
Expand All @@ -43,7 +43,11 @@ function cluster (centers) {
// populate local `centerpoints` using `centers` accessor
let i, n = nodes.length;
centerpoints = new Array(n);
for (i = 0; i < n; i++) centerpoints[i] = centers(nodes[i], i, nodes);
strengths = new Array(n);
for (i = 0; i < n; i++) {
centerpoints[i] = centers(nodes[i], i, nodes);
strengths[i] = strength(nodes[i], i, nodes);
}
}

/**
Expand Down Expand Up @@ -77,7 +81,17 @@ function cluster (centers) {
* Strength of attraction to the cluster center node/position.
*/
force.strength = _ => {
return _ == null ? strength : (strength = +_, force);
// return existing value if no value passed
if (_ == null) return strength;

// coerce strength accessor into a function
strength = typeof _ === 'function' ? _ : (n, i) => _[i];

// reinitialize
initialize();

// allow chaining
return force;
};

/**
Expand Down
Loading