A D3 force plugin that straightens chains of connected nodes into rigid lines.
npm install d3-force-straightend3.forceStraighten
==================
[![NPM package][npm-img]][npm-url]
[![Build Size][build-size-img]][build-size-url]
[![NPM Downloads][npm-downloads-img]][npm-downloads-url]
This force transforms flexible sequences of nodes into straighter configurations by identifying arms—linear chains of nodes where each internal node connects to exactly two others—and nudging them toward alignment.
Under the hood, it uses linear regression to find the best-fit line through each arm, then applies a spring-like force that pulls each node toward that line. The farther a node is from alignment, the stronger the corrective force, encouraging long arms to behave like stiff rods instead of floppy chains.
This force plugin is designed to be used with the d3-force simulation engine. It is also compatible with d3-force-3d and can function in a one, two (default) or three-dimensional space.
``js`
import d3ForceStraighten from 'd3-force-straighten';`
or using a script taghtml``
thenjs`
d3.forceSimulation()
.nodes(
.force('straighten-arms', d3.forceStraighten(
| Method | Description | Default |
|-----------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:------------:|
| links([array]) | Sets or gets the list of links connecting nodes. Each link should follow the syntax: {source: . | [] |node.index
| id([fn]) | Sets or gets the node object unique id accessor function, used by links to reference nodes. | |0
| strength([num]) | Sets or gets the force strength. Defines how strongly nodes are pulled toward their best-fit straight line. A value of disables the force; 1 applies full strength. | 1 |false` |
| considerAlpha([num]) | Sets or gets whether the force intensity should decrease or not as alpha decays and the simulation cools down. |
If you find this module useful and would like to support its development, you can buy me a ☕. Your contributions help keep open-source sustainable!

[npm-img]: https://img.shields.io/npm/v/d3-force-straighten
[npm-url]: https://npmjs.org/package/d3-force-straighten
[build-size-img]: https://img.shields.io/bundlephobia/minzip/d3-force-straighten
[build-size-url]: https://bundlephobia.com/result?p=d3-force-straighten
[npm-downloads-img]: https://img.shields.io/npm/dt/d3-force-straighten
[npm-downloads-url]: https://www.npmtrends.com/d3-force-straighten