React Sigma
repository·main·Indexed 42 days ago
https://github.com/sim51/react-sigmaA suite of React components providing a declarative interface for rendering and interacting with graphs using the Sigma v3 engine. It includes a core library (@react-sigma/core) with components like SigmaContainer and ControlsContainer, as well as specialized modules for graph search, minimaps, and various layout algorithms including ForceAtlas2, circular, circle-packing, and force-directed layouts.
What's inside React Sigma
- React Sigma is a collection of React components designed to display and interact with graphs using Sigma v3. It provides a React-friendly wrapper around the Sigma graph visualization engine, allowing developers to integrate complex graph visualizations into React applications.
Prerequisites for React Sigma
mainBefore using React Sigma, ensure you have knowledge of the following:
- React: You should have an existing React project setup.
- Sigma: React Sigma is a React wrapper for Sigma.js. It is not a replacement for Sigma's documentation; you should refer to Sigma's official docs for graph rendering logic and core Sigma concepts.
- Graphology: Since React Sigma relies on
graphologyfor graph data structures, familiarity with it is recommended.
Use regular layouts with useLayout hooks
mainRegular layouts are intended for one-time execution (e.g.,
randomorcircular). The layout hooks return two functions:positions: Executes the layout on the sigma graph and returns a map of positions (where the key is the node key) without modifying the graph itself.assign: Executes the layout on the sigma graph and directly saves the node positions into the graph.
Example usage:
const { positions, assign } = useLayoutCircular(...);const { positions, assign } = useLayoutCircular(...);Understand the SigmaContainer lifecycle
mainThe
SigmaContainerfollows a specific lifecycle to bridge React and Sigma:- DOM Rendering: It renders a container structure containing a
<div className="sigma-container">element. - Instantiation: It instantiates a new Sigma instance targeting that
divand stores it in a React context. - Child Rendering: It renders all child components.
- Interaction: Child components consume the Sigma instance via context to manipulate the graph or the view.
Warning: Changing props on
SigmaContainertriggers a re-render of the container and all its children, causing the Sigma instance to be killed and re-created.- DOM Rendering: It renders a container structure containing a
Passing a graph instance to SigmaContainer
mainYou can pass a pre-instantiated
graphologyinstance directly to thegraphprop ofSigmaContainer.Warning: When passing an instance, Sigma will be killed, recreated, and the graph will be reloaded on every render of the component. To avoid significant performance issues, especially with large graphs, you must manage the React lifecycle of the parent component to ensure the instance is stable and not recreated on every render.
import Graph from "graphology"; import { SigmaContainer, useLoadGraph } from "@react-sigma/core"; import "@react-sigma/core/lib/style.css"; export const DisplayGraph = () => { const graph = new Graph(); graph.addNode("first", { x: 0, y: 0, size: 15, label: "My first node", color: "#FA4F40" }); return <SigmaContainer style={{ height: "500px", width: "500px" }} graph={graph}></SigmaContainer>; };How non-iterative layouts work
mainNon-iterative layouts (such as
randomorcircular) are computed once to determine node positions. The layout modules for these types export a hook that provides two functions:positions(): Returns the computed node positions without modifying the graph.assign(graph): Computes the positions and immediately assigns them to the provided graph.
These hooks must be used within a component that is a child of
SigmaContainer.TypeScript support in React Sigma
mainReact Sigma is written in TypeScript. Type definitions are provided natively by the library, so no additional@typespackages are required for the core library itself. However, ensuregraphology-typesis installed as a peer dependency to support graph-related types.Custom render for controls
mainYou can override the default styles and icons of controls by providing custom JSX children. The required number of children depends on the specific control:
- FullScreenControl: Requires two children: the first for the "enter fullscreen" action and the second for the "exit fullscreen" action.
- ZoomControl: Requires three children: zoom in, zoom out, and zoom reset.
- Worker layout controls: Requires two children: stop and start.
How React Sigma works and its core concepts
mainReact Sigma provides React bindings and helpers for Sigma.js. The central component is
SigmaContainer, which manages the lifecycle of a Sigma instance.Key Mental Models
- Immutable Component Pattern: Treat
SigmaContaineras an immutable component. If you update thesettingsorgraphprops, the entire Sigma instance is destroyed and re-created (though the camera state is preserved). To avoid performance degradation, do not pass frequently changing objects to these props. Instead, define them as constants outside your component or wrap them inuseMemo. Alternatively, use the Sigma or Graphology APIs directly to mutate the graph or settings without triggering a full re-render. - Context-Driven Architecture: React Sigma uses the React Context API to propagate the Sigma instance to all child components. This allows you to nest components within
SigmaContainerthat can interact with the graph. - Modular Design: The library is split into specialized modules.
@react-sigma/corehandles the React-Sigma lifecycle, while layout-specific logic (e.g.,@react-sigma/layout-forceatlas2) is provided in separate packages to allow for tree-shaking and smaller bundle sizes.
- Immutable Component Pattern: Treat
Define a custom graph constructor for MultiGraphs
mainBy default,
SigmaContainercreates a standard graphology graph when it mounts. If you need to support features like parallel edges (multi-graphs), you must explicitly pass the desired graph constructor to thegraphproperty ofSigmaContainer. This step is mandatory for using multi-graphs.import { MultiGraph } from "graphology"; // Pass the constructor to SigmaContainer to ensure the internal graph is a MultiGraph <SigmaContainer graph={MultiGraph} />Use worker layouts with useWorkerLayout hooks
mainWorker layouts are designed for continuous or iterative processes (like
force atlas 2) that run in a web worker. The worker hooks return control functions and status information:start: Starts the layout process.stop: Stops the layout process.kill: Kills the layout worker.isRunning: A boolean indicating whether the layout is currently running.
Example usage:
const { stop, start, kill, isRunning } = useWorkerLayoutForceAtlas2(...);const { stop, start, kill, isRunning } = useWorkerLayoutForceAtlas2(...);How iterative layouts work
mainIterative layouts (such as
forceatlas2) run continuously in a web worker to refine node positions. These modules provide three ways to interact with the layout:- Layout Hook: Similar to non-iterative layouts, it runs a fixed number of iterations (configurable via options like
{ iterations: 100 }). - Worker Management Hook: Provides fine-grained control over the background worker via
start(),stop(),kill(), and anisRunningstatus. - Control Component: A pre-built UI component that provides start/stop buttons for the user.
- Layout Hook: Similar to non-iterative layouts, it runs a fixed number of iterations (configurable via options like