force-graph.tsx

Overview

force-graph.tsx is a React functional component designed to render an interactive, force-directed graph visualization using the @antv/g6 graph visualization library. It takes graph data as input and displays nodes, edges, and combos (groupings of nodes) with customizable styles, behaviors, and tooltips. The graph supports user interactions such as dragging nodes and canvas, zooming, and collapsing/expanding combos, making it suitable for exploring complex relational data visually.

The component provides a dynamic, responsive container that adjusts to its parent size and toggles visibility based on props. It also leverages memoization and efficient rendering hooks to optimize performance with React.


Detailed Documentation

Component: ForceGraph

Description

ForceGraph renders a force-directed graph visualization. It processes raw input data, constructs the graph elements (nodes, edges, combos), and initializes the G6 Graph instance with configured layouts, styles, and interaction behaviors.

Props

Prop

Type

Description

data

any

The graph data object containing nodes and edges. Expected to have at least nodes and edges arrays.

show

boolean

Controls whether the graph visualization is visible (true) or hidden (false).

Internal State & Refs

Hooks Used


Methods and Functions

render()


Important Implementation Details


Usage Example

import React, { useState } from 'react';
import ForceGraph from './force-graph';

const sampleData = {
  nodes: [
    { id: 'node1', entity_type: 'typeA', label: 'Node 1' },
    { id: 'node2', entity_type: 'typeB', label: 'Node 2' },
  ],
  edges: [
    { source: 'node1', target: 'node2', weight: 3 },
  ],
};

const App = () => {
  const [showGraph, setShowGraph] = useState(true);

  return (
    <>
      <button onClick={() => setShowGraph(!showGraph)}>Toggle Graph</button>
      <ForceGraph data={sampleData} show={showGraph} />
    </>
  );
};

export default App;

Interaction with Other Parts of the System

This file acts as the visualization layer in a broader application, likely integrated with data management and UI components that provide the raw graph data.


Visual Diagram

classDiagram
    class ForceGraph {
        +props: IProps
        -containerRef: React.RefObject<HTMLDivElement>
        -graphRef: React.RefObject<Graph | null>
        -nextData: {nodes: any[], edges: any[], combos?: any[]}
        +render(): void
        +useEffect(): void
        +useMemo(): {nodes: any[], edges: any[], combos?: any[]}
    }

    ForceGraph ..> Graph : uses
    ForceGraph ..> buildNodesAndCombos : uses
    ForceGraph ..> isEmpty : uses

Summary

The force-graph.tsx component provides a powerful, flexible force-directed graph visualization in React using @antv/g6. It handles data preprocessing, graph initialization, styling, and interactive behaviors within a responsive container. The modular design and hooks usage ensure efficient updates and smooth user experience for exploring complex graph data.