How vasturiano/react-force-graph works
Based on our analysis of both the file tree and README, this project is a library that provides React bindings for a suite of force-directed graph visualization components. These components expose four separate packages for 2D, 3D, VR, and AR graph rendering, all of which share a similar API. The examples supplied in the “example” folder help demonstrate how each component is used, and the source code in the “src/packages” directory is organized as a monorepo containing each React package separately.
Main Components
• Library Packages – These are the core React components:
– react-force-graph-2d (2D Canvas – HTML based)
– react-force-graph-3d (3D with Three.js / WebGL)
– react-force-graph-vr (3D with VR support using A-Frame)
– react-force-graph-ar (AR using AR.js)
• Core Graph Data and Simulation Engine – The library consumes a generic graph data structure (nodes and links) and uses physics engines (primarily d3-force-3d and optionally ngraph) to simulate force-directed layouts.
• Rendering Backends – Depending on the package, the rendering technology varies:
– 2D: HTML5 Canvas
– 3D/VR/AR: Three.js-based rendering and additional VR/AR frameworks (A-Frame, AR.js)
• Build/Packaging Infrastructure – Include a component illustrating the monorepo structure (the “src/packages” folder) together with Rollup configuration and transpilation (files like rollup.config.js and .babelrc).
Relationships and Data Flow
• An input “Graph Data” block that provides dynamic data (nodes and links in JSON format) to the React components.
• Link the “Graph Data” to the “Simulation Engine” (i.e., d3-force-3d or ngraph). Then, display its connection to each “Rendering Engine” (Canvas for 2D; Three.js based for 3D, VR, AR).
• That the React bindings are essentially wrappers that map properties and events from React props onto the internal force simulation and rendering lifecycle.
• How events (node click, drag, hover) propagate from the rendered output back to user-defined callbacks.
Architectural Patterns and Design Principles
• Modularity: Emphasize the shared interfaces across the four separate packages.
• Separation of Concerns: Clearly separate the graph data handling (physics engine) and the rendering layer (Canvas/Three.js) while showing that the React components form a thin presentation layer.
• Monorepo Organization: Include a logical representation of how multiple packages share common configurations (e.g., rollup and type definitions), indicating both the “src” directory and sub-packages.
• Extensibility: Highlight the customization points – for example, custom rendering (nodeCanvasObject, nodeThreeObject), custom event handlers, and custom simulation parameters.