Skip to main content
100%

Bounded Force Layout

✓ Published0🌍 Public
Mmbostock
Last edited Feb 9, 2016
Created on Aug 6, 2011

This example demonstrates a force-directed graph constrained within a fixed rectangular boundary. It visualizes character relationships from *Les Misérables*, with nodes colored by group and connected by gray links. The simulation uses the `d3.layout.force` API, and during each tick event, node positions are clamped so their centers remain within the SVG canvas bounds, accounting for a fixed radius. The graph is rendered as SVG circles and lines, with data loaded from a JSON file.

AI-generated description

This D3 example shows how to constrain the position of nodes within the rectangular bounds of the containing SVG element. As a side-effect of updating the node's cx and cy attributes, we update the node positions to be within the range [radius, width - radius] for x, [radius, height - radius] for y. If you prefer, you could use the each operator to do this as a separate step, rather than as a side-effect of setting attributes.

gpl-3.0 Licensed

Similar vizzes