Comparing version 1.0.2 to 1.0.3
@@ -1,2 +0,2 @@ | ||
// https://d3js.org/d3-force/ Version 1.0.2. Copyright 2016 Mike Bostock. | ||
// https://d3js.org/d3-force/ Version 1.0.3. Copyright 2016 Mike Bostock. | ||
(function (global, factory) { | ||
@@ -6,588 +6,602 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-quadtree'), require('d3-collection'), require('d3-dispatch'), require('d3-timer')) : | ||
(factory((global.d3 = global.d3 || {}),global.d3,global.d3,global.d3,global.d3)); | ||
}(this, function (exports,d3Quadtree,d3Collection,d3Dispatch,d3Timer) { 'use strict'; | ||
}(this, (function (exports,d3Quadtree,d3Collection,d3Dispatch,d3Timer) { 'use strict'; | ||
function center(x, y) { | ||
var nodes; | ||
var center = function(x, y) { | ||
var nodes; | ||
if (x == null) x = 0; | ||
if (y == null) y = 0; | ||
if (x == null) x = 0; | ||
if (y == null) y = 0; | ||
function force() { | ||
var i, | ||
n = nodes.length, | ||
node, | ||
sx = 0, | ||
sy = 0; | ||
function force() { | ||
var i, | ||
n = nodes.length, | ||
node, | ||
sx = 0, | ||
sy = 0; | ||
for (i = 0; i < n; ++i) { | ||
node = nodes[i], sx += node.x, sy += node.y; | ||
} | ||
for (i = 0; i < n; ++i) { | ||
node = nodes[i], sx += node.x, sy += node.y; | ||
} | ||
for (sx = sx / n - x, sy = sy / n - y, i = 0; i < n; ++i) { | ||
node = nodes[i], node.x -= sx, node.y -= sy; | ||
} | ||
for (sx = sx / n - x, sy = sy / n - y, i = 0; i < n; ++i) { | ||
node = nodes[i], node.x -= sx, node.y -= sy; | ||
} | ||
} | ||
force.initialize = function(_) { | ||
nodes = _; | ||
}; | ||
force.initialize = function(_) { | ||
nodes = _; | ||
}; | ||
force.x = function(_) { | ||
return arguments.length ? (x = +_, force) : x; | ||
}; | ||
force.x = function(_) { | ||
return arguments.length ? (x = +_, force) : x; | ||
}; | ||
force.y = function(_) { | ||
return arguments.length ? (y = +_, force) : y; | ||
}; | ||
force.y = function(_) { | ||
return arguments.length ? (y = +_, force) : y; | ||
}; | ||
return force; | ||
} | ||
return force; | ||
} | ||
function constant(x) { | ||
return function() { | ||
return x; | ||
}; | ||
} | ||
var constant = function(x) { | ||
return function() { | ||
return x; | ||
}; | ||
} | ||
function jiggle() { | ||
return (Math.random() - 0.5) * 1e-6; | ||
} | ||
var jiggle = function() { | ||
return (Math.random() - 0.5) * 1e-6; | ||
} | ||
function x(d) { | ||
return d.x + d.vx; | ||
} | ||
function x(d) { | ||
return d.x + d.vx; | ||
} | ||
function y(d) { | ||
return d.y + d.vy; | ||
} | ||
function y(d) { | ||
return d.y + d.vy; | ||
} | ||
function collide(radius) { | ||
var nodes, | ||
radii, | ||
strength = 1, | ||
iterations = 1; | ||
var collide = function(radius) { | ||
var nodes, | ||
radii, | ||
strength = 1, | ||
iterations = 1; | ||
if (typeof radius !== "function") radius = constant(radius == null ? 1 : +radius); | ||
if (typeof radius !== "function") radius = constant(radius == null ? 1 : +radius); | ||
function force() { | ||
var i, n = nodes.length, | ||
tree, | ||
node, | ||
xi, | ||
yi, | ||
ri, | ||
ri2; | ||
function force() { | ||
var i, n = nodes.length, | ||
tree, | ||
node, | ||
xi, | ||
yi, | ||
ri, | ||
ri2; | ||
for (var k = 0; k < iterations; ++k) { | ||
tree = d3Quadtree.quadtree(nodes, x, y).visitAfter(prepare); | ||
for (i = 0; i < n; ++i) { | ||
node = nodes[i]; | ||
ri = radii[i], ri2 = ri * ri; | ||
xi = node.x + node.vx; | ||
yi = node.y + node.vy; | ||
tree.visit(apply); | ||
} | ||
for (var k = 0; k < iterations; ++k) { | ||
tree = d3Quadtree.quadtree(nodes, x, y).visitAfter(prepare); | ||
for (i = 0; i < n; ++i) { | ||
node = nodes[i]; | ||
ri = radii[i], ri2 = ri * ri; | ||
xi = node.x + node.vx; | ||
yi = node.y + node.vy; | ||
tree.visit(apply); | ||
} | ||
} | ||
function apply(quad, x0, y0, x1, y1) { | ||
var data = quad.data, rj = quad.r, r = ri + rj; | ||
if (data) { | ||
if (data.index > i) { | ||
var x = xi - data.x - data.vx, | ||
y = yi - data.y - data.vy, | ||
l = x * x + y * y; | ||
if (l < r * r) { | ||
if (x === 0) x = jiggle(), l += x * x; | ||
if (y === 0) y = jiggle(), l += y * y; | ||
l = (r - (l = Math.sqrt(l))) / l * strength; | ||
node.vx += (x *= l) * (r = (rj *= rj) / (ri2 + rj)); | ||
node.vy += (y *= l) * r; | ||
data.vx -= x * (r = 1 - r); | ||
data.vy -= y * r; | ||
} | ||
function apply(quad, x0, y0, x1, y1) { | ||
var data = quad.data, rj = quad.r, r = ri + rj; | ||
if (data) { | ||
if (data.index > i) { | ||
var x = xi - data.x - data.vx, | ||
y = yi - data.y - data.vy, | ||
l = x * x + y * y; | ||
if (l < r * r) { | ||
if (x === 0) x = jiggle(), l += x * x; | ||
if (y === 0) y = jiggle(), l += y * y; | ||
l = (r - (l = Math.sqrt(l))) / l * strength; | ||
node.vx += (x *= l) * (r = (rj *= rj) / (ri2 + rj)); | ||
node.vy += (y *= l) * r; | ||
data.vx -= x * (r = 1 - r); | ||
data.vy -= y * r; | ||
} | ||
return; | ||
} | ||
return x0 > xi + r || x1 < xi - r || y0 > yi + r || y1 < yi - r; | ||
return; | ||
} | ||
return x0 > xi + r || x1 < xi - r || y0 > yi + r || y1 < yi - r; | ||
} | ||
} | ||
function prepare(quad) { | ||
if (quad.data) return quad.r = radii[quad.data.index]; | ||
for (var i = quad.r = 0; i < 4; ++i) { | ||
if (quad[i] && quad[i].r > quad.r) { | ||
quad.r = quad[i].r; | ||
} | ||
function prepare(quad) { | ||
if (quad.data) return quad.r = radii[quad.data.index]; | ||
for (var i = quad.r = 0; i < 4; ++i) { | ||
if (quad[i] && quad[i].r > quad.r) { | ||
quad.r = quad[i].r; | ||
} | ||
} | ||
} | ||
force.initialize = function(_) { | ||
var i, n = (nodes = _).length; radii = new Array(n); | ||
for (i = 0; i < n; ++i) radii[i] = +radius(nodes[i], i, nodes); | ||
}; | ||
function initialize() { | ||
if (!nodes) return; | ||
var i, n = nodes.length; | ||
radii = new Array(n); | ||
for (i = 0; i < n; ++i) radii[i] = +radius(nodes[i], i, nodes); | ||
} | ||
force.iterations = function(_) { | ||
return arguments.length ? (iterations = +_, force) : iterations; | ||
}; | ||
force.initialize = function(_) { | ||
nodes = _; | ||
initialize(); | ||
}; | ||
force.strength = function(_) { | ||
return arguments.length ? (strength = +_, force) : strength; | ||
}; | ||
force.iterations = function(_) { | ||
return arguments.length ? (iterations = +_, force) : iterations; | ||
}; | ||
force.radius = function(_) { | ||
return arguments.length ? (radius = typeof _ === "function" ? _ : constant(+_), force) : radius; | ||
}; | ||
force.strength = function(_) { | ||
return arguments.length ? (strength = +_, force) : strength; | ||
}; | ||
return force; | ||
} | ||
force.radius = function(_) { | ||
return arguments.length ? (radius = typeof _ === "function" ? _ : constant(+_), initialize(), force) : radius; | ||
}; | ||
function index(d, i) { | ||
return i; | ||
} | ||
return force; | ||
} | ||
function link(links) { | ||
var id = index, | ||
strength = defaultStrength, | ||
strengths, | ||
distance = constant(30), | ||
distances, | ||
nodes, | ||
count, | ||
bias, | ||
iterations = 1; | ||
function index(d, i) { | ||
return i; | ||
} | ||
if (links == null) links = []; | ||
function find(nodeById, nodeId) { | ||
var node = nodeById.get(nodeId); | ||
if (!node) throw new Error("missing: " + nodeId); | ||
return node; | ||
} | ||
function defaultStrength(link) { | ||
return 1 / Math.min(count[link.source.index], count[link.target.index]); | ||
} | ||
var link = function(links) { | ||
var id = index, | ||
strength = defaultStrength, | ||
strengths, | ||
distance = constant(30), | ||
distances, | ||
nodes, | ||
count, | ||
bias, | ||
iterations = 1; | ||
function force(alpha) { | ||
for (var k = 0, n = links.length; k < iterations; ++k) { | ||
for (var i = 0, link, source, target, x, y, l, b; i < n; ++i) { | ||
link = links[i], source = link.source, target = link.target; | ||
x = target.x + target.vx - source.x - source.vx || jiggle(); | ||
y = target.y + target.vy - source.y - source.vy || jiggle(); | ||
l = Math.sqrt(x * x + y * y); | ||
l = (l - distances[i]) / l * alpha * strengths[i]; | ||
x *= l, y *= l; | ||
target.vx -= x * (b = bias[i]); | ||
target.vy -= y * b; | ||
source.vx += x * (b = 1 - b); | ||
source.vy += y * b; | ||
} | ||
if (links == null) links = []; | ||
function defaultStrength(link) { | ||
return 1 / Math.min(count[link.source.index], count[link.target.index]); | ||
} | ||
function force(alpha) { | ||
for (var k = 0, n = links.length; k < iterations; ++k) { | ||
for (var i = 0, link, source, target, x, y, l, b; i < n; ++i) { | ||
link = links[i], source = link.source, target = link.target; | ||
x = target.x + target.vx - source.x - source.vx || jiggle(); | ||
y = target.y + target.vy - source.y - source.vy || jiggle(); | ||
l = Math.sqrt(x * x + y * y); | ||
l = (l - distances[i]) / l * alpha * strengths[i]; | ||
x *= l, y *= l; | ||
target.vx -= x * (b = bias[i]); | ||
target.vy -= y * b; | ||
source.vx += x * (b = 1 - b); | ||
source.vy += y * b; | ||
} | ||
} | ||
} | ||
function initialize() { | ||
if (!nodes) return; | ||
function initialize() { | ||
if (!nodes) return; | ||
var i, | ||
n = nodes.length, | ||
m = links.length, | ||
nodeById = d3Collection.map(nodes, id), | ||
link; | ||
var i, | ||
n = nodes.length, | ||
m = links.length, | ||
nodeById = d3Collection.map(nodes, id), | ||
link; | ||
for (i = 0, count = new Array(n); i < n; ++i) { | ||
count[i] = 0; | ||
} | ||
for (i = 0, count = new Array(n); i < n; ++i) { | ||
count[i] = 0; | ||
} | ||
for (i = 0; i < m; ++i) { | ||
link = links[i], link.index = i; | ||
if (typeof link.source !== "object") link.source = nodeById.get(link.source); | ||
if (typeof link.target !== "object") link.target = nodeById.get(link.target); | ||
++count[link.source.index], ++count[link.target.index]; | ||
} | ||
for (i = 0; i < m; ++i) { | ||
link = links[i], link.index = i; | ||
if (typeof link.source !== "object") link.source = find(nodeById, link.source); | ||
if (typeof link.target !== "object") link.target = find(nodeById, link.target); | ||
++count[link.source.index], ++count[link.target.index]; | ||
} | ||
for (i = 0, bias = new Array(m); i < m; ++i) { | ||
link = links[i], bias[i] = count[link.source.index] / (count[link.source.index] + count[link.target.index]); | ||
} | ||
strengths = new Array(m), initializeStrength(); | ||
distances = new Array(m), initializeDistance(); | ||
for (i = 0, bias = new Array(m); i < m; ++i) { | ||
link = links[i], bias[i] = count[link.source.index] / (count[link.source.index] + count[link.target.index]); | ||
} | ||
function initializeStrength() { | ||
if (!nodes) return; | ||
strengths = new Array(m), initializeStrength(); | ||
distances = new Array(m), initializeDistance(); | ||
} | ||
for (var i = 0, n = links.length; i < n; ++i) { | ||
strengths[i] = +strength(links[i], i, links); | ||
} | ||
function initializeStrength() { | ||
if (!nodes) return; | ||
for (var i = 0, n = links.length; i < n; ++i) { | ||
strengths[i] = +strength(links[i], i, links); | ||
} | ||
} | ||
function initializeDistance() { | ||
if (!nodes) return; | ||
function initializeDistance() { | ||
if (!nodes) return; | ||
for (var i = 0, n = links.length; i < n; ++i) { | ||
distances[i] = +distance(links[i], i, links); | ||
} | ||
for (var i = 0, n = links.length; i < n; ++i) { | ||
distances[i] = +distance(links[i], i, links); | ||
} | ||
} | ||
force.initialize = function(_) { | ||
nodes = _; | ||
initialize(); | ||
}; | ||
force.initialize = function(_) { | ||
nodes = _; | ||
initialize(); | ||
}; | ||
force.links = function(_) { | ||
return arguments.length ? (links = _, initialize(), force) : links; | ||
}; | ||
force.links = function(_) { | ||
return arguments.length ? (links = _, initialize(), force) : links; | ||
}; | ||
force.id = function(_) { | ||
return arguments.length ? (id = _, force) : id; | ||
}; | ||
force.id = function(_) { | ||
return arguments.length ? (id = _, force) : id; | ||
}; | ||
force.iterations = function(_) { | ||
return arguments.length ? (iterations = +_, force) : iterations; | ||
}; | ||
force.iterations = function(_) { | ||
return arguments.length ? (iterations = +_, force) : iterations; | ||
}; | ||
force.strength = function(_) { | ||
return arguments.length ? (strength = typeof _ === "function" ? _ : constant(+_), initializeStrength(), force) : strength; | ||
}; | ||
force.strength = function(_) { | ||
return arguments.length ? (strength = typeof _ === "function" ? _ : constant(+_), initializeStrength(), force) : strength; | ||
}; | ||
force.distance = function(_) { | ||
return arguments.length ? (distance = typeof _ === "function" ? _ : constant(+_), initializeDistance(), force) : distance; | ||
}; | ||
force.distance = function(_) { | ||
return arguments.length ? (distance = typeof _ === "function" ? _ : constant(+_), initializeDistance(), force) : distance; | ||
}; | ||
return force; | ||
} | ||
return force; | ||
} | ||
function x$1(d) { | ||
return d.x; | ||
} | ||
function x$1(d) { | ||
return d.x; | ||
} | ||
function y$1(d) { | ||
return d.y; | ||
} | ||
function y$1(d) { | ||
return d.y; | ||
} | ||
var initialRadius = 10; | ||
var initialAngle = Math.PI * (3 - Math.sqrt(5)); | ||
function simulation(nodes) { | ||
var simulation, | ||
alpha = 1, | ||
alphaMin = 0.001, | ||
alphaDecay = 1 - Math.pow(alphaMin, 1 / 300), | ||
alphaTarget = 0, | ||
velocityDecay = 0.6, | ||
forces = d3Collection.map(), | ||
stepper = d3Timer.timer(step), | ||
event = d3Dispatch.dispatch("tick", "end"); | ||
var initialRadius = 10; | ||
var initialAngle = Math.PI * (3 - Math.sqrt(5)); | ||
if (nodes == null) nodes = []; | ||
var simulation = function(nodes) { | ||
var simulation, | ||
alpha = 1, | ||
alphaMin = 0.001, | ||
alphaDecay = 1 - Math.pow(alphaMin, 1 / 300), | ||
alphaTarget = 0, | ||
velocityDecay = 0.6, | ||
forces = d3Collection.map(), | ||
stepper = d3Timer.timer(step), | ||
event = d3Dispatch.dispatch("tick", "end"); | ||
function step() { | ||
tick(); | ||
event.call("tick", simulation); | ||
if (alpha < alphaMin) { | ||
stepper.stop(); | ||
event.call("end", simulation); | ||
} | ||
if (nodes == null) nodes = []; | ||
function step() { | ||
tick(); | ||
event.call("tick", simulation); | ||
if (alpha < alphaMin) { | ||
stepper.stop(); | ||
event.call("end", simulation); | ||
} | ||
} | ||
function tick() { | ||
var i, n = nodes.length, node; | ||
function tick() { | ||
var i, n = nodes.length, node; | ||
alpha += (alphaTarget - alpha) * alphaDecay; | ||
alpha += (alphaTarget - alpha) * alphaDecay; | ||
forces.each(function(force) { | ||
force(alpha); | ||
}); | ||
forces.each(function(force) { | ||
force(alpha); | ||
}); | ||
for (i = 0; i < n; ++i) { | ||
node = nodes[i]; | ||
if (node.fx == null) node.x += node.vx *= velocityDecay; | ||
else node.x = node.fx, node.vx = 0; | ||
if (node.fy == null) node.y += node.vy *= velocityDecay; | ||
else node.y = node.fy, node.vy = 0; | ||
} | ||
for (i = 0; i < n; ++i) { | ||
node = nodes[i]; | ||
if (node.fx == null) node.x += node.vx *= velocityDecay; | ||
else node.x = node.fx, node.vx = 0; | ||
if (node.fy == null) node.y += node.vy *= velocityDecay; | ||
else node.y = node.fy, node.vy = 0; | ||
} | ||
} | ||
function initializeNodes() { | ||
for (var i = 0, n = nodes.length, node; i < n; ++i) { | ||
node = nodes[i], node.index = i; | ||
if (isNaN(node.x) || isNaN(node.y)) { | ||
var radius = initialRadius * Math.sqrt(i), angle = i * initialAngle; | ||
node.x = radius * Math.cos(angle); | ||
node.y = radius * Math.sin(angle); | ||
} | ||
if (isNaN(node.vx) || isNaN(node.vy)) { | ||
node.vx = node.vy = 0; | ||
} | ||
function initializeNodes() { | ||
for (var i = 0, n = nodes.length, node; i < n; ++i) { | ||
node = nodes[i], node.index = i; | ||
if (isNaN(node.x) || isNaN(node.y)) { | ||
var radius = initialRadius * Math.sqrt(i), angle = i * initialAngle; | ||
node.x = radius * Math.cos(angle); | ||
node.y = radius * Math.sin(angle); | ||
} | ||
if (isNaN(node.vx) || isNaN(node.vy)) { | ||
node.vx = node.vy = 0; | ||
} | ||
} | ||
} | ||
function initializeForce(force) { | ||
if (force.initialize) force.initialize(nodes); | ||
return force; | ||
} | ||
function initializeForce(force) { | ||
if (force.initialize) force.initialize(nodes); | ||
return force; | ||
} | ||
initializeNodes(); | ||
initializeNodes(); | ||
return simulation = { | ||
tick: tick, | ||
return simulation = { | ||
tick: tick, | ||
restart: function() { | ||
return stepper.restart(step), simulation; | ||
}, | ||
restart: function() { | ||
return stepper.restart(step), simulation; | ||
}, | ||
stop: function() { | ||
return stepper.stop(), simulation; | ||
}, | ||
stop: function() { | ||
return stepper.stop(), simulation; | ||
}, | ||
nodes: function(_) { | ||
return arguments.length ? (nodes = _, initializeNodes(), forces.each(initializeForce), simulation) : nodes; | ||
}, | ||
nodes: function(_) { | ||
return arguments.length ? (nodes = _, initializeNodes(), forces.each(initializeForce), simulation) : nodes; | ||
}, | ||
alpha: function(_) { | ||
return arguments.length ? (alpha = +_, simulation) : alpha; | ||
}, | ||
alpha: function(_) { | ||
return arguments.length ? (alpha = +_, simulation) : alpha; | ||
}, | ||
alphaMin: function(_) { | ||
return arguments.length ? (alphaMin = +_, simulation) : alphaMin; | ||
}, | ||
alphaMin: function(_) { | ||
return arguments.length ? (alphaMin = +_, simulation) : alphaMin; | ||
}, | ||
alphaDecay: function(_) { | ||
return arguments.length ? (alphaDecay = +_, simulation) : +alphaDecay; | ||
}, | ||
alphaDecay: function(_) { | ||
return arguments.length ? (alphaDecay = +_, simulation) : +alphaDecay; | ||
}, | ||
alphaTarget: function(_) { | ||
return arguments.length ? (alphaTarget = +_, simulation) : alphaTarget; | ||
}, | ||
alphaTarget: function(_) { | ||
return arguments.length ? (alphaTarget = +_, simulation) : alphaTarget; | ||
}, | ||
velocityDecay: function(_) { | ||
return arguments.length ? (velocityDecay = 1 - _, simulation) : 1 - velocityDecay; | ||
}, | ||
velocityDecay: function(_) { | ||
return arguments.length ? (velocityDecay = 1 - _, simulation) : 1 - velocityDecay; | ||
}, | ||
force: function(name, _) { | ||
return arguments.length > 1 ? ((_ == null ? forces.remove(name) : forces.set(name, initializeForce(_))), simulation) : forces.get(name); | ||
}, | ||
force: function(name, _) { | ||
return arguments.length > 1 ? ((_ == null ? forces.remove(name) : forces.set(name, initializeForce(_))), simulation) : forces.get(name); | ||
}, | ||
find: function(x, y, radius) { | ||
var i = 0, | ||
n = nodes.length, | ||
dx, | ||
dy, | ||
d2, | ||
node, | ||
closest; | ||
find: function(x, y, radius) { | ||
var i = 0, | ||
n = nodes.length, | ||
dx, | ||
dy, | ||
d2, | ||
node, | ||
closest; | ||
if (radius == null) radius = Infinity; | ||
else radius *= radius; | ||
if (radius == null) radius = Infinity; | ||
else radius *= radius; | ||
for (i = 0; i < n; ++i) { | ||
node = nodes[i]; | ||
dx = x - node.x; | ||
dy = y - node.y; | ||
d2 = dx * dx + dy * dy; | ||
if (d2 < radius) closest = node, radius = d2; | ||
} | ||
return closest; | ||
}, | ||
on: function(name, _) { | ||
return arguments.length > 1 ? (event.on(name, _), simulation) : event.on(name); | ||
for (i = 0; i < n; ++i) { | ||
node = nodes[i]; | ||
dx = x - node.x; | ||
dy = y - node.y; | ||
d2 = dx * dx + dy * dy; | ||
if (d2 < radius) closest = node, radius = d2; | ||
} | ||
}; | ||
} | ||
function manyBody() { | ||
var nodes, | ||
node, | ||
alpha, | ||
strength = constant(-30), | ||
strengths, | ||
distanceMin2 = 1, | ||
distanceMax2 = Infinity, | ||
theta2 = 0.81; | ||
return closest; | ||
}, | ||
function force(_) { | ||
var i, n = nodes.length, tree = d3Quadtree.quadtree(nodes, x$1, y$1).visitAfter(accumulate); | ||
for (alpha = _, i = 0; i < n; ++i) node = nodes[i], tree.visit(apply); | ||
on: function(name, _) { | ||
return arguments.length > 1 ? (event.on(name, _), simulation) : event.on(name); | ||
} | ||
}; | ||
} | ||
function initialize() { | ||
if (!nodes) return; | ||
var i, n = nodes.length; | ||
strengths = new Array(n); | ||
for (i = 0; i < n; ++i) strengths[i] = +strength(nodes[i], i, nodes); | ||
} | ||
var manyBody = function() { | ||
var nodes, | ||
node, | ||
alpha, | ||
strength = constant(-30), | ||
strengths, | ||
distanceMin2 = 1, | ||
distanceMax2 = Infinity, | ||
theta2 = 0.81; | ||
function accumulate(quad) { | ||
var strength = 0, q, c, x, y, i; | ||
function force(_) { | ||
var i, n = nodes.length, tree = d3Quadtree.quadtree(nodes, x$1, y$1).visitAfter(accumulate); | ||
for (alpha = _, i = 0; i < n; ++i) node = nodes[i], tree.visit(apply); | ||
} | ||
// For internal nodes, accumulate forces from child quadrants. | ||
if (quad.length) { | ||
for (x = y = i = 0; i < 4; ++i) { | ||
if ((q = quad[i]) && (c = q.value)) { | ||
strength += c, x += c * q.x, y += c * q.y; | ||
} | ||
function initialize() { | ||
if (!nodes) return; | ||
var i, n = nodes.length; | ||
strengths = new Array(n); | ||
for (i = 0; i < n; ++i) strengths[i] = +strength(nodes[i], i, nodes); | ||
} | ||
function accumulate(quad) { | ||
var strength = 0, q, c, x$$1, y$$1, i; | ||
// For internal nodes, accumulate forces from child quadrants. | ||
if (quad.length) { | ||
for (x$$1 = y$$1 = i = 0; i < 4; ++i) { | ||
if ((q = quad[i]) && (c = q.value)) { | ||
strength += c, x$$1 += c * q.x, y$$1 += c * q.y; | ||
} | ||
quad.x = x / strength; | ||
quad.y = y / strength; | ||
} | ||
quad.x = x$$1 / strength; | ||
quad.y = y$$1 / strength; | ||
} | ||
// For leaf nodes, accumulate forces from coincident quadrants. | ||
else { | ||
q = quad; | ||
q.x = q.data.x; | ||
q.y = q.data.y; | ||
do strength += strengths[q.data.index]; | ||
while (q = q.next); | ||
} | ||
quad.value = strength; | ||
// For leaf nodes, accumulate forces from coincident quadrants. | ||
else { | ||
q = quad; | ||
q.x = q.data.x; | ||
q.y = q.data.y; | ||
do strength += strengths[q.data.index]; | ||
while (q = q.next); | ||
} | ||
function apply(quad, x1, _, x2) { | ||
if (!quad.value) return true; | ||
quad.value = strength; | ||
} | ||
var x = quad.x - node.x, | ||
y = quad.y - node.y, | ||
w = x2 - x1, | ||
l = x * x + y * y; | ||
function apply(quad, x1, _, x2) { | ||
if (!quad.value) return true; | ||
// Apply the Barnes-Hut approximation if possible. | ||
// Limit forces for very close nodes; randomize direction if coincident. | ||
if (w * w / theta2 < l) { | ||
if (l < distanceMax2) { | ||
if (x === 0) x = jiggle(), l += x * x; | ||
if (y === 0) y = jiggle(), l += y * y; | ||
if (l < distanceMin2) l = Math.sqrt(distanceMin2 * l); | ||
node.vx += x * quad.value * alpha / l; | ||
node.vy += y * quad.value * alpha / l; | ||
} | ||
return true; | ||
} | ||
var x$$1 = quad.x - node.x, | ||
y$$1 = quad.y - node.y, | ||
w = x2 - x1, | ||
l = x$$1 * x$$1 + y$$1 * y$$1; | ||
// Otherwise, process points directly. | ||
else if (quad.length || l >= distanceMax2) return; | ||
// Limit forces for very close nodes; randomize direction if coincident. | ||
if (quad.data !== node || quad.next) { | ||
if (x === 0) x = jiggle(), l += x * x; | ||
if (y === 0) y = jiggle(), l += y * y; | ||
// Apply the Barnes-Hut approximation if possible. | ||
// Limit forces for very close nodes; randomize direction if coincident. | ||
if (w * w / theta2 < l) { | ||
if (l < distanceMax2) { | ||
if (x$$1 === 0) x$$1 = jiggle(), l += x$$1 * x$$1; | ||
if (y$$1 === 0) y$$1 = jiggle(), l += y$$1 * y$$1; | ||
if (l < distanceMin2) l = Math.sqrt(distanceMin2 * l); | ||
node.vx += x$$1 * quad.value * alpha / l; | ||
node.vy += y$$1 * quad.value * alpha / l; | ||
} | ||
return true; | ||
} | ||
do if (quad.data !== node) { | ||
w = strengths[quad.data.index] * alpha / l; | ||
node.vx += x * w; | ||
node.vy += y * w; | ||
} while (quad = quad.next); | ||
// Otherwise, process points directly. | ||
else if (quad.length || l >= distanceMax2) return; | ||
// Limit forces for very close nodes; randomize direction if coincident. | ||
if (quad.data !== node || quad.next) { | ||
if (x$$1 === 0) x$$1 = jiggle(), l += x$$1 * x$$1; | ||
if (y$$1 === 0) y$$1 = jiggle(), l += y$$1 * y$$1; | ||
if (l < distanceMin2) l = Math.sqrt(distanceMin2 * l); | ||
} | ||
force.initialize = function(_) { | ||
nodes = _; | ||
initialize(); | ||
}; | ||
do if (quad.data !== node) { | ||
w = strengths[quad.data.index] * alpha / l; | ||
node.vx += x$$1 * w; | ||
node.vy += y$$1 * w; | ||
} while (quad = quad.next); | ||
} | ||
force.strength = function(_) { | ||
return arguments.length ? (strength = typeof _ === "function" ? _ : constant(+_), initialize(), force) : strength; | ||
}; | ||
force.initialize = function(_) { | ||
nodes = _; | ||
initialize(); | ||
}; | ||
force.distanceMin = function(_) { | ||
return arguments.length ? (distanceMin2 = _ * _, force) : Math.sqrt(distanceMin2); | ||
}; | ||
force.strength = function(_) { | ||
return arguments.length ? (strength = typeof _ === "function" ? _ : constant(+_), initialize(), force) : strength; | ||
}; | ||
force.distanceMax = function(_) { | ||
return arguments.length ? (distanceMax2 = _ * _, force) : Math.sqrt(distanceMax2); | ||
}; | ||
force.distanceMin = function(_) { | ||
return arguments.length ? (distanceMin2 = _ * _, force) : Math.sqrt(distanceMin2); | ||
}; | ||
force.theta = function(_) { | ||
return arguments.length ? (theta2 = _ * _, force) : Math.sqrt(theta2); | ||
}; | ||
force.distanceMax = function(_) { | ||
return arguments.length ? (distanceMax2 = _ * _, force) : Math.sqrt(distanceMax2); | ||
}; | ||
return force; | ||
} | ||
force.theta = function(_) { | ||
return arguments.length ? (theta2 = _ * _, force) : Math.sqrt(theta2); | ||
}; | ||
function x$2(x) { | ||
var strength = constant(0.1), | ||
nodes, | ||
strengths, | ||
xz; | ||
return force; | ||
} | ||
if (typeof x !== "function") x = constant(x == null ? 0 : +x); | ||
var x$2 = function(x) { | ||
var strength = constant(0.1), | ||
nodes, | ||
strengths, | ||
xz; | ||
function force(alpha) { | ||
for (var i = 0, n = nodes.length, node; i < n; ++i) { | ||
node = nodes[i], node.vx += (xz[i] - node.x) * strengths[i] * alpha; | ||
} | ||
if (typeof x !== "function") x = constant(x == null ? 0 : +x); | ||
function force(alpha) { | ||
for (var i = 0, n = nodes.length, node; i < n; ++i) { | ||
node = nodes[i], node.vx += (xz[i] - node.x) * strengths[i] * alpha; | ||
} | ||
} | ||
function initialize() { | ||
if (!nodes) return; | ||
var i, n = nodes.length; | ||
strengths = new Array(n); | ||
xz = new Array(n); | ||
for (i = 0; i < n; ++i) { | ||
strengths[i] = isNaN(xz[i] = +x(nodes[i], i, nodes)) ? 0 : +strength(nodes[i], i, nodes); | ||
} | ||
function initialize() { | ||
if (!nodes) return; | ||
var i, n = nodes.length; | ||
strengths = new Array(n); | ||
xz = new Array(n); | ||
for (i = 0; i < n; ++i) { | ||
strengths[i] = isNaN(xz[i] = +x(nodes[i], i, nodes)) ? 0 : +strength(nodes[i], i, nodes); | ||
} | ||
} | ||
force.initialize = function(_) { | ||
nodes = _; | ||
initialize(); | ||
}; | ||
force.initialize = function(_) { | ||
nodes = _; | ||
initialize(); | ||
}; | ||
force.strength = function(_) { | ||
return arguments.length ? (strength = typeof _ === "function" ? _ : constant(+_), initialize(), force) : strength; | ||
}; | ||
force.strength = function(_) { | ||
return arguments.length ? (strength = typeof _ === "function" ? _ : constant(+_), initialize(), force) : strength; | ||
}; | ||
force.x = function(_) { | ||
return arguments.length ? (x = typeof _ === "function" ? _ : constant(+_), initialize(), force) : x; | ||
}; | ||
force.x = function(_) { | ||
return arguments.length ? (x = typeof _ === "function" ? _ : constant(+_), initialize(), force) : x; | ||
}; | ||
return force; | ||
} | ||
return force; | ||
} | ||
function y$2(y) { | ||
var strength = constant(0.1), | ||
nodes, | ||
strengths, | ||
yz; | ||
var y$2 = function(y) { | ||
var strength = constant(0.1), | ||
nodes, | ||
strengths, | ||
yz; | ||
if (typeof y !== "function") y = constant(y == null ? 0 : +y); | ||
if (typeof y !== "function") y = constant(y == null ? 0 : +y); | ||
function force(alpha) { | ||
for (var i = 0, n = nodes.length, node; i < n; ++i) { | ||
node = nodes[i], node.vy += (yz[i] - node.y) * strengths[i] * alpha; | ||
} | ||
function force(alpha) { | ||
for (var i = 0, n = nodes.length, node; i < n; ++i) { | ||
node = nodes[i], node.vy += (yz[i] - node.y) * strengths[i] * alpha; | ||
} | ||
} | ||
function initialize() { | ||
if (!nodes) return; | ||
var i, n = nodes.length; | ||
strengths = new Array(n); | ||
yz = new Array(n); | ||
for (i = 0; i < n; ++i) { | ||
strengths[i] = isNaN(yz[i] = +y(nodes[i], i, nodes)) ? 0 : +strength(nodes[i], i, nodes); | ||
} | ||
function initialize() { | ||
if (!nodes) return; | ||
var i, n = nodes.length; | ||
strengths = new Array(n); | ||
yz = new Array(n); | ||
for (i = 0; i < n; ++i) { | ||
strengths[i] = isNaN(yz[i] = +y(nodes[i], i, nodes)) ? 0 : +strength(nodes[i], i, nodes); | ||
} | ||
} | ||
force.initialize = function(_) { | ||
nodes = _; | ||
initialize(); | ||
}; | ||
force.initialize = function(_) { | ||
nodes = _; | ||
initialize(); | ||
}; | ||
force.strength = function(_) { | ||
return arguments.length ? (strength = typeof _ === "function" ? _ : constant(+_), initialize(), force) : strength; | ||
}; | ||
force.strength = function(_) { | ||
return arguments.length ? (strength = typeof _ === "function" ? _ : constant(+_), initialize(), force) : strength; | ||
}; | ||
force.y = function(_) { | ||
return arguments.length ? (y = typeof _ === "function" ? _ : constant(+_), initialize(), force) : y; | ||
}; | ||
force.y = function(_) { | ||
return arguments.length ? (y = typeof _ === "function" ? _ : constant(+_), initialize(), force) : y; | ||
}; | ||
return force; | ||
} | ||
return force; | ||
} | ||
exports.forceCenter = center; | ||
exports.forceCollide = collide; | ||
exports.forceLink = link; | ||
exports.forceManyBody = manyBody; | ||
exports.forceSimulation = simulation; | ||
exports.forceX = x$2; | ||
exports.forceY = y$2; | ||
exports.forceCenter = center; | ||
exports.forceCollide = collide; | ||
exports.forceLink = link; | ||
exports.forceManyBody = manyBody; | ||
exports.forceSimulation = simulation; | ||
exports.forceX = x$2; | ||
exports.forceY = y$2; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
})); | ||
}))); |
@@ -1,2 +0,2 @@ | ||
// https://d3js.org/d3-force/ Version 1.0.2. Copyright 2016 Mike Bostock. | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("d3-quadtree"),require("d3-collection"),require("d3-dispatch"),require("d3-timer")):"function"==typeof define&&define.amd?define(["exports","d3-quadtree","d3-collection","d3-dispatch","d3-timer"],t):t(n.d3=n.d3||{},n.d3,n.d3,n.d3,n.d3)}(this,function(n,t,e,r,i){"use strict";function u(n,t){function e(){var e,i,u=r.length,o=0,f=0;for(e=0;e<u;++e)i=r[e],o+=i.x,f+=i.y;for(o=o/u-n,f=f/u-t,e=0;e<u;++e)i=r[e],i.x-=o,i.y-=f}var r;return null==n&&(n=0),null==t&&(t=0),e.initialize=function(n){r=n},e.x=function(t){return arguments.length?(n=+t,e):n},e.y=function(n){return arguments.length?(t=+n,e):t},e}function o(n){return function(){return n}}function f(){return 1e-6*(Math.random()-.5)}function a(n){return n.x+n.vx}function c(n){return n.y+n.vy}function l(n){function e(){function n(n,t,r,i,u){var o=n.data,a=n.r,c=g+a;{if(!o)return t>y+c||i<y-c||r>d+c||u<d-c;if(o.index>e){var h=y-o.x-o.vx,s=d-o.y-o.vy,p=h*h+s*s;p<c*c&&(0===h&&(h=f(),p+=h*h),0===s&&(s=f(),p+=s*s),p=(c-(p=Math.sqrt(p)))/p*l,v.vx+=(h*=p)*(c=(a*=a)/(x+a)),v.vy+=(s*=p)*c,o.vx-=h*(c=1-c),o.vy-=s*c)}}}for(var e,o,v,y,d,g,x,s=i.length,p=0;p<h;++p)for(o=t.quadtree(i,a,c).visitAfter(r),e=0;e<s;++e)v=i[e],g=u[e],x=g*g,y=v.x+v.vx,d=v.y+v.vy,o.visit(n)}function r(n){if(n.data)return n.r=u[n.data.index];for(var t=n.r=0;t<4;++t)n[t]&&n[t].r>n.r&&(n.r=n[t].r)}var i,u,l=1,h=1;return"function"!=typeof n&&(n=o(null==n?1:+n)),e.initialize=function(t){var e,r=(i=t).length;for(u=new Array(r),e=0;e<r;++e)u[e]=+n(i[e],e,i)},e.iterations=function(n){return arguments.length?(h=+n,e):h},e.strength=function(n){return arguments.length?(l=+n,e):l},e.radius=function(t){return arguments.length?(n="function"==typeof t?t:o(+t),e):n},e}function h(n,t){return t}function v(n){function t(n){return 1/Math.min(y[n.source.index],y[n.target.index])}function r(t){for(var e=0,r=n.length;e<p;++e)for(var i,u,o,a,h,v,y,g=0;g<r;++g)i=n[g],u=i.source,o=i.target,a=o.x+o.vx-u.x-u.vx||f(),h=o.y+o.vy-u.y-u.vy||f(),v=Math.sqrt(a*a+h*h),v=(v-l[g])/v*t*c[g],a*=v,h*=v,o.vx-=a*(y=d[g]),o.vy-=h*y,u.vx+=a*(y=1-y),u.vy+=h*y}function i(){if(v){var t,r,i=v.length,o=n.length,f=e.map(v,g);for(t=0,y=new Array(i);t<i;++t)y[t]=0;for(t=0;t<o;++t)r=n[t],r.index=t,"object"!=typeof r.source&&(r.source=f.get(r.source)),"object"!=typeof r.target&&(r.target=f.get(r.target)),++y[r.source.index],++y[r.target.index];for(t=0,d=new Array(o);t<o;++t)r=n[t],d[t]=y[r.source.index]/(y[r.source.index]+y[r.target.index]);c=new Array(o),u(),l=new Array(o),a()}}function u(){if(v)for(var t=0,e=n.length;t<e;++t)c[t]=+x(n[t],t,n)}function a(){if(v)for(var t=0,e=n.length;t<e;++t)l[t]=+s(n[t],t,n)}var c,l,v,y,d,g=h,x=t,s=o(30),p=1;return null==n&&(n=[]),r.initialize=function(n){v=n,i()},r.links=function(t){return arguments.length?(n=t,i(),r):n},r.id=function(n){return arguments.length?(g=n,r):g},r.iterations=function(n){return arguments.length?(p=+n,r):p},r.strength=function(n){return arguments.length?(x="function"==typeof n?n:o(+n),u(),r):x},r.distance=function(n){return arguments.length?(s="function"==typeof n?n:o(+n),a(),r):s},r}function y(n){return n.x}function d(n){return n.y}function g(n){function t(){u(),x.call("tick",a),c<l&&(g.stop(),x.call("end",a))}function u(){var t,e,r=n.length;for(c+=(v-c)*h,d.each(function(n){n(c)}),t=0;t<r;++t)e=n[t],null==e.fx?e.x+=e.vx*=y:(e.x=e.fx,e.vx=0),null==e.fy?e.y+=e.vy*=y:(e.y=e.fy,e.vy=0)}function o(){for(var t,e=0,r=n.length;e<r;++e){if(t=n[e],t.index=e,isNaN(t.x)||isNaN(t.y)){var i=M*Math.sqrt(e),u=e*q;t.x=i*Math.cos(u),t.y=i*Math.sin(u)}(isNaN(t.vx)||isNaN(t.vy))&&(t.vx=t.vy=0)}}function f(t){return t.initialize&&t.initialize(n),t}var a,c=1,l=.001,h=1-Math.pow(l,1/300),v=0,y=.6,d=e.map(),g=i.timer(t),x=r.dispatch("tick","end");return null==n&&(n=[]),o(),a={tick:u,restart:function(){return g.restart(t),a},stop:function(){return g.stop(),a},nodes:function(t){return arguments.length?(n=t,o(),d.each(f),a):n},alpha:function(n){return arguments.length?(c=+n,a):c},alphaMin:function(n){return arguments.length?(l=+n,a):l},alphaDecay:function(n){return arguments.length?(h=+n,a):+h},alphaTarget:function(n){return arguments.length?(v=+n,a):v},velocityDecay:function(n){return arguments.length?(y=1-n,a):1-y},force:function(n,t){return arguments.length>1?(null==t?d.remove(n):d.set(n,f(t)),a):d.get(n)},find:function(t,e,r){var i,u,o,f,a,c=0,l=n.length;for(null==r?r=1/0:r*=r,c=0;c<l;++c)f=n[c],i=t-f.x,u=e-f.y,o=i*i+u*u,o<r&&(a=f,r=o);return a},on:function(n,t){return arguments.length>1?(x.on(n,t),a):x.on(n)}}}function x(){function n(n){var e,o=u.length,f=t.quadtree(u,y,d).visitAfter(r);for(c=n,e=0;e<o;++e)a=u[e],f.visit(i)}function e(){if(u){var n,t=u.length;for(l=new Array(t),n=0;n<t;++n)l[n]=+h(u[n],n,u)}}function r(n){var t,e,r,i,u,o=0;if(n.length){for(r=i=u=0;u<4;++u)(t=n[u])&&(e=t.value)&&(o+=e,r+=e*t.x,i+=e*t.y);n.x=r/o,n.y=i/o}else{t=n,t.x=t.data.x,t.y=t.data.y;do o+=l[t.data.index];while(t=t.next)}n.value=o}function i(n,t,e,r){if(!n.value)return!0;var i=n.x-a.x,u=n.y-a.y,o=r-t,h=i*i+u*u;if(o*o/x<h)return h<g&&(0===i&&(i=f(),h+=i*i),0===u&&(u=f(),h+=u*u),h<v&&(h=Math.sqrt(v*h)),a.vx+=i*n.value*c/h,a.vy+=u*n.value*c/h),!0;if(!(n.length||h>=g)){(n.data!==a||n.next)&&(0===i&&(i=f(),h+=i*i),0===u&&(u=f(),h+=u*u),h<v&&(h=Math.sqrt(v*h)));do n.data!==a&&(o=l[n.data.index]*c/h,a.vx+=i*o,a.vy+=u*o);while(n=n.next)}}var u,a,c,l,h=o(-30),v=1,g=1/0,x=.81;return n.initialize=function(n){u=n,e()},n.strength=function(t){return arguments.length?(h="function"==typeof t?t:o(+t),e(),n):h},n.distanceMin=function(t){return arguments.length?(v=t*t,n):Math.sqrt(v)},n.distanceMax=function(t){return arguments.length?(g=t*t,n):Math.sqrt(g)},n.theta=function(t){return arguments.length?(x=t*t,n):Math.sqrt(x)},n}function s(n){function t(n){for(var t,e=0,o=r.length;e<o;++e)t=r[e],t.vx+=(u[e]-t.x)*i[e]*n}function e(){if(r){var t,e=r.length;for(i=new Array(e),u=new Array(e),t=0;t<e;++t)i[t]=isNaN(u[t]=+n(r[t],t,r))?0:+f(r[t],t,r)}}var r,i,u,f=o(.1);return"function"!=typeof n&&(n=o(null==n?0:+n)),t.initialize=function(n){r=n,e()},t.strength=function(n){return arguments.length?(f="function"==typeof n?n:o(+n),e(),t):f},t.x=function(r){return arguments.length?(n="function"==typeof r?r:o(+r),e(),t):n},t}function p(n){function t(n){for(var t,e=0,o=r.length;e<o;++e)t=r[e],t.vy+=(u[e]-t.y)*i[e]*n}function e(){if(r){var t,e=r.length;for(i=new Array(e),u=new Array(e),t=0;t<e;++t)i[t]=isNaN(u[t]=+n(r[t],t,r))?0:+f(r[t],t,r)}}var r,i,u,f=o(.1);return"function"!=typeof n&&(n=o(null==n?0:+n)),t.initialize=function(n){r=n,e()},t.strength=function(n){return arguments.length?(f="function"==typeof n?n:o(+n),e(),t):f},t.y=function(r){return arguments.length?(n="function"==typeof r?r:o(+r),e(),t):n},t}var M=10,q=Math.PI*(3-Math.sqrt(5));n.forceCenter=u,n.forceCollide=l,n.forceLink=v,n.forceManyBody=x,n.forceSimulation=g,n.forceX=s,n.forceY=p,Object.defineProperty(n,"__esModule",{value:!0})}); | ||
// https://d3js.org/d3-force/ Version 1.0.3. Copyright 2016 Mike Bostock. | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("d3-quadtree"),require("d3-collection"),require("d3-dispatch"),require("d3-timer")):"function"==typeof define&&define.amd?define(["exports","d3-quadtree","d3-collection","d3-dispatch","d3-timer"],t):t(n.d3=n.d3||{},n.d3,n.d3,n.d3,n.d3)}(this,function(n,t,r,e,i){"use strict";function u(n){return n.x+n.vx}function o(n){return n.y+n.vy}function f(n,t){return t}function a(n,t){var r=n.get(t);if(!r)throw new Error("missing: "+t);return r}function c(n){return n.x}function l(n){return n.y}var h=function(n,t){function r(){var r,i,u=e.length,o=0,f=0;for(r=0;r<u;++r)i=e[r],o+=i.x,f+=i.y;for(o=o/u-n,f=f/u-t,r=0;r<u;++r)i=e[r],i.x-=o,i.y-=f}var e;return null==n&&(n=0),null==t&&(t=0),r.initialize=function(n){e=n},r.x=function(t){return arguments.length?(n=+t,r):n},r.y=function(n){return arguments.length?(t=+n,r):t},r},v=function(n){return function(){return n}},y=function(){return 1e-6*(Math.random()-.5)},d=function(n){function r(){function n(n,t,e,i,u){var o=n.data,f=n.r,a=g+f;{if(!o)return t>v+a||i<v-a||e>d+a||u<d-a;if(o.index>r){var l=v-o.x-o.vx,x=d-o.y-o.vy,p=l*l+x*x;p<a*a&&(0===l&&(l=y(),p+=l*l),0===x&&(x=y(),p+=x*x),p=(a-(p=Math.sqrt(p)))/p*c,h.vx+=(l*=p)*(a=(f*=f)/(s+f)),h.vy+=(x*=p)*a,o.vx-=l*(a=1-a),o.vy-=x*a)}}}for(var r,i,h,v,d,g,s,x=f.length,p=0;p<l;++p)for(i=t.quadtree(f,u,o).visitAfter(e),r=0;r<x;++r)h=f[r],g=a[r],s=g*g,v=h.x+h.vx,d=h.y+h.vy,i.visit(n)}function e(n){if(n.data)return n.r=a[n.data.index];for(var t=n.r=0;t<4;++t)n[t]&&n[t].r>n.r&&(n.r=n[t].r)}function i(){if(f){var t,r=f.length;for(a=new Array(r),t=0;t<r;++t)a[t]=+n(f[t],t,f)}}var f,a,c=1,l=1;return"function"!=typeof n&&(n=v(null==n?1:+n)),r.initialize=function(n){f=n,i()},r.iterations=function(n){return arguments.length?(l=+n,r):l},r.strength=function(n){return arguments.length?(c=+n,r):c},r.radius=function(t){return arguments.length?(n="function"==typeof t?t:v(+t),i(),r):n},r},g=function(n){function t(n){return 1/Math.min(d[n.source.index],d[n.target.index])}function e(t){for(var r=0,e=n.length;r<M;++r)for(var i,u,o,f,a,h,v,d=0;d<e;++d)i=n[d],u=i.source,o=i.target,f=o.x+o.vx-u.x-u.vx||y(),a=o.y+o.vy-u.y-u.vy||y(),h=Math.sqrt(f*f+a*a),h=(h-l[d])/h*t*c[d],f*=h,a*=h,o.vx-=f*(v=g[d]),o.vy-=a*v,u.vx+=f*(v=1-v),u.vy+=a*v}function i(){if(h){var t,e,i=h.length,f=n.length,v=r.map(h,s);for(t=0,d=new Array(i);t<i;++t)d[t]=0;for(t=0;t<f;++t)e=n[t],e.index=t,"object"!=typeof e.source&&(e.source=a(v,e.source)),"object"!=typeof e.target&&(e.target=a(v,e.target)),++d[e.source.index],++d[e.target.index];for(t=0,g=new Array(f);t<f;++t)e=n[t],g[t]=d[e.source.index]/(d[e.source.index]+d[e.target.index]);c=new Array(f),u(),l=new Array(f),o()}}function u(){if(h)for(var t=0,r=n.length;t<r;++t)c[t]=+x(n[t],t,n)}function o(){if(h)for(var t=0,r=n.length;t<r;++t)l[t]=+p(n[t],t,n)}var c,l,h,d,g,s=f,x=t,p=v(30),M=1;return null==n&&(n=[]),e.initialize=function(n){h=n,i()},e.links=function(t){return arguments.length?(n=t,i(),e):n},e.id=function(n){return arguments.length?(s=n,e):s},e.iterations=function(n){return arguments.length?(M=+n,e):M},e.strength=function(n){return arguments.length?(x="function"==typeof n?n:v(+n),u(),e):x},e.distance=function(n){return arguments.length?(p="function"==typeof n?n:v(+n),o(),e):p},e},s=10,x=Math.PI*(3-Math.sqrt(5)),p=function(n){function t(){u(),p.call("tick",a),c<l&&(g.stop(),p.call("end",a))}function u(){var t,r,e=n.length;for(c+=(v-c)*h,d.each(function(n){n(c)}),t=0;t<e;++t)r=n[t],null==r.fx?r.x+=r.vx*=y:(r.x=r.fx,r.vx=0),null==r.fy?r.y+=r.vy*=y:(r.y=r.fy,r.vy=0)}function o(){for(var t,r=0,e=n.length;r<e;++r){if(t=n[r],t.index=r,isNaN(t.x)||isNaN(t.y)){var i=s*Math.sqrt(r),u=r*x;t.x=i*Math.cos(u),t.y=i*Math.sin(u)}(isNaN(t.vx)||isNaN(t.vy))&&(t.vx=t.vy=0)}}function f(t){return t.initialize&&t.initialize(n),t}var a,c=1,l=.001,h=1-Math.pow(l,1/300),v=0,y=.6,d=r.map(),g=i.timer(t),p=e.dispatch("tick","end");return null==n&&(n=[]),o(),a={tick:u,restart:function(){return g.restart(t),a},stop:function(){return g.stop(),a},nodes:function(t){return arguments.length?(n=t,o(),d.each(f),a):n},alpha:function(n){return arguments.length?(c=+n,a):c},alphaMin:function(n){return arguments.length?(l=+n,a):l},alphaDecay:function(n){return arguments.length?(h=+n,a):+h},alphaTarget:function(n){return arguments.length?(v=+n,a):v},velocityDecay:function(n){return arguments.length?(y=1-n,a):1-y},force:function(n,t){return arguments.length>1?(null==t?d.remove(n):d.set(n,f(t)),a):d.get(n)},find:function(t,r,e){var i,u,o,f,a,c=0,l=n.length;for(null==e?e=1/0:e*=e,c=0;c<l;++c)f=n[c],i=t-f.x,u=r-f.y,o=i*i+u*u,o<e&&(a=f,e=o);return a},on:function(n,t){return arguments.length>1?(p.on(n,t),a):p.on(n)}}},M=function(){function n(n){var r,a=u.length,h=t.quadtree(u,c,l).visitAfter(e);for(f=n,r=0;r<a;++r)o=u[r],h.visit(i)}function r(){if(u){var n,t=u.length;for(a=new Array(t),n=0;n<t;++n)a[n]=+h(u[n],n,u)}}function e(n){var t,r,e,i,u,o=0;if(n.length){for(e=i=u=0;u<4;++u)(t=n[u])&&(r=t.value)&&(o+=r,e+=r*t.x,i+=r*t.y);n.x=e/o,n.y=i/o}else{t=n,t.x=t.data.x,t.y=t.data.y;do o+=a[t.data.index];while(t=t.next)}n.value=o}function i(n,t,r,e){if(!n.value)return!0;var i=n.x-o.x,u=n.y-o.y,c=e-t,l=i*i+u*u;if(c*c/s<l)return l<g&&(0===i&&(i=y(),l+=i*i),0===u&&(u=y(),l+=u*u),l<d&&(l=Math.sqrt(d*l)),o.vx+=i*n.value*f/l,o.vy+=u*n.value*f/l),!0;if(!(n.length||l>=g)){(n.data!==o||n.next)&&(0===i&&(i=y(),l+=i*i),0===u&&(u=y(),l+=u*u),l<d&&(l=Math.sqrt(d*l)));do n.data!==o&&(c=a[n.data.index]*f/l,o.vx+=i*c,o.vy+=u*c);while(n=n.next)}}var u,o,f,a,h=v(-30),d=1,g=1/0,s=.81;return n.initialize=function(n){u=n,r()},n.strength=function(t){return arguments.length?(h="function"==typeof t?t:v(+t),r(),n):h},n.distanceMin=function(t){return arguments.length?(d=t*t,n):Math.sqrt(d)},n.distanceMax=function(t){return arguments.length?(g=t*t,n):Math.sqrt(g)},n.theta=function(t){return arguments.length?(s=t*t,n):Math.sqrt(s)},n},q=function(n){function t(n){for(var t,r=0,o=e.length;r<o;++r)t=e[r],t.vx+=(u[r]-t.x)*i[r]*n}function r(){if(e){var t,r=e.length;for(i=new Array(r),u=new Array(r),t=0;t<r;++t)i[t]=isNaN(u[t]=+n(e[t],t,e))?0:+o(e[t],t,e)}}var e,i,u,o=v(.1);return"function"!=typeof n&&(n=v(null==n?0:+n)),t.initialize=function(n){e=n,r()},t.strength=function(n){return arguments.length?(o="function"==typeof n?n:v(+n),r(),t):o},t.x=function(e){return arguments.length?(n="function"==typeof e?e:v(+e),r(),t):n},t},w=function(n){function t(n){for(var t,r=0,o=e.length;r<o;++r)t=e[r],t.vy+=(u[r]-t.y)*i[r]*n}function r(){if(e){var t,r=e.length;for(i=new Array(r),u=new Array(r),t=0;t<r;++t)i[t]=isNaN(u[t]=+n(e[t],t,e))?0:+o(e[t],t,e)}}var e,i,u,o=v(.1);return"function"!=typeof n&&(n=v(null==n?0:+n)),t.initialize=function(n){e=n,r()},t.strength=function(n){return arguments.length?(o="function"==typeof n?n:v(+n),r(),t):o},t.y=function(e){return arguments.length?(n="function"==typeof e?e:v(+e),r(),t):n},t};n.forceCenter=h,n.forceCollide=d,n.forceLink=g,n.forceManyBody=M,n.forceSimulation=p,n.forceX=q,n.forceY=w,Object.defineProperty(n,"__esModule",{value:!0})}); |
{ | ||
"name": "d3-force", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Force-directed graph layout using velocity Verlet integration.", | ||
@@ -10,3 +10,3 @@ "keywords": [ | ||
"network", | ||
"graphc", | ||
"graph", | ||
"force", | ||
@@ -42,5 +42,5 @@ "verlet", | ||
"devDependencies": { | ||
"eslint": "2", | ||
"eslint": "3", | ||
"package-preamble": "0.0", | ||
"rollup": "0.34", | ||
"rollup": "0.36", | ||
"tape": "4", | ||
@@ -47,0 +47,0 @@ "uglify-js": "2" |
100
README.md
@@ -9,3 +9,3 @@ # d3-force | ||
You can also simulate circles (disks) with collision, such as for [bubble charts](http://www.nytimes.com/interactive/2012/09/06/us/politics/convention-word-counts.html) or [beeswam plots](http://bl.ocks.org/mbostock/6526445e2b44303eebf21da3b6627320): | ||
You can also simulate circles (disks) with collision, such as for [bubble charts](http://www.nytimes.com/interactive/2012/09/06/us/politics/convention-word-counts.html) or [beeswarm plots](http://bl.ocks.org/mbostock/6526445e2b44303eebf21da3b6627320): | ||
@@ -43,15 +43,15 @@ [<img alt="Collision Detection" src="https://raw.githubusercontent.com/d3/d3-force/master/img/collide.png" width="420" height="219">](http://bl.ocks.org/mbostock/31ce330646fa8bcb7289ff3b97aab3f5)[<img alt="Beeswarm" src="https://raw.githubusercontent.com/d3/d3-force/master/img/beeswarm.png" width="420" height="219">](http://bl.ocks.org/mbostock/6526445e2b44303eebf21da3b6627320) | ||
<a name="forceSimulation" href="#forceSimulation">#</a> d3.<b>forceSimulation</b>([<i>nodes</i>]) | ||
<a name="forceSimulation" href="#forceSimulation">#</a> d3.<b>forceSimulation</b>([<i>nodes</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/simulation.js "Source") | ||
Creates a new simulation with the specified array of [*nodes*](#simulation_nodes) and no [forces](#simulation_force). If *nodes* is not specified, it defaults to the empty array. The simulator [starts](#simulation_restart) automatically; use [*simulation*.on](#simulation_on) to listen for tick events as the simulation runs. If you wish to run the simulation manually instead, call [*simulation*.stop](#simulation_stop), and then call [*simulation*.tick](#simulation_tick) as desired. | ||
<a name="simulation_restart" href="#simulation_restart">#</a> <i>simulation</i>.<b>restart</b>() | ||
<a name="simulation_restart" href="#simulation_restart">#</a> <i>simulation</i>.<b>restart</b>() [<>](https://github.com/d3/d3-force/blob/master/src/simulation.js#L80 "Source") | ||
Restarts the simulation’s internal timer and returns the simulation. In conjunction with [*simulation*.alphaTarget](#simulation_alphaTarget) or [*simulation*.alpha](#simulation_alpha), this method can be used to “reheat” the simulation during interaction, such as when dragging a node, or to resume the simulation after temporarily pausing it with [*simulation*.stop](#simulation_stop). | ||
<a name="simulation_stop" href="#simulation_stop">#</a> <i>simulation</i>.<b>stop</b>() | ||
<a name="simulation_stop" href="#simulation_stop">#</a> <i>simulation</i>.<b>stop</b>() [<>](https://github.com/d3/d3-force/blob/master/src/simulation.js#L84 "Source") | ||
Stops the simulation’s internal timer, if it is running, and returns the simulation. If the timer is already stopped, this method does nothing. This method is useful for running the simulation manually; see [*simulation*.tick](#simulation_tick). | ||
<a name="simulation_tick" href="#simulation_tick">#</a> <i>simulation</i>.<b>tick</b>() | ||
<a name="simulation_tick" href="#simulation_tick">#</a> <i>simulation</i>.<b>tick</b>() [<>](https://github.com/d3/d3-force/blob/master/src/simulation.js#L38 "Source") | ||
@@ -62,3 +62,3 @@ Increments the current [*alpha*](#simulation_alpha) by ([*alphaTarget*](#simulation_alphaTarget) - *alpha*) × [*alphaDecay*](#simulation_alphaDecay); then invokes each registered [force](#simulation_force), passing the new *alpha*; then decrements each [node](#simulation_nodes)’s velocity by *velocity* × [*velocityDecay*](#simulation_velocityDecay); lastly increments each node’s position by *velocity*. | ||
<a name="simulation_nodes" href="#simulation_nodes">#</a> <i>simulation</i>.<b>nodes</b>([<i>nodes</i>]) | ||
<a name="simulation_nodes" href="#simulation_nodes">#</a> <i>simulation</i>.<b>nodes</b>([<i>nodes</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/simulation.js#L88 "Source") | ||
@@ -86,11 +86,11 @@ If *nodes* is specified, sets the simulation’s nodes to the specified array of objects, initializing their positions and velocities if necessary, and then [re-initializes](#force_initialize) any bound [forces](#simulation_force); returns the simulation. If *nodes* is not specified, returns the simulation’s array of nodes as specified to the [constructor](#forceSimulation). | ||
<a name="simulation_alpha" href="#simulation_alpha">#</a> <i>simulation</i>.<b>alpha</b>([<i>alpha</i>]) | ||
<a name="simulation_alpha" href="#simulation_alpha">#</a> <i>simulation</i>.<b>alpha</b>([<i>alpha</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/simulation.js#L92 "Source") | ||
If *alpha* is specified, sets the current alpha to the specified number in the range [0,1] and returns this simulation. If *alpha* is not specified, returns the current alpha value, which defaults to 1. | ||
<a name="simulation_alphaMin" href="#simulation_alphaMin">#</a> <i>simulation</i>.<b>alphaMin</b>([<i>min</i>]) | ||
<a name="simulation_alphaMin" href="#simulation_alphaMin">#</a> <i>simulation</i>.<b>alphaMin</b>([<i>min</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/simulation.js#L96 "Source") | ||
If *min* is specified, sets the minimum *alpha* to the specified number in the range [0,1] and returns this simulation. If *min* is not specified, returns the current minimum *alpha* value, which defaults to 0.001. The simulation’s internal timer stops when the current [*alpha*](#simulation_alpha) is less than the minimum *alpha*. The default [alpha decay rate](#simulation_alphaDecay) of ~0.0228 corresponds to 300 iterations. | ||
<a name="simulation_alphaDecay" href="#simulation_alphaDecay">#</a> <i>simulation</i>.<b>alphaDecay</b>([<i>decay</i>]) | ||
<a name="simulation_alphaDecay" href="#simulation_alphaDecay">#</a> <i>simulation</i>.<b>alphaDecay</b>([<i>decay</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/simulation.js#L100 "Source") | ||
@@ -101,11 +101,11 @@ If *decay* is specified, sets the [*alpha*](#simulation_alpha) decay rate to the specified number in the range [0,1] and returns this simulation. If *decay* is not specified, returns the current *alpha* decay rate, which defaults to 0.0228… = 1 - *pow*(0.001, 1 / 300) where 0.001 is the default [minimum *alpha*](#simulation_alphaMin). | ||
<a name="simulation_alphaTarget" href="#simulation_alphaTarget">#</a> <i>simulation</i>.<b>alphaTarget</b>([<i>target</i>]) | ||
<a name="simulation_alphaTarget" href="#simulation_alphaTarget">#</a> <i>simulation</i>.<b>alphaTarget</b>([<i>target</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/simulation.js#L104 "Source") | ||
If *target* is specified, sets the current target [*alpha*](#simulation_alpha) to the specified number in the range [0,1] and returns this simulation. If *target* is not specified, returns the current target alpha value, which defaults to 0. | ||
<a name="simulation_velocityDecay" href="#simulation_velocityDecay">#</a> <i>simulation</i>.<b>velocityDecay</b>([<i>decay</i>]) | ||
<a name="simulation_velocityDecay" href="#simulation_velocityDecay">#</a> <i>simulation</i>.<b>velocityDecay</b>([<i>decay</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/simulation.js#L108 "Source") | ||
If *decay* is specified, sets the velocity decay factor to the specified number in the range [0,1] and returns this simulation. If *decay* is not specified, returns the current velocity decay factor, which defaults to 0.4. The decay factor is akin to atmospheric friction; after the application of any forces during a [tick](#simulation_tick), each node’s velocity is multiplied by 1 - *decay*. As with lowering the [alpha decay rate](#simulation_alphaDecay), less velocity decay may converge on a better solution, but risks numerical instabilities and oscillation. | ||
<a name="simulation_force" href="#simulation_force">#</a> <i>simulation</i>.<b>force</b>(<i>name</i>[, <i>force</i>]) | ||
<a name="simulation_force" href="#simulation_force">#</a> <i>simulation</i>.<b>force</b>(<i>name</i>[, <i>force</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/simulation.js#L112 "Source") | ||
@@ -121,7 +121,7 @@ If *force* is specified, assigns the [force](#forces) for the specified *name* and returns this simulation. If *force* is not specified, returns the force with the specified name, or undefined if there is no such force. (By default, new simulations have no forces.) For example, to create a new simulation to layout a graph, you might say: | ||
<a name="simulation_find" href="#simulation_find">#</a> <i>simulation</i>.<b>find</b>(<i>x</i>, <i>y</i>[, <i>radius</i>]) | ||
<a name="simulation_find" href="#simulation_find">#</a> <i>simulation</i>.<b>find</b>(<i>x</i>, <i>y</i>[, <i>radius</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/simulation.js#L116 "Source") | ||
Returns the node closest to the position ⟨*x*,*y*⟩ with the given search *radius*. If *radius* is not specified, it defaults to infinity. If there is no node within the search area, returns undefined. | ||
<a name="simulation_on" href="#simulation_on">#</a> <i>simulation</i>.<b>on</b>(<i>typenames</i>, [<i>listener</i>]) | ||
<a name="simulation_on" href="#simulation_on">#</a> <i>simulation</i>.<b>on</b>(<i>typenames</i>, [<i>listener</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/simulation.js#L139 "Source") | ||
@@ -165,7 +165,7 @@ If *listener* is specified, sets the event *listener* for the specified *typenames* and returns this simulation. If an event listener was already registered for the same type and name, the existing listener is removed before the new listener is added. If *listener* is null, removes the current event listeners for the specified *typenames*, if any. If *listener* is not specified, returns the first currently-assigned listener matching the specified *typenames*, if any. When a specified event is dispatched, each *listener* will be invoked with the `this` context as the simulation. | ||
<a name="_force" href="#_force">#</a> <i>force</i>(<i>alpha</i>) | ||
<a name="_force" href="#_force">#</a> <i>force</i>(<i>alpha</i>) [<>](https://github.com/d3/d3-force/blob/master/src/simulation.js#L44 "Source") | ||
Applies this force, optionally observing the specified *alpha*. Typically, the force is applied to the array of nodes previously passed to [*force*.initialize](#force_initialize), however, some forces may apply to a subset of nodes, or behave differently. For example, [d3.forceLink](#links) applies to the source and target of each link. | ||
<a name="force_initialize" href="#force_initialize">#</a> <i>force</i>.<b>initialize</b>(<i>nodes</i>) | ||
<a name="force_initialize" href="#force_initialize">#</a> <i>force</i>.<b>initialize</b>(<i>nodes</i>) [<>](https://github.com/d3/d3-force/blob/master/src/simulation.js#L71 "Source") | ||
@@ -178,11 +178,11 @@ Assigns the array of *nodes* to this force. This method is called when a force is bound to a simulation via [*simulation*.force](#simulation_force) and when the simulation’s nodes change via [*simulation*.nodes](#simulation_nodes). A force may perform necessary work during initialization, such as evaluating per-node parameters, to avoid repeatedly performing work during each application of the force. | ||
<a name="forceCenter" href="#forceCenter">#</a> d3.<b>forceCenter</b>([<i>x</i>, <i>y</i>]) | ||
<a name="forceCenter" href="#forceCenter">#</a> d3.<b>forceCenter</b>([<i>x</i>, <i>y</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/center.js#L1 "Source") | ||
Creates a new centering force with the specified [*x*-](#center_x) and [*y*-](#center_y) coordinates. If *x* and *y* are not specified, they default to ⟨0,0⟩. | ||
<a name="center_x" href="#center_x">#</a> <i>center</i>.<b>x</b>([<i>x</i>]) | ||
<a name="center_x" href="#center_x">#</a> <i>center</i>.<b>x</b>([<i>x</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/center.js#L27 "Source") | ||
If *x* is specified, sets the *x*-coordinate of the centering position to the specified number and returns this force. If *x* is not specified, returns the current *x*-coordinate, which defaults to zero. | ||
<a name="center_y" href="#center_y">#</a> <i>center</i>.<b>y</b>([<i>y</i>]) | ||
<a name="center_y" href="#center_y">#</a> <i>center</i>.<b>y</b>([<i>y</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/center.js#L31 "Source") | ||
@@ -195,7 +195,7 @@ If *y* is specified, sets the *y*-coordinate of the centering position to the specified number and returns this force. If *y* is not specified, returns the current *y*-coordinate, which defaults to zero. | ||
<a name="forceCollide" href="#forceCollide">#</a> d3.<b>forceCollide</b>([<i>radius</i>]) | ||
<a name="forceCollide" href="#forceCollide">#</a> d3.<b>forceCollide</b>([<i>radius</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/collide.js "Source") | ||
Creates a new circle collision force with the specified [*radius*](#collide_radius). If *radius* is not specified, it defaults to the constant one for all nodes. | ||
<a name="collide_radius" href="#collide_radius">#</a> <i>collide</i>.<b>radius</b>([<i>radius</i>]) | ||
<a name="collide_radius" href="#collide_radius">#</a> <i>collide</i>.<b>radius</b>([<i>radius</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/collide.js#L86 "Source") | ||
@@ -210,11 +210,11 @@ If *radius* is specified, sets the radius accessor to the specified number or function, re-evaluates the radius accessor for each node, and returns this force. If *radius* is not specified, returns the current radius accessor, which defaults to: | ||
The radius accessor is invoked for each [node](#simulation_nodes) in the simulation, being passed the *node* and its zero-based *index*. The resulting number is then stored internally, such that the radius of each node is only recomputed when the force is initialized or when this method is called, and not on every application of the force. | ||
The radius accessor is invoked for each [node](#simulation_nodes) in the simulation, being passed the *node* and its zero-based *index*. The resulting number is then stored internally, such that the radius of each node is only recomputed when the force is initialized or when this method is called with a new *radius*, and not on every application of the force. | ||
<a name="collide_strength" href="#collide_strength">#</a> <i>collide</i>.<b>strength</b>([<i>strength</i>]) | ||
<a name="collide_strength" href="#collide_strength">#</a> <i>collide</i>.<b>strength</b>([<i>strength</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/collide.js#L82 "Source") | ||
If *strength* is specified, sets the force strength to the specified number in the range [0,1] and returns this force. If *strength* is not specified, returns the current strength which defaults to 0.7. | ||
Overlapping nodes are resolved through iterative relaxation. For each node, the other nodes that are anticipated to overlap at the the at the next tick (using the anticipated positions ⟨*x* + *vx*,*y* + *vy*⟩) are determined; the node’s velocity is then modified to push the node out of each overlapping node. The change in velocity is dampened by the force’s strength such that the resolution of simultaneous overlaps can be blended together to find a stable solution. | ||
Overlapping nodes are resolved through iterative relaxation. For each node, the other nodes that are anticipated to overlap at the next tick (using the anticipated positions ⟨*x* + *vx*,*y* + *vy*⟩) are determined; the node’s velocity is then modified to push the node out of each overlapping node. The change in velocity is dampened by the force’s strength such that the resolution of simultaneous overlaps can be blended together to find a stable solution. | ||
<a name="collide_iterations" href="#collide_iterations">#</a> <i>collide</i>.<b>iterations</b>([<i>iterations</i>]) | ||
<a name="collide_iterations" href="#collide_iterations">#</a> <i>collide</i>.<b>iterations</b>([<i>iterations</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/collide.js#L78 "Source") | ||
@@ -227,7 +227,7 @@ If *iterations* is specified, sets the number of iterations per application to the specified number and returns this force. If *iterations* is not specified, returns the current iteration count which defaults to 1. Increasing the number of iterations greatly increases the rigidity of the constraint and avoids partial overlap of nodes, but also increases the runtime cost to evaluate the force. | ||
<a name="forceLink" href="#forceLink">#</a> d3.<b>forceLink</b>([<i>links</i>]) | ||
<a name="forceLink" href="#forceLink">#</a> d3.<b>forceLink</b>([<i>links</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/link.js "Source") | ||
Creates a new link force with the specified *links* and default parameters. If *links* is not specified, it defaults to the empty array. | ||
<a name="link_links" href="#link_links">#</a> <i>link</i>.<b>links</b>([<i>links</i>]) | ||
<a name="link_links" href="#link_links">#</a> <i>link</i>.<b>links</b>([<i>links</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/link.js#L92 "Source") | ||
@@ -246,3 +246,3 @@ If *links* is specified, sets the array of links associated with this force, recomputes the [distance](#link_distance) and [strength](#link_strength) parameters for each link, and returns this force. If *links* is not specified, returns the current array of links, which defaults to the empty array. | ||
<a name="link_id" href="#link_id">#</a> <i>link</i>.<b>id</b>([<i>id</i>]) | ||
<a name="link_id" href="#link_id">#</a> <i>link</i>.<b>id</b>([<i>id</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/link.js#L96 "Source") | ||
@@ -299,3 +299,3 @@ If *id* is specified, sets the node id accessor to the specified function and returns this force. If *id* is not specified, returns the current node id accessor, which defaults to the numeric index of the node: | ||
<a name="link_distance" href="#link_distance">#</a> <i>link</i>.<b>distance</b>([<i>distance</i>]) | ||
<a name="link_distance" href="#link_distance">#</a> <i>link</i>.<b>distance</b>([<i>distance</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/link.js#L108 "Source") | ||
@@ -310,5 +310,5 @@ If *distance* is specified, sets the distance accessor to the specified number or function, re-evaluates the distance accessor for each link, and returns this force. If *distance* is not specified, returns the current distance accessor, which defaults to: | ||
The distance accessor is invoked for each [link](#link_links), being passed the *link* and its zero-based *index*. The resulting number is then stored internally, such that the distance of each link is only recomputed when the force is initialized or when this method is called, and not on every application of the force. | ||
The distance accessor is invoked for each [link](#link_links), being passed the *link* and its zero-based *index*. The resulting number is then stored internally, such that the distance of each link is only recomputed when the force is initialized or when this method is called with a new *distance*, and not on every application of the force. | ||
<a name="link_strength" href="#link_strength">#</a> <i>link</i>.<b>strength</b>([<i>strength</i>]) | ||
<a name="link_strength" href="#link_strength">#</a> <i>link</i>.<b>strength</b>([<i>strength</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/link.js#L104 "Source") | ||
@@ -325,5 +325,5 @@ If *strength* is specified, sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each link, and returns this force. If *strength* is not specified, returns the current strength accessor, which defaults to: | ||
The strength accessor is invoked for each [link](#link_links), being passed the *link* and its zero-based *index*. The resulting number is then stored internally, such that the strength of each link is only recomputed when the force is initialized or when this method is called, and not on every application of the force. | ||
The strength accessor is invoked for each [link](#link_links), being passed the *link* and its zero-based *index*. The resulting number is then stored internally, such that the strength of each link is only recomputed when the force is initialized or when this method is called with a new *strength*, and not on every application of the force. | ||
<a name="link_iterations" href="#link_iterations">#</a> <i>link</i>.<b>iterations</b>([<i>iterations</i>]) | ||
<a name="link_iterations" href="#link_iterations">#</a> <i>link</i>.<b>iterations</b>([<i>iterations</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/link.js#L100 "Source") | ||
@@ -338,9 +338,9 @@ If *iterations* is specified, sets the number of iterations per application to the specified number and returns this force. If *iterations* is not specified, returns the current iteration count which defaults to 1. Increasing the number of iterations greatly increases the rigidity of the constraint and is useful for [complex structures such as lattices](http://bl.ocks.org/mbostock/1b64ec067fcfc51e7471d944f51f1611), but also increases the runtime cost to evaluate the force. | ||
<a name="forceManyBody" href="#forceManyBody">#</a> d3.<b>forceManyBody</b>() | ||
<a name="forceManyBody" href="#forceManyBody">#</a> d3.<b>forceManyBody</b>() [<>](https://github.com/d3/d3-force/blob/master/src/manyBody.js "Source") | ||
Creates a new many-body force with the default parameters. | ||
<a name="manyBody_strength" href="#manyBody_strength">#</a> <i>manyBody</i>.<b>strength</b>([<i>strength</i>]) | ||
<a name="manyBody_strength" href="#manyBody_strength">#</a> <i>manyBody</i>.<b>strength</b>([<i>strength</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/manyBody.js#L97 "Source") | ||
If *strength* is specified, sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each node, and returns this force. A positive value causes nodes to attract each other, similar to gravity, while a negative value causes nodes to repel each other, similar to electrostic charge. If *strength* is not specified, returns the current strength accessor, which defaults to: | ||
If *strength* is specified, sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each node, and returns this force. A positive value causes nodes to attract each other, similar to gravity, while a negative value causes nodes to repel each other, similar to electrostatic charge. If *strength* is not specified, returns the current strength accessor, which defaults to: | ||
@@ -353,5 +353,5 @@ ```js | ||
The strength accessor is invoked for each [node](#simulation_nodes) in the simulation, being passed the *node* and its zero-based *index*. The resulting number is then stored internally, such that the strength of each node is only recomputed when the force is initialized or when this method is called, and not on every application of the force. | ||
The strength accessor is invoked for each [node](#simulation_nodes) in the simulation, being passed the *node* and its zero-based *index*. The resulting number is then stored internally, such that the strength of each node is only recomputed when the force is initialized or when this method is called with a new *strength*, and not on every application of the force. | ||
<a name="manyBody_theta" href="#manyBody_theta">#</a> <i>manyBody</i>.<b>theta</b>([<i>theta</i>]) | ||
<a name="manyBody_theta" href="#manyBody_theta">#</a> <i>manyBody</i>.<b>theta</b>([<i>theta</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/manyBody.js#L109 "Source") | ||
@@ -362,7 +362,7 @@ If *theta* is specified, sets the Barnes–Hut approximation criterion to the specified number and returns this force. If *theta* is not specified, returns the current value, which defaults to 0.9. | ||
<a name="manyBody_distanceMin" href="#manyBody_distanceMin">#</a> <i>manyBody</i>.<b>distanceMin</b>([<i>distance</i>]) | ||
<a name="manyBody_distanceMin" href="#manyBody_distanceMin">#</a> <i>manyBody</i>.<b>distanceMin</b>([<i>distance</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/manyBody.js#L101 "Source") | ||
If *distance* is specified, sets the minimum distance between nodes over which this force is considered. If *distance* is not specified, returns the current minimum distance, which defaults to 1. A minimum distance establishes an upper bound on the strength of the force between two nearby nodes, avoiding instability. In particular, it avoids an infinitely-strong force if two nodes are exactly coincident; in this case, the direction of the force is random. | ||
<a name="manyBody_distanceMax" href="#manyBody_distanceMax">#</a> <i>manyBody</i>.<b>distanceMax</b>([<i>distance</i>]) | ||
<a name="manyBody_distanceMax" href="#manyBody_distanceMax">#</a> <i>manyBody</i>.<b>distanceMax</b>([<i>distance</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/manyBody.js#L105 "Source") | ||
@@ -375,7 +375,7 @@ If *distance* is specified, sets the maximum distance between nodes over which this force is considered. If *distance* is not specified, returns the current maximum distance, which defaults to infinity. Specifying a finite maximum distance improves performance and produces a more localized layout. | ||
<a name="forceX" href="#forceX">#</a> d3.<b>forceX</b>([<i>x</i>]) | ||
<a name="forceX" href="#forceX">#</a> d3.<b>forceX</b>([<i>x</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/x.js "Source") | ||
Creates a new positioning force along the *x*-axis towards the given position [*x*](#x_x). If *x* is not specified, it defaults to 0. | ||
<a name="x_strength" href="#x_strength">#</a> <i>x</i>.<b>strength</b>([<i>strength</i>]) | ||
<a name="x_strength" href="#x_strength">#</a> <i>x</i>.<b>strength</b>([<i>strength</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/x.js#L32 "Source") | ||
@@ -392,5 +392,5 @@ If *strength* is specified, sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each node, and returns this force. The *strength* determines how much to increment the node’s *x*-velocity: ([*x*](#x_x) - *node*.x) × *strength*. For example, a value of 0.1 indicates that the node should move a tenth of the way from its current *x*-position to the target *x*-position with each application. Higher values moves nodes more quickly to the target position, often at the expense of other forces or constraints. A value outside the range [0,1] is not recommended. | ||
The strength accessor is invoked for each [node](#simulation_nodes) in the simulation, being passed the *node* and its zero-based *index*. The resulting number is then stored internally, such that the strength of each node is only recomputed when the force is initialized or when this method is called, and not on every application of the force. | ||
The strength accessor is invoked for each [node](#simulation_nodes) in the simulation, being passed the *node* and its zero-based *index*. The resulting number is then stored internally, such that the strength of each node is only recomputed when the force is initialized or when this method is called with a new *strength*, and not on every application of the force. | ||
<a name="x_x" href="#x_x">#</a> <i>x</i>.<b>x</b>([<i>x</i>]) | ||
<a name="x_x" href="#x_x">#</a> <i>x</i>.<b>x</b>([<i>x</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/x.js#L36 "Source") | ||
@@ -405,9 +405,9 @@ If *x* is specified, sets the *x*-coordinate accessor to the specified number or function, re-evaluates the *x*-accessor for each node, and returns this force. If *x* is not specified, returns the current *x*-accessor, which defaults to: | ||
The *x*-accessor is invoked for each [node](#simulation_nodes) in the simulation, being passed the *node* and its zero-based *index*. The resulting number is then stored internally, such that the target *x*-coordinate of each node is only recomputed when the force is initialized or when this method is called, and not on every application of the force. | ||
The *x*-accessor is invoked for each [node](#simulation_nodes) in the simulation, being passed the *node* and its zero-based *index*. The resulting number is then stored internally, such that the target *x*-coordinate of each node is only recomputed when the force is initialized or when this method is called with a new *x*, and not on every application of the force. | ||
<a name="forceY" href="#forceY">#</a> d3.<b>forceY</b>([<i>y</i>]) | ||
<a name="forceY" href="#forceY">#</a> d3.<b>forceY</b>([<i>y</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/y.js "Source") | ||
Creates a new positioning force along the *y*-axis towards the given position [*y*](#y_y). If *y* is not specified, it defaults to 0. | ||
<a name="y_strength" href="#y_strength">#</a> <i>y</i>.<b>strength</b>([<i>strength</i>]) | ||
<a name="y_strength" href="#y_strength">#</a> <i>y</i>.<b>strength</b>([<i>strength</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/y.js#L32 "Source") | ||
@@ -424,5 +424,5 @@ If *strength* is specified, sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each node, and returns this force. The *strength* determines how much to increment the node’s *y*-velocity: ([*y*](#y_y) - *node*.y) × *strength*. For example, a value of 0.1 indicates that the node should move a tenth of the way from its current *y*-position to the target *y*-position with each application. Higher values moves nodes more quickly to the target position, often at the expense of other forces or constraints. A value outside the range [0,1] is not recommended. | ||
The strength accessor is invoked for each [node](#simulation_nodes) in the simulation, being passed the *node* and its zero-based *index*. The resulting number is then stored internally, such that the strength of each node is only recomputed when the force is initialized or when this method is called, and not on every application of the force. | ||
The strength accessor is invoked for each [node](#simulation_nodes) in the simulation, being passed the *node* and its zero-based *index*. The resulting number is then stored internally, such that the strength of each node is only recomputed when the force is initialized or when this method is called with a new *strength*, and not on every application of the force. | ||
<a name="y_y" href="#y_y">#</a> <i>y</i>.<b>y</b>([<i>y</i>]) | ||
<a name="y_y" href="#y_y">#</a> <i>y</i>.<b>y</b>([<i>y</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/y.js#L36 "Source") | ||
@@ -437,2 +437,2 @@ If *y* is specified, sets the *y*-coordinate accessor to the specified number or function, re-evaluates the *y*-accessor for each node, and returns this force. If *y* is not specified, returns the current *y*-accessor, which defaults to: | ||
The *y*-accessor is invoked for each [node](#simulation_nodes) in the simulation, being passed the *node* and its zero-based *index*. The resulting number is then stored internally, such that the target *y*-coordinate of each node is only recomputed when the force is initialized or when this method is called, and not on every application of the force. | ||
The *y*-accessor is invoked for each [node](#simulation_nodes) in the simulation, being passed the *node* and its zero-based *index*. The resulting number is then stored internally, such that the target *y*-coordinate of each node is only recomputed when the force is initialized or when this method is called with a new *y*, and not on every application of the force. |
@@ -73,5 +73,12 @@ import constant from "./constant"; | ||
function initialize() { | ||
if (!nodes) return; | ||
var i, n = nodes.length; | ||
radii = new Array(n); | ||
for (i = 0; i < n; ++i) radii[i] = +radius(nodes[i], i, nodes); | ||
} | ||
force.initialize = function(_) { | ||
var i, n = (nodes = _).length; radii = new Array(n); | ||
for (i = 0; i < n; ++i) radii[i] = +radius(nodes[i], i, nodes); | ||
nodes = _; | ||
initialize(); | ||
}; | ||
@@ -88,3 +95,3 @@ | ||
force.radius = function(_) { | ||
return arguments.length ? (radius = typeof _ === "function" ? _ : constant(+_), force) : radius; | ||
return arguments.length ? (radius = typeof _ === "function" ? _ : constant(+_), initialize(), force) : radius; | ||
}; | ||
@@ -91,0 +98,0 @@ |
@@ -9,2 +9,8 @@ import constant from "./constant"; | ||
function find(nodeById, nodeId) { | ||
var node = nodeById.get(nodeId); | ||
if (!node) throw new Error("missing: " + nodeId); | ||
return node; | ||
} | ||
export default function(links) { | ||
@@ -59,4 +65,4 @@ var id = index, | ||
link = links[i], link.index = i; | ||
if (typeof link.source !== "object") link.source = nodeById.get(link.source); | ||
if (typeof link.target !== "object") link.target = nodeById.get(link.target); | ||
if (typeof link.source !== "object") link.source = find(nodeById, link.source); | ||
if (typeof link.target !== "object") link.target = find(nodeById, link.target); | ||
++count[link.source.index], ++count[link.target.index]; | ||
@@ -63,0 +69,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
395899
992