Force-directed graph layout for redux.
math & physics credits goes to d3-force
redux-force
- exposes a set of specialized reducers which work together to apply forces to a simulation
- expose a set of actions to add
links
andnodes
and update the simulation - does not provide a graphical representation by default but plays well with
react
(for example@vx/network
)
customInitialState
: (Object
) - a custom initial state that will be merged with the default initial state
A force is simply a function that modifies nodes’ positions or velocities; in this context, a force can apply a classical physical force such as electrical charge or gravity, or it can resolve a geometric constraint, such as keeping nodes within a bounding box or keeping linked nodes a fixed distance apart. [...]
Creates a new circle collision force with the specified radius. If radius is not specified, it defaults to the constant one for all nodes.
radius
: (Function|Number
) - a state function invoked on each node to compute the radius(state) => (node, i, nodes) => i
strength
: (Number
) - the default strength
The link force pushes linked nodes together or apart according to the desired link distance. The strength of the force is proportional to the difference between the linked nodes’ distance and the target distance, similar to a spring force
distance
: (Function|Number
) - a state function invoked on each link to compute the distance between nodesstrength
: (Function|Number
) - a state function invoked on each node to compute the force of each links
Creates a new many-body force with the default parameters.
strength
: (Function|Number
) - a state function invoked on each node to compute the force of each nodes
Creates a new positioning force along the x-axis towards the given position x. If x is not specified, it defaults to 0.
x
: (Function|Number
) - a state function invoked on each node to compute itsx
positionstrength
: (Function|Number
) - a state function invoked on each node to compute its force
Creates a new positioning force along the y-axis towards the given position y. If y is not specified, it defaults to 0.
y
: (Function|Number
) - a state function invoked on each node to compute itsy
positionstrength
: (Function|Number
) - a state function invoked on each node to compute its force
Creates a new centering force with the specified x- and y- coordinates. If x and y are not specified, they default to ⟨0,0⟩.
x
: (Number
)y
: (Number
)
- Setup the reducers:
import reduceMergeReducers from "reduce-merge-reducers";
import simulation, { forceLink, ..., velocityDecay } from "redux-force";
export default reduceMergeReducers(
simulation(), // creates the simulation state first
forceLink({ distance: 50 }), // then add some or many forces reducers
...
velocityDecay({ velocityDecay: 0.633 }) // finally add the velocity decay
);
see example/src/reducer.js for a full example
- Choose a graph representation library (
@vx/network
, etc...) and map yourredux-force
data to it:
import React, { Component } from "react";
import { connect } from "react-redux";
import { Graph, DefaultLink, DefaultNode } from "@vx/network";
import { getNodes, getLinks } from "redux-force";
class MyGraph extends Component {
...
render() {
return (
<svg ...>
<Graph
graph={this.props.dataSample}
linkComponent={DefaultLink}
nodeComponent={DefaultNode}
/>
</svg>
);
}
}
export default connect(
state => ({
dataSample: {
nodes: getNodes(state),
links: getLinks(state),
},
}),
)(App);
see example/src/App.js for a full example
- And dispatch your actions:
import React, { Component } from "react";
import { connect } from "react-redux";
import { initialize, tick } from "redux-force";
class MyGraph extends Component {
componentDidMount() {
this.props.initialize();
}
componentWillUpdate() {
window.requestAnimationFrame(this.props.tick);
}
...
}
export default connect(
state => ({}),
{
initialize,
tick,
}
)(App);
see example/src/reducer.js for a full example
Simply clone the repo, npm install, and run npm test