react-force-graph
React bindings for the
force-graph suite of components:
3d-force-graph (ThreeJS/WebGL),
3d-force-graph-vr (A-Frame) and
force-graph (2D HTML Canvas).

This module exports 3 React components with identical interfaces:
ForceGraph2D,
ForceGraph3D and
ForceGraphVR. Each can be used to represent a graph data structure in a 2 or 3-dimensional space using a force-directed iterative layout.
Uses canvas/WebGL for rendering and
d3-force-3d for the underlying physics engine.
Supports zooming/panning, node dragging and node/link hover/click interactions.
Check out the examples:
*
Basic (
source)
*
Directional arrows (
source)
*
Directional moving particles (
source)
*
Auto-colored nodes/links (
source)
*
2D Text nodes (
source)
*
3D Text nodes (
source)
*
Custom 2D node shapes (
source)
*
Custom 3D/VR node geometries (
source)
*
Curved lines and self links (
source)
*
Highlight nodes/links (
source)
*
Larger graph (
source)
*
Dynamic data changes (
source)
*
Click to focus on node (
source)
*
Camera automatic orbitting (
source)
*
Node collision detection (
source)
Quick start
``
import { ForceGraph2D, ForceGraph3D, ForceGraphVR } from 'react-force-graph';
`
or
`
var { ForceGraph2D, ForceGraph3D, ForceGraphVR } = require('react-force-graph');
`
or even
`
`
then
`
ReactDOM.render(
graphData={myData}
/>,
myDOMElement
);
`
API reference
Note that not all props listed below apply to all 3 components. The last 3 columns in these tables indicate the specific component availability of each prop/method.
$3
| Prop | Type | Default | Description | 2D | 3D | VR |
| --- | :--: | :--: | --- | :--: | :--: | :--: |
| graphData | object | { nodes: [], links: [] }
| Graph data structure (see below for syntax details). Can also be used to apply incremental updates. | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| nodeId | string | id
| Node object accessor attribute for unique node id (used in link objects source/target). | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| linkSource | string | source
| Link object accessor attribute referring to id of source node. | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| linkTarget | string | target
| Link object accessor attribute referring to id of target node. | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
$3
| Prop | Type | Default | Description | 2D | 3D | VR |
| --- | :--: | :--: | --- | :--: | :--: | :--: |
| width | number | <window width> | Canvas width, in px. | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| height | number | <window height> | Canvas height, in px. | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| backgroundColor | string | (2D - light / 3D - dark)| Chart background color. | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| showNavInfo | bool | true
| Whether to show the navigation controls footer info. | | :heavy_check_mark: | :heavy_check_mark: |
$3
| Prop | Type | Default | Description | 2D | 3D | VR |
| --- | :--: | :--: | --- | :--: | :--: | :--: |
| nodeRelSize | number | 4 | Ratio of node circle area (square px) [2D] or sphere volume (cubic px) [3D] per value unit. | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| nodeVal | number, string or func | val
| Node object accessor function, attribute or a numeric constant for the node numeric value (affects node size). | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| nodeLabel | string or func | name
| Node object accessor function or attribute for name (shown in label). Supports plain text or HTML content (except in VR). | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| nodeDesc | string or func | desc
| For VR only. Node object accessor function or attribute for description (shown under label). | | | :heavy_check_mark: |
| nodeColor | string or func | color
| Node object accessor function or attribute for node color. | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| nodeAutoColorBy | string or func | | Node object accessor function or attribute to automatically group colors by. Only affects nodes without a color attribute. | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| nodeOpacity | number | 0.75 | Nodes sphere opacity, between [0,1]. | | :heavy_check_mark: | :heavy_check_mark: |
| nodeResolution | number | 8 | Geometric resolution of each node's sphere, expressed in how many slice segments to divide the circumference. Higher values yield smoother spheres. Only applicable to 3D modes. | | :heavy_check_mark: | :heavy_check_mark: |
| nodeCanvasObject | func | default 2D node object is a circle, sized according to val
and styled according to color
. | Callback function for painting a custom 2D canvas object to represent graph nodes. Should use the provided canvas context attribute to perform drawing operations for each node. The callback function will be called for each node at every frame, and has the signature: nodeCanvasObject(
,