【React】基于JS 3D引擎库实现关系图(图&graph)

主角:3D Force-Directed Graph
简介:一个使用ThreeJS/WebGL进行3D渲染的Graph图库
GitHub: https://github.com/vasturiano/3d-force-graph
Ps: 较为复杂或节点巨大时,对GPU>CPU消耗较大,同量级节点对比下优于AntV G6和Echarts渲染

效果

在这里插入图片描述

环境

  • 3d-force-graph: ^1.73.3
  • next: 14.1.3
  • react: ^18

目录

仅包含涉及到的文件

| - app
   |- page.tsx
| - components
    |- ForceGraphW3D
       |- data.ts
       |- index.tsx

实操

演示数据

由于效果展示的演示过于庞大,以下仅展示基本数据结构

  • components/ForceGraphW3D/data.ts
// 来源:https://vasturiano.github.io/3d-force-graph/example/datasets/blocks.json
export default {
    "nodes": [ // 拥有的节点及扩展数据
		{
            "id": "4062045",
            "user": "mbostock",
            "description": "Force-Directed Graph"
        },
        // .....
	],
    "links": [ // 建立节点关系,根据nodes的id字段进行关联
    	{
            "source": "950642",
            "target": "4062045"
        },
        // .....
    ]
}

创建EchartsGraph组件

  • components/ForceGraphW3D/index.tsx
"use client";

import type {ConfigOptions, ForceGraph3DInstance} from "3d-force-graph";
import React, {useEffect, useRef} from "react";
import ForceGraph3D from '3d-force-graph';
import data from "./data"

type Props = {
    children?: React.ReactNode
}

const ForceGraph3DOptions: ConfigOptions = {}

let _forceGraph3D: ForceGraph3DInstance | undefined = undefined;
let _graph: ForceGraph3DInstance | undefined = undefined;

const ForceGraphW3D = function (props: Props) {
    const containerRef = useRef<HTMLDivElement>(null);
    const graphRef = useRef<HTMLDivElement>(null);

    function graphInit(elm: HTMLDivElement) {
        if (!containerRef) return;
        // 只能在客户端模式下载入
        if (typeof window !== 'undefined') {
            // 构建实例化
            if (!_forceGraph3D) {
                _forceGraph3D = ForceGraph3D(ForceGraph3DOptions);
            }
            // 绑定容器元素
            _graph = _forceGraph3D(elm);
            // 实例配置
            _graph
                .width(containerRef.current?.offsetWidth || 800)
                .height(containerRef.current?.offsetHeight || 800)
                .graphData(data);
        }
    }

    useEffect(() => {
        if (graphRef.current) {
            graphInit(graphRef.current);
        }
    }, [graphRef]);

    return (
        <div ref={containerRef}>
            <div ref={graphRef}></div>
            {props.children}
        </div>
    )
}

export default ForceGraphW3D;

页面调用

"use client";

import ForceGraphW3D from "@/component/ForceGraphW3D";

const Page = function () {
    return (
        <div style={{width: '100%', height: '100%',overflow: 'hidden'}}>
            <ForceGraphW3D />
        </div>
    );
}

export default Page;

如果大家对其他实战实例感兴趣,请在下面留言,我会尽快更新。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陀螺蚁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值