Skip to content
mbostock edited this page Jul 3, 2011 · 41 revisions

API ReferenceLayouts

A flexible force-directed graph layout implementation using position Verlet integration to allow simple constraints. For more on physical simulations, see Thomas Jakobsen. This implementation uses a quadtree to accelerate charge interaction using the Barnes–Hut approximation. In addition to the repulsive charge force, a psuedo-gravity force keeps nodes centered in the visible area. Links are fixed-distance geometric constraints.

force

Additional custom forces and constraints may be applied on the "tick" event, simply by updating the x and y attributes of nodes. For example, this can be used to implement multiple foci. The layout algorithm is also extremely robust in dealing with disconnected graphs.

# d3.layout.force()

Constructs a new, default force-directed layout.

# force.size([size])

Get or set the layout size in x and y.

# force.distance([distance])

Get or set the link distance.

# force.friction([friction])

Get or set the friction coefficient in [0,1].

# force.charge([charge])

Get or set the charge strength. A negative number is recommended for repulsion.

# force.gravity([gravity])

Get or set the gravity strength.

# force.theta([theta])

Get or set the accuracy of the charge interaction (Barnes–Hut approximation).

# force.nodes([nodes])

Get or set the array of nodes to layout.

# force.links([links])

Get or set the array of links between nodes.

# force.start()

Start the simulation. Can also be used to restart the simulation when the nodes or links change. As the layout stabilizes, it cools, slowing down movement and eventually stopping. This way, it doesn't hog the CPU or drain the battery.

# force.resume()

Reheat the cooling parameter (alpha) and restart simulation.

# force.stop()

Immediately terminate the simulation.

# force.on(type, listener)

Listen to "tick" updates in the computed layout positions. Use this to update the displayed nodes and links.

# force.drag()

Bind a behavior to nodes to allow interactive dragging. Use in conjunction with the call operator on the nodes.

Clone this wiki locally