d3-force/README.md
2016-04-26 11:50:55 -07:00

7.8 KiB
Raw Blame History

d3-force

Installing

If you use NPM, npm install d3-force. Otherwise, download the latest release. You can also load directly from d3js.org, either as a standalone library or as part of D3 4.0 alpha. AMD, CommonJS, and vanilla environments are supported. In vanilla, a d3_force global is exported:

<script src="https://d3js.org/d3-collection.v0.1.min.js"></script>
<script src="https://d3js.org/d3-dispatch.v0.4.min.js"></script>
<script src="https://d3js.org/d3-quadtree.v0.7.min.js"></script>
<script src="https://d3js.org/d3-timer.v0.4.min.js"></script>
<script src="https://d3js.org/d3-force.v0.2.min.js"></script>
<script>

var simulation = d3_force.forceSimulation(nodes);

</script>

Try d3-force in your browser.

API Reference

Simulation

# d3.forceSimulation([nodes])

# simulation.start()

# simulation.stop()

# simulation.tick()

# simulation.nodes([nodes])

  • index - the nodes zero-based index into nodes
  • x - the nodes current x-position
  • y - the nodes current y-position
  • vx - the nodes current x-velocity
  • vy - the nodes current y-velocity

# simulation.alphaMin([alpha])

# simulation.alphaDecay([decay])

# simulation.drag([drag])

# simulation.force(name[, force])

# simulation.on(typenames, [callback])

Forces

Simulations compose multiple arbitrary forces. By default, simulations have no bound forces; add or remove a force using simulation.force. This module provides several built-in forces:

You may also implement your own custom force. A force is simply a function that takes the simulations current alpha value and then modifies nodes positions or velocities. Forces may optionally implement force.initialize to receive the simulations array of nodes.

# force(alpha)

# force.initialize(nodes)

Centering

The centering force moves nodes so that their center of mass (assuming all nodes are equal-weight) is at the given position ⟨x,y⟩.

# d3.forceCenter([x, y])

# center.x([x])

# center.y([y])

Circle Collision

The circle collision force prevents circular nodes with a given 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. (These parameters are only recomputed when the force is initialized, not on every application.)

# d3.forceCollide([radius])

# collide.radius([radius])

# collide.strength([strength])

Circle Containment

The circle containment force constrains nodes to fit within a circle of a given radius and center ⟨x,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.)

# d3.forceContain([radius[, x, y]])

# contain.radius([radius])

# contain.x([x])

# contain.y([y])

The link force pushes linked nodes closer together or farther apart according to the desired 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.)

# d3.forceLink([links])

# link.links([links])

The source and target properties may be initialized using link.id.

# link.id([id])

# link.strength([strength])

# link.distance([distance])

Many-Body

The many-body (or n-body) force applies mutally amongst all nodes. It can be used to simulate gravity (attraction) if the strength is positive, or eletrical charge (repulsion) if the strength is negative. This implementation uses quadtrees and the BarnesHut approximation to greatly improve performance; the accuracy can be customized using the theta parameter.

# d3.forceManyBody()

# manyBody.strength([strength])

# manyBody.theta([theta])

# manyBody.distanceMin([distance])

# manyBody.distanceMax([distance])

Positioning

The positioning force pushes nodes towards a desired position ⟨x,y⟩. The position and strength can be specified on a per-node basis. (These parameters are only recomputed when the force is initialized, not on every application.)

# d3.forcePosition([x, y])

# position.strength([strength])

# position.x([x])

# position.y([y])