Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

d3-force

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

d3-force - npm Package Compare versions

Comparing version 0.2.0 to 0.2.1

32

build/d3-force.js

@@ -7,3 +7,3 @@ (function (global, factory) {

var version = "0.2.0";
var version = "0.2.1";

@@ -61,2 +61,4 @@ function center(x, y) {

var initialRadius = 10;
var initialAngle = Math.PI * (3 - Math.sqrt(5));
function simulation(nodes) {

@@ -110,6 +112,10 @@ var simulation,

node = nodes[i], node.index = i;
if (isNaN(node.x)) node.x = Math.random() * 100 - 50;
if (isNaN(node.y)) node.y = Math.random() * 100 - 50;
if (isNaN(node.vx)) node.vx = 0;
if (isNaN(node.vy)) node.vy = 0;
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;
}
}

@@ -425,12 +431,7 @@ }

// Apply the Barnes-Hut approximation if possible.
// Limit forces for very close nodes.
// Randomize direction for exactly-coincident nodes.
if (l < distanceMin2) {
if (!l) l = Math.random() * tau$1, x = Math.cos(l), y = Math.sin(l), l = 1;
l = Math.sqrt(l / distanceMin2), x /= l, y /= l, l = distanceMin2;
}
// Apply the Barnes-Hut approximation if possible.
if (w * w / theta2 < l) {
if (l < distanceMax2) {
if (l < distanceMin2) l = Math.sqrt(l / distanceMin2), x /= l, y /= l, l = distanceMin2;
l = quad.value * alpha / l;

@@ -446,2 +447,9 @@ node.vx += x * l;

// Limit forces for very close nodes.
// Randomize direction for exactly-coincident nodes.
if (l < distanceMin2) {
if (!l) l = Math.random() * tau$1, x = Math.cos(l), y = Math.sin(l), l = 1;
l = Math.sqrt(l / distanceMin2), x /= l, y /= l, l = distanceMin2;
}
do if (quad.data !== node) {

@@ -448,0 +456,0 @@ w = strengths[quad.data.index] * alpha / l;

@@ -1,1 +0,1 @@

!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("d3-quadtree"),require("d3-dispatch"),require("d3-collection"),require("d3-timer")):"function"==typeof define&&define.amd?define(["exports","d3-quadtree","d3-dispatch","d3-collection","d3-timer"],t):t(n.d3_force=n.d3_force||{},n.d3_quadtree,n.d3_dispatch,n.d3_collection,n.d3_timer)}(this,function(n,t,e,r,i){"use strict";function o(n,t){function e(){var e,i,o=r.length,u=0,f=0;for(e=0;o>e;++e)i=r[e],u+=i.x,f+=i.y;for(u=u/o-n,f=f/o-t,e=0;o>e;++e)i=r[e],i.x-=u,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 u(n){return function(){return n}}function f(n){return n.x}function a(n){return n.y}function c(n){function t(){return l=0,s.restart(u),c}function o(){return s.stop(),c}function u(){var t=Math.exp(++l*d);if(!(t>h))return s.stop(),void v.call("end",c);g.each(function(n){n(t)});for(var e,r=0,i=n.length;i>r;++r)e=n[r],e.x+=e.vx*=y,e.y+=e.vy*=y;v.call("tick",c)}function f(){for(var t,e=0,r=n.length;r>e;++e)t=n[e],t.index=e,isNaN(t.x)&&(t.x=100*Math.random()-50),isNaN(t.y)&&(t.y=100*Math.random()-50),isNaN(t.vx)&&(t.vx=0),isNaN(t.vy)&&(t.vy=0)}function a(t){return t.initialize&&t.initialize(n),t}var c,l=0,h=1e-4,d=-.02,y=.5,g=r.map(),s=i.timer(u),v=e.dispatch("tick","end");return null==n&&(n=[]),f(),c={start:t,stop:o,tick:u,nodes:function(t){return arguments.length?(n=t,f(),g.each(a),c):n},alphaMin:function(n){return arguments.length?(h=n,c):h},alphaDecay:function(n){return arguments.length?(l=+n?Math.round(l*d/-n):0,d=-n,c):-d},drag:function(n){return arguments.length?(y=1-n,c):1-y},force:function(n,t){return arguments.length>1?(null==t?g.remove(n):g.set(n,a(t)),c):g.get(n)},on:function(n,t){return arguments.length>1?(v.on(n,t),c):v.on(n)}}}function l(n){function e(){function n(n,t,r,o,f){if(t>d||l>o||r>y||h>f)return!0;if(!n.length){var a=u.x-n.data.x,c=u.y-n.data.y,v=a*a+c*c,x=i[e]+i[n.data.index];x*x>v&&(v=(x-(v=Math.sqrt(v)))/v,g+=a*v,s+=c*v)}}var e,u,l,h,d,y,g,s,v,x=r.length,p=t.quadtree(r,f,a);for(e=0;x>e;++e)u=r[e],v=i[e]+o,g=s=0,l=u.x-v,h=u.y-v,d=u.x+v,y=u.y+v,p.remove(u).visit(n),u.x+=g*c,u.y+=s*c,p.add(u)}var r,i,o,c=.7;return"function"!=typeof n&&(n=u(null==n?1:+n)),e.initialize=function(t){var e,u,f=(r=t).length;for(i=new Array(f),o=0,e=0;f>e;++e)(i[e]=u=+n(r[e],e,r))>o&&(o=u)},e.strength=function(n){return arguments.length?(c=+n,e):c},e.radius=function(t){return arguments.length?(n="function"==typeof t?t:u(+t),e):n},e}function h(n,t,e){function r(){for(var n,t,e,r,i,u=0,l=o.length;l>u;++u)n=o[u],t=n.x-a[u],e=n.y-c[u],(r=t*t+e*e)>(i=f[u])*i&&(r=Math.sqrt(r),r=(r-i)/r,n.vx-=t*r,n.vy-=e*r)}function i(){if(o){var r,i=o.length;for(f=new Array(i),a=new Array(i),c=new Array(i),r=0;i>r;++r)f[r]=+n(o[r],r,o),a[r]=+t(o[r],r,o),c[r]=+e(o[r],r,o)}}var o,f,a,c;return"function"!=typeof n&&(n=u(null==n?100:+n)),"function"!=typeof t&&(t=u(null==t?0:+t)),"function"!=typeof e&&(e=u(null==e?0:+e)),r.initialize=function(n){o=n,i()},r.radius=function(t){return arguments.length?(n="function"==typeof t?t:u(+t),i(),r):n},r.x=function(n){return arguments.length?(t="function"==typeof n?n:u(+n),i(),r):t},r.y=function(n){return arguments.length?(e="function"==typeof n?n:u(+n),i(),r):e},r}function d(n,t){return t}function y(n){function t(t){for(var e,r,u,f,c,l,h,d=0,y=n.length;y>d;++d)e=n[d],r=e.source,u=e.target,f=u.x-r.x,c=u.y-r.y,(l=f*f+c*c)?(l=Math.sqrt(l),l=(l-o[d])/l):(l=Math.random()*x,f=Math.cos(l),c=Math.sin(l),l=o[d]),l*=t*i[d],f*=l,c*=l,u.vx-=f*(h=a[d]),u.vy-=c*h,r.vx+=f*(h=1-h),r.vy+=c*h}function e(){if(f&&n){var t,e,u=f.length,d=n.length,y=new Array(u),g=r.map(f,c);for(t=0;u>t;++t)y[t]=0;for(t=0,a=new Array(d);d>t;++t)e=n[t],e.index=t,"object"!=typeof e.source&&(e.source=g.get(e.source)),"object"!=typeof e.target&&(e.target=g.get(e.target)),++y[e.source.index],++y[e.target.index];for(t=0;d>t;++t)e=n[t],a[t]=y[e.source.index]/(y[e.source.index]+y[e.target.index]);if(!i)for(t=0,i=new Array(d);d>t;++t)i[t]=+l(n[t]);if(!o)for(t=0,o=new Array(d);d>t;++t)o[t]=+h(n[t])}}var i,o,f,a,c=d,l=u(.5),h=u(30);return null==n&&(n=[]),t.initialize=function(n){f=n,e()},t.links=function(r){return arguments.length?(n=r,i=o=null,e(),t):n},t.id=function(n){return arguments.length?(c=n,e(),t):c},t.strength=function(n){return arguments.length?(l="function"==typeof n?n:u(+n),i=null,e(),t):l},t.distance=function(n){return arguments.length?(h="function"==typeof n?n:u(+n),o=null,e(),t):h},t}function g(){function n(n){var e,u=o.length,h=t.quadtree(o,f,a).visitAfter(r);for(l=n,e=0;u>e;++e)c=o[e],h.visit(i)}function e(){if(o){var n,t=o.length;for(h=new Array(t),n=0;t>n;++n)h[n]=+d(o[n],n,o)}}function r(n){var t,e,r,i,o,u=0;if(n.length){for(r=i=o=0;4>o;++o)(t=n[o])&&(e=t.value)&&(u+=e,r+=e*t.x,i+=e*t.y);n.x=r/u,n.y=i/u}else{t=n,t.x=t.data.x,t.y=t.data.y;do u+=h[t.data.index];while(t=t.next)}n.value=u}function i(n,t,e,r){if(!n.value)return!0;var i=n.x-c.x,o=n.y-c.y,u=r-t,f=i*i+o*o;if(y>f&&(f||(f=Math.random()*p,i=Math.cos(f),o=Math.sin(f),f=1),f=Math.sqrt(f/y),i/=f,o/=f,f=y),f>u*u/s)return g>f&&(f=n.value*l/f,c.vx+=i*f,c.vy+=o*f),!0;if(!(n.length||f>=g))do n.data!==c&&(u=h[n.data.index]*l/f,c.vx+=i*u,c.vy+=o*u);while(n=n.next)}var o,c,l,h,d=u(-100),y=1,g=1/0,s=.81;return n.initialize=function(n){o=n,e()},n.strength=function(t){return arguments.length?(d="function"==typeof t?t:u(+t),e(),n):d},n.distanceMin=function(t){return arguments.length?(y=t*t,n):Math.sqrt(y)},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}function s(n,t){function e(n){for(var t,e,r=0,u=i.length;u>r;++r)t=i[r],e=o[r]*n,t.vx+=(f[r]-t.x)*e,t.vy+=(a[r]-t.y)*e}function r(){if(i){var e,r=i.length;for(o=new Array(r),f=new Array(r),a=new Array(r),e=0;r>e;++e)o[e]=+c(i[e],e,i),f[e]=+n(i[e],e,i),a[e]=+t(i[e],e,i)}}var i,o,f,a,c=u(.1);return"function"!=typeof n&&(n=u(null==n?0:+n)),"function"!=typeof t&&(t=u(null==t?0:+t)),e.initialize=function(n){i=n,r()},e.strength=function(n){return arguments.length?(c="function"==typeof n?n:u(+n),r(),e):c},e.x=function(t){return arguments.length?(n="function"==typeof t?t:u(+t),r(),e):n},e.y=function(n){return arguments.length?(t="function"==typeof n?n:u(+n),r(),e):t},e}var v="0.2.0",x=2*Math.PI,p=2*Math.PI;n.version=v,n.forceCenter=o,n.forceCollide=l,n.forceContain=h,n.forceLink=y,n.forceManyBody=g,n.forcePosition=s,n.forceSimulation=c});
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("d3-quadtree"),require("d3-dispatch"),require("d3-collection"),require("d3-timer")):"function"==typeof define&&define.amd?define(["exports","d3-quadtree","d3-dispatch","d3-collection","d3-timer"],t):t(n.d3_force=n.d3_force||{},n.d3_quadtree,n.d3_dispatch,n.d3_collection,n.d3_timer)}(this,function(n,t,e,r,i){"use strict";function o(n,t){function e(){var e,i,o=r.length,u=0,f=0;for(e=0;o>e;++e)i=r[e],u+=i.x,f+=i.y;for(u=u/o-n,f=f/o-t,e=0;o>e;++e)i=r[e],i.x-=u,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 u(n){return function(){return n}}function f(n){return n.x}function a(n){return n.y}function c(n){function t(){return l=0,g.restart(u),c}function o(){return g.stop(),c}function u(){var t=Math.exp(++l*y);if(!(t>h))return g.stop(),void v.call("end",c);s.each(function(n){n(t)});for(var e,r=0,i=n.length;i>r;++r)e=n[r],e.x+=e.vx*=d,e.y+=e.vy*=d;v.call("tick",c)}function f(){for(var t,e=0,r=n.length;r>e;++e){if(t=n[e],t.index=e,isNaN(t.x)||isNaN(t.y)){var i=x*Math.sqrt(e),o=e*p;t.x=i*Math.cos(o),t.y=i*Math.sin(o)}(isNaN(t.vx)||isNaN(t.vy))&&(t.vx=t.vy=0)}}function a(t){return t.initialize&&t.initialize(n),t}var c,l=0,h=1e-4,y=-.02,d=.5,s=r.map(),g=i.timer(u),v=e.dispatch("tick","end");return null==n&&(n=[]),f(),c={start:t,stop:o,tick:u,nodes:function(t){return arguments.length?(n=t,f(),s.each(a),c):n},alphaMin:function(n){return arguments.length?(h=n,c):h},alphaDecay:function(n){return arguments.length?(l=+n?Math.round(l*y/-n):0,y=-n,c):-y},drag:function(n){return arguments.length?(d=1-n,c):1-d},force:function(n,t){return arguments.length>1?(null==t?s.remove(n):s.set(n,a(t)),c):s.get(n)},on:function(n,t){return arguments.length>1?(v.on(n,t),c):v.on(n)}}}function l(n){function e(){function n(n,t,r,o,f){if(t>y||l>o||r>d||h>f)return!0;if(!n.length){var a=u.x-n.data.x,c=u.y-n.data.y,v=a*a+c*c,x=i[e]+i[n.data.index];x*x>v&&(v=(x-(v=Math.sqrt(v)))/v,s+=a*v,g+=c*v)}}var e,u,l,h,y,d,s,g,v,x=r.length,p=t.quadtree(r,f,a);for(e=0;x>e;++e)u=r[e],v=i[e]+o,s=g=0,l=u.x-v,h=u.y-v,y=u.x+v,d=u.y+v,p.remove(u).visit(n),u.x+=s*c,u.y+=g*c,p.add(u)}var r,i,o,c=.7;return"function"!=typeof n&&(n=u(null==n?1:+n)),e.initialize=function(t){var e,u,f=(r=t).length;for(i=new Array(f),o=0,e=0;f>e;++e)(i[e]=u=+n(r[e],e,r))>o&&(o=u)},e.strength=function(n){return arguments.length?(c=+n,e):c},e.radius=function(t){return arguments.length?(n="function"==typeof t?t:u(+t),e):n},e}function h(n,t,e){function r(){for(var n,t,e,r,i,u=0,l=o.length;l>u;++u)n=o[u],t=n.x-a[u],e=n.y-c[u],(r=t*t+e*e)>(i=f[u])*i&&(r=Math.sqrt(r),r=(r-i)/r,n.vx-=t*r,n.vy-=e*r)}function i(){if(o){var r,i=o.length;for(f=new Array(i),a=new Array(i),c=new Array(i),r=0;i>r;++r)f[r]=+n(o[r],r,o),a[r]=+t(o[r],r,o),c[r]=+e(o[r],r,o)}}var o,f,a,c;return"function"!=typeof n&&(n=u(null==n?100:+n)),"function"!=typeof t&&(t=u(null==t?0:+t)),"function"!=typeof e&&(e=u(null==e?0:+e)),r.initialize=function(n){o=n,i()},r.radius=function(t){return arguments.length?(n="function"==typeof t?t:u(+t),i(),r):n},r.x=function(n){return arguments.length?(t="function"==typeof n?n:u(+n),i(),r):t},r.y=function(n){return arguments.length?(e="function"==typeof n?n:u(+n),i(),r):e},r}function y(n,t){return t}function d(n){function t(t){for(var e,r,u,f,c,l,h,y=0,d=n.length;d>y;++y)e=n[y],r=e.source,u=e.target,f=u.x-r.x,c=u.y-r.y,(l=f*f+c*c)?(l=Math.sqrt(l),l=(l-o[y])/l):(l=Math.random()*M,f=Math.cos(l),c=Math.sin(l),l=o[y]),l*=t*i[y],f*=l,c*=l,u.vx-=f*(h=a[y]),u.vy-=c*h,r.vx+=f*(h=1-h),r.vy+=c*h}function e(){if(f&&n){var t,e,u=f.length,y=n.length,d=new Array(u),s=r.map(f,c);for(t=0;u>t;++t)d[t]=0;for(t=0,a=new Array(y);y>t;++t)e=n[t],e.index=t,"object"!=typeof e.source&&(e.source=s.get(e.source)),"object"!=typeof e.target&&(e.target=s.get(e.target)),++d[e.source.index],++d[e.target.index];for(t=0;y>t;++t)e=n[t],a[t]=d[e.source.index]/(d[e.source.index]+d[e.target.index]);if(!i)for(t=0,i=new Array(y);y>t;++t)i[t]=+l(n[t]);if(!o)for(t=0,o=new Array(y);y>t;++t)o[t]=+h(n[t])}}var i,o,f,a,c=y,l=u(.5),h=u(30);return null==n&&(n=[]),t.initialize=function(n){f=n,e()},t.links=function(r){return arguments.length?(n=r,i=o=null,e(),t):n},t.id=function(n){return arguments.length?(c=n,e(),t):c},t.strength=function(n){return arguments.length?(l="function"==typeof n?n:u(+n),i=null,e(),t):l},t.distance=function(n){return arguments.length?(h="function"==typeof n?n:u(+n),o=null,e(),t):h},t}function s(){function n(n){var e,u=o.length,h=t.quadtree(o,f,a).visitAfter(r);for(l=n,e=0;u>e;++e)c=o[e],h.visit(i)}function e(){if(o){var n,t=o.length;for(h=new Array(t),n=0;t>n;++n)h[n]=+y(o[n],n,o)}}function r(n){var t,e,r,i,o,u=0;if(n.length){for(r=i=o=0;4>o;++o)(t=n[o])&&(e=t.value)&&(u+=e,r+=e*t.x,i+=e*t.y);n.x=r/u,n.y=i/u}else{t=n,t.x=t.data.x,t.y=t.data.y;do u+=h[t.data.index];while(t=t.next)}n.value=u}function i(n,t,e,r){if(!n.value)return!0;var i=n.x-c.x,o=n.y-c.y,u=r-t,f=i*i+o*o;if(f>u*u/g)return s>f&&(d>f&&(f=Math.sqrt(f/d),i/=f,o/=f,f=d),f=n.value*l/f,c.vx+=i*f,c.vy+=o*f),!0;if(!(n.length||f>=s)){d>f&&(f||(f=Math.random()*q,i=Math.cos(f),o=Math.sin(f),f=1),f=Math.sqrt(f/d),i/=f,o/=f,f=d);do n.data!==c&&(u=h[n.data.index]*l/f,c.vx+=i*u,c.vy+=o*u);while(n=n.next)}}var o,c,l,h,y=u(-100),d=1,s=1/0,g=.81;return n.initialize=function(n){o=n,e()},n.strength=function(t){return arguments.length?(y="function"==typeof t?t:u(+t),e(),n):y},n.distanceMin=function(t){return arguments.length?(d=t*t,n):Math.sqrt(d)},n.distanceMax=function(t){return arguments.length?(s=t*t,n):Math.sqrt(s)},n.theta=function(t){return arguments.length?(g=t*t,n):Math.sqrt(g)},n}function g(n,t){function e(n){for(var t,e,r=0,u=i.length;u>r;++r)t=i[r],e=o[r]*n,t.vx+=(f[r]-t.x)*e,t.vy+=(a[r]-t.y)*e}function r(){if(i){var e,r=i.length;for(o=new Array(r),f=new Array(r),a=new Array(r),e=0;r>e;++e)o[e]=+c(i[e],e,i),f[e]=+n(i[e],e,i),a[e]=+t(i[e],e,i)}}var i,o,f,a,c=u(.1);return"function"!=typeof n&&(n=u(null==n?0:+n)),"function"!=typeof t&&(t=u(null==t?0:+t)),e.initialize=function(n){i=n,r()},e.strength=function(n){return arguments.length?(c="function"==typeof n?n:u(+n),r(),e):c},e.x=function(t){return arguments.length?(n="function"==typeof t?t:u(+t),r(),e):n},e.y=function(n){return arguments.length?(t="function"==typeof n?n:u(+n),r(),e):t},e}var v="0.2.1",x=10,p=Math.PI*(3-Math.sqrt(5)),M=2*Math.PI,q=2*Math.PI;n.version=v,n.forceCenter=o,n.forceCollide=l,n.forceContain=h,n.forceLink=d,n.forceManyBody=s,n.forcePosition=g,n.forceSimulation=c});
export var name = "d3-force";
export var version = "0.2.0";
export var version = "0.2.1";
export var description = "Force-directed graph layout using velocity Verlet integration.";

@@ -4,0 +4,0 @@ export var keywords = ["d3","layout","network","graphc","force","verlet","infovis"];

{
"name": "d3-force",
"version": "0.2.0",
"version": "0.2.1",
"description": "Force-directed graph layout using velocity Verlet integration.",

@@ -5,0 +5,0 @@ "keywords": [

@@ -27,1 +27,198 @@ # d3-force

### Simulation
<a name="forceSimulation" href="#forceSimulation">#</a> d3.<b>forceSimulation</b>([<i>nodes</i>])
<a name="simulation_start" href="#simulation_start">#</a> <i>simulation</i>.<b>start</b>()
<a name="simulation_stop" href="#simulation_stop">#</a> <i>simulation</i>.<b>stop</b>()
<a name="simulation_tick" href="#simulation_tick">#</a> <i>simulation</i>.<b>tick</b>()
<a name="simulation_nodes" href="#simulation_nodes">#</a> <i>simulation</i>.<b>nodes</b>([<i>nodes</i>])
* `index` - the node’s zero-based index into *nodes*
* `x` - the node’s current *x*-position
* `y` - the node’s current *y*-position
* `vx` - the node’s current *x*-velocity
* `vy` - the node’s current *y*-velocity
<a name="simulation_alphaMin" href="#simulation_alphaMin">#</a> <i>simulation</i>.<b>alphaMin</b>([<i>alpha</i>])
<a name="simulation_alphaDecay" href="#simulation_alphaDecay">#</a> <i>simulation</i>.<b>alphaDecay</b>([<i>decay</i>])
<a name="simulation_drag" href="#simulation_drag">#</a> <i>simulation</i>.<b>drag</b>([<i>drag</i>])
<a name="simulation_force" href="#simulation_force">#</a> <i>simulation</i>.<b>force</b>(<i>name</i>[, <i>force</i>])
<a name="simulation_on" href="#simulation_on">#</a> <i>simulation</i>.<b>on</b>(<i>typenames</i>, [<i>callback</i>])
### Forces
[Simulations](#simulation) compose multiple arbitrary forces. By default, simulations have no bound forces; add or remove a force using [*simulation*.force](#simulation_force). This module provides several built-in forces:
* [Centering](#centering)
* [Circle Collision](#circle-collision)
* [Circle Containment](#circle-containment)
* [Links](#links)
* [Many-Body](#many-body)
* [Positioning](#positioning)
You may also implement your own custom force. A force is simply a [function](#_force) that takes the simulation’s current *alpha* value and then modifies nodes’ positions or velocities. Forces may optionally implement [*force*.initialize](#force_initialize) to receive the simulation’s array of nodes.
<a name="_force" href="#_force">#</a> <i>force</i>(<i>alpha</i>)
<a name="force_initialize" href="#force_initialize">#</a> <i>force</i>.<b>initialize</b>(<i>nodes</i>)
#### Centering
The centering force moves nodes so that their center of mass (assuming all nodes are equal-weight) is at the given position ⟨[*x*](#center_x),[*y*](#center_y)⟩. (These parameters are only recomputed when the force is initialized, not on every application.)
<a name="forceCenter" href="#forceCenter">#</a> d3.<b>forceCenter</b>([<i>x</i>, <i>y</i>])
<a name="center_x" href="#center_x">#</a> <i>center</i>.<b>x</b>([<i>x</i>])
<a name="center_y" href="#center_y">#</a> <i>center</i>.<b>y</b>([<i>y</i>])
#### Circle Collision
The circle collision force prevents circular nodes with a given [radius](#collide_radius) from overlapping. More formally, two nodes *a* and *b* are separated so that the distance between *a* and *b* is at least *radius*(*a*) + *radius*(*b*). To reduce jitter, this is by default a “soft” constraint with a configurable [strength](#collide_strength). (These parameters are only recomputed when the force is initialized, not on every application.)
<a name="forceCollide" href="#forceCollide">#</a> d3.<b>forceCollide</b>([<i>radius</i>])
<a name="collide_radius" href="#collide_radius">#</a> <i>collide</i>.<b>radius</b>([<i>radius</i>])
<a name="collide_strength" href="#collide_strength">#</a> <i>collide</i>.<b>strength</b>([<i>strength</i>])
#### Circle Containment
The circle containment force constrains nodes to fit within a circle of a given [*radius*](#contain_radius) and center ⟨[*x*](#contain_x),[*y*](#contain_y)⟩. The radius and center can be specified on a per-node basis. (These parameters are only recomputed when the force is initialized, not on every application.)
<a name="forceContain" href="#forceContain">#</a> d3.<b>forceContain</b>([<i>radius</i>[, <i>x</i>, <i>y</i>]])
<a name="contain_radius" href="#contain_radius">#</a> <i>contain</i>.<b>radius</b>([<i>radius</i>])
<a name="contain_x" href="#contain_x">#</a> <i>contain</i>.<b>x</b>([<i>x</i>])
<a name="contain_y" href="#contain_y">#</a> <i>contain</i>.<b>y</b>([<i>y</i>])
#### Links
The link force pushes linked nodes closer together or farther apart according to the desired [link distance](#link_distance), which may be specified on a per-node basis. (These parameters are only recomputed when the force is initialized, not on every application.)
<a name="forceLink" href="#forceLink">#</a> d3.<b>forceLink</b>([<i>links</i>])
<a name="link_links" href="#link_links">#</a> <i>link</i>.<b>links</b>([<i>links</i>])
* `index` - the zero-based index into *links*
* `source` - the link’s source node; see [*simulation*.nodes](#simulation_nodes)
* `target` - the link’s target node; see [*simulation*.nodes](#simulation_nodes)
The source and target properties may be initialized using [*link*.id](#link_id).
<a name="link_id" href="#link_id">#</a> <i>link</i>.<b>id</b>([<i>id</i>])
<a name="link_strength" href="#link_strength">#</a> <i>link</i>.<b>strength</b>([<i>strength</i>])
<a name="link_distance" href="#link_distance">#</a> <i>link</i>.<b>distance</b>([<i>distance</i>])
#### Many-Body
The many-body (or *n*-body) force applies mutally amongst all [nodes](#simulation_nodes). It can be used to simulate gravity (attraction) if the [strength](#manyBody_strength) is positive, or eletrical charge (repulsion) if the strength is negative. This implementation uses quadtrees and the [Barnes–Hut approximation](https://en.wikipedia.org/wiki/Barnes–Hut_simulation) to greatly improve performance; the accuracy can be customized using the [theta](#manyBody_theta) parameter. The strength can be specified on a per-node basis. (This parameter is only recomputed when the force is initialized, not on every application.)
<a name="forceManyBody" href="#forceManyBody">#</a> d3.<b>forceManyBody</b>()
<a name="manyBody_strength" href="#manyBody_strength">#</a> <i>manyBody</i>.<b>strength</b>([<i>strength</i>])
<a name="manyBody_theta" href="#manyBody_theta">#</a> <i>manyBody</i>.<b>theta</b>([<i>theta</i>])
<a name="manyBody_distanceMin" href="#manyBody_distanceMin">#</a> <i>manyBody</i>.<b>distanceMin</b>([<i>distance</i>])
<a name="manyBody_distanceMax" href="#manyBody_distanceMax">#</a> <i>manyBody</i>.<b>distanceMax</b>([<i>distance</i>])
#### Positioning
The positioning force pushes nodes towards a desired position ⟨[*x*](#position_x),[*y*](#position_y)⟩. The position and [strength](#position_strength) can be specified on a per-node basis. (These parameters are only recomputed when the force is initialized, not on every application.)
<a name="forcePosition" href="#forcePosition">#</a> d3.<b>forcePosition</b>([<i>x</i>, <i>y</i>])
<a name="position_strength" href="#position_strength">#</a> <i>position</i>.<b>strength</b>([<i>strength</i>])
<a name="position_x" href="#position_x">#</a> <i>position</i>.<b>x</b>([<i>x</i>])
<a name="position_y" href="#position_y">#</a> <i>position</i>.<b>y</b>([<i>y</i>])

@@ -63,12 +63,7 @@ import constant from "./constant";

// Apply the Barnes-Hut approximation if possible.
// Limit forces for very close nodes.
// Randomize direction for exactly-coincident nodes.
if (l < distanceMin2) {
if (!l) l = Math.random() * tau, x = Math.cos(l), y = Math.sin(l), l = 1;
l = Math.sqrt(l / distanceMin2), x /= l, y /= l, l = distanceMin2;
}
// Apply the Barnes-Hut approximation if possible.
if (w * w / theta2 < l) {
if (l < distanceMax2) {
if (l < distanceMin2) l = Math.sqrt(l / distanceMin2), x /= l, y /= l, l = distanceMin2;
l = quad.value * alpha / l;

@@ -84,2 +79,9 @@ node.vx += x * l;

// Limit forces for very close nodes.
// Randomize direction for exactly-coincident nodes.
if (l < distanceMin2) {
if (!l) l = Math.random() * tau, x = Math.cos(l), y = Math.sin(l), l = 1;
l = Math.sqrt(l / distanceMin2), x /= l, y /= l, l = distanceMin2;
}
do if (quad.data !== node) {

@@ -86,0 +88,0 @@ w = strengths[quad.data.index] * alpha / l;

@@ -13,2 +13,5 @@ import {dispatch} from "d3-dispatch";

var initialRadius = 10,
initialAngle = Math.PI * (3 - Math.sqrt(5));
export default function(nodes) {

@@ -62,6 +65,10 @@ var simulation,

node = nodes[i], node.index = i;
if (isNaN(node.x)) node.x = Math.random() * 100 - 50;
if (isNaN(node.y)) node.y = Math.random() * 100 - 50;
if (isNaN(node.vx)) node.vx = 0;
if (isNaN(node.vy)) node.vy = 0;
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;
}
}

@@ -68,0 +75,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc