force-graph.tsx


Overview

force-graph.tsx is a React functional component that renders an interactive force-directed graph visualization using the @antv/g6 graph visualization library. It provides a dynamic and visually rich representation of nodes, edges, and combos (grouped nodes) with behaviors such as dragging, zooming, collapsing/expanding combos, and tooltips showing detailed information about graph elements.

This component is designed for integration in React applications where complex relational data needs to be visualized and explored interactively. It supports customizable node and edge styles, automatic layout, and dynamic updates based on input data.


Component: ForceGraph

Description

The ForceGraph component accepts graph data as props and renders an interactive graph visualization inside a container <div>. It handles graph initialization, rendering, and updates when the data changes. The visualization supports combos (grouped nodes), tooltips, and multiple user interactions.

Props

Prop

Type

Description

data

any

The graph data object containing nodes and edges to visualize. Expected to have at least nodes and edges arrays. Nodes can include combo information.

show

boolean

Controls the visibility of the graph container. When false, the graph is hidden by setting CSS display: none.

Internal State and Refs

Key Hooks Used

Main Functionalities

Data Preparation (nextData)

Graph Initialization and Rendering (render function)

Tooltip Content Logic

Rendered Output


Usage Example

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

const sampleData = {
  nodes: [
    { id: 'node1', entity_type: 'Person', comboId: 'group1', description: 'A person node' },
    { id: 'node2', entity_type: 'Company', comboId: 'group1' },
  ],
  edges: [
    { source: 'node1', target: 'node2', weight: 3 },
  ],
};

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

  return (
    <div style={{ width: '800px', height: '600px' }}>
      <ForceGraph data={sampleData} show={showGraph} />
      <button onClick={() => setShowGraph((prev) => !prev)}>
        Toggle Graph
      </button>
    </div>
  );
};

export default App;

Important Implementation Details


Interaction with Other Files


Visual Diagram

Below is a Mermaid component diagram illustrating the structure and interactions within the ForceGraph component:

componentDiagram
    ForceGraph <|-- ReactComponent
    ForceGraph : +props data:any, show:boolean
    ForceGraph : +containerRef:Ref<HTMLDivElement>
    ForceGraph : +graphRef:Ref<Graph|null>
    ForceGraph : +nextData = useMemo()
    ForceGraph : +render = useCallback()
    ForceGraph : +useEffect to trigger render()
    ForceGraph --> Graph : creates Graph instance (@antv/g6)
    ForceGraph --> buildNodesAndCombos : prepares data (nodes & combos)
    ForceGraph --> TooltipPlugin : custom tooltips for elements
    ForceGraph --> styles : CSS styles for container

Summary

force-graph.tsx is a React component that encapsulates the creation and rendering of a force-directed graph visualization using G6. It:

This makes it a reusable and powerful visualization component for applications requiring interactive exploration of relational data.


If you need further details about the buildNodesAndCombos utility or integration with the overall system, please provide related files or context.