【React】AntV G6 - 快速入手

环境

  • react: ^18
  • next: 14.1.0
  • @antv/g6: ^4.8.24

安装

npm install @antv/g6

# or
pnpm add @antv/g6

# or 
yarn add @antv/g6

使用

模拟数据

const data = {
  nodes: [ // 节点信息
    {
      id: "node1",
      data: {
        name: "Circle1"
      }
    },
    {
      id: "node2",
      data: {
        name: "Circle2"
      }
    }
  ],
  edges: [ // 边线
    {
      id: "edge1",
      source: "node1",
      target: "node2"
    }
  ]
};

示例

创建ForceGraph组件

// file: component/ForceGraph/index.tsx
"use client"

import type { GraphOptions, GraphData } from '@antv/g6';

import React, {useEffect, useRef} from "react"
import { Graph } from '@antv/g6';

interface Props{  
	nodes: any[],  
	edges: any[],  
	options?: GraphOptions  
}

// 基础配置
const defaultOptions = {
    width: 500,
    height: 500,
    defaultNode: {
        type: "circle",
        size: [100],
        color: "#5B8FF9",
        style: {
            fill: "#9EC9FF",
            lineWidth: 3
        },
        labelCfg: {
            style: {
                fill: "#fff",
                fontSize: 20
            }
        }
    },
    defaultEdge: {
        style: {
            stroke: "#e2e2e2"
        }
    }
};

const ForceGraph = function (
    props:Props
) {
    const containerRef= useRef<HTMLDivElement | null>(null);
    const graphRef = React.useRef<Graph>();

    const data:GraphData = {nodes:[], edges:[]};

    useEffect(()=>{
        if(graphRef.current || !containerRef.current)return;

        if (props.nodes)data.nodes=props.nodes
        if (props.edges)data.edges=props.edges

        const _options = Object.assign({}, defaultOptions, props.options);
        _options.container = containerRef.current;

        const graph = new Graph(_options);
		
		// 绑定数据
        graph.data(data);
        // 渲染图
        graph.render();
        graphRef.current = graph;
    }, 
    [
        props.nodes,
        props.edges,
    ])

    return <div ref={containerRef}></div>;
}

export default ForceGraph

组件调用

// file: app/pages.tsx
import ForceGraph from "@/component/ForceGraph";

export default function Home(){
    const data = {
        nodes: [
            { id: 'node1', data: { name: 'Circle1' } },
            { id: 'node2', data: { name: 'Circle2' } },
        ],
        edges: [{ id: 'edge1', source: 'node1', target: 'node2', data: {} }],
    };


    return (
        <div>
			<ForceGraph
				nodes={data.nodes}
				edges={data.edges}
			/>
		</div>
    )
}

效果

![[Pasted image 20240311114517.png]]

相关推荐

  1. QT6.3学习技巧,快速入门

    2024-03-14 17:24:04       62 阅读
  2. QT6.3学习技巧,快速入门

    2024-03-14 17:24:04       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-14 17:24:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-14 17:24:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-14 17:24:04       82 阅读
  4. Python语言-面向对象

    2024-03-14 17:24:04       91 阅读

热门阅读

  1. [ffmpeg] 获取编译配置信息

    2024-03-14 17:24:04       35 阅读
  2. ffmpeg 通过遍历视频流,对视频帧进行打标

    2024-03-14 17:24:04       30 阅读
  3. Lua速成(1)

    2024-03-14 17:24:04       41 阅读
  4. Spring 整合 MyBatis、Junit

    2024-03-14 17:24:04       44 阅读
  5. springboot使用EasyExcel实现Excel导入导出

    2024-03-14 17:24:04       38 阅读