React Sigma

repository·main·Indexed 42 days ago

https://github.com/sim51/react-sigma

A 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.

Tokens
23.4K
Snippets
89
Records
119
Agent score
53%

What's inside React Sigma

  1. Overview of React Sigma

    main
    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.
  2. Prerequisites for React Sigma

    main

    Before using React Sigma, ensure you have knowledge of the following:

    1. React: You should have an existing React project setup.
    2. 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.
    3. Graphology: Since React Sigma relies on graphology for graph data structures, familiarity with it is recommended.
  3. Use regular layouts with useLayout hooks

    main

    Regular layouts are intended for one-time execution (e.g., random or circular). 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(...);
  4. Understand the SigmaContainer lifecycle

    main

    The SigmaContainer follows a specific lifecycle to bridge React and Sigma:

    1. DOM Rendering: It renders a container structure containing a <div className="sigma-container"> element.
    2. Instantiation: It instantiates a new Sigma instance targeting that div and stores it in a React context.
    3. Child Rendering: It renders all child components.
    4. Interaction: Child components consume the Sigma instance via context to manipulate the graph or the view.

    Warning: Changing props on SigmaContainer triggers a re-render of the container and all its children, causing the Sigma instance to be killed and re-created.

  5. Passing a graph instance to SigmaContainer

    main

    You can pass a pre-instantiated graphology instance directly to the graph prop of SigmaContainer.

    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>;
    };
  6. How non-iterative layouts work

    main

    Non-iterative layouts (such as random or circular) are computed once to determine node positions. The layout modules for these types export a hook that provides two functions:

    1. positions(): Returns the computed node positions without modifying the graph.
    2. 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.

  7. Custom render for controls

    main

    You 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.
  8. How React Sigma works and its core concepts

    main

    React 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 SigmaContainer as an immutable component. If you update the settings or graph props, 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 in useMemo. 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 SigmaContainer that can interact with the graph.
    • Modular Design: The library is split into specialized modules. @react-sigma/core handles 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.
  9. Define a custom graph constructor for MultiGraphs

    main

    By default, SigmaContainer creates 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 the graph property of SigmaContainer. 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} />
  10. Use worker layouts with useWorkerLayout hooks

    main

    Worker 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(...);
  11. How iterative layouts work

    main

    Iterative layouts (such as forceatlas2) run continuously in a web worker to refine node positions. These modules provide three ways to interact with the layout:

    1. Layout Hook: Similar to non-iterative layouts, it runs a fixed number of iterations (configurable via options like { iterations: 100 }).
    2. Worker Management Hook: Provides fine-grained control over the background worker via start(), stop(), kill(), and an isRunning status.
    3. Control Component: A pre-built UI component that provides start/stop buttons for the user.