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

news/2024/7/15 17:34:19 标签: javascript, react.js, 前端

主角: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
javascript">// 来源: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
javascript">"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;

页面调用

javascript">"use client";

import ForceGraphW3D from "@/component/ForceGraphW3D";

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

export default Page;

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


http://www.niftyadmin.cn/n/5464567.html

相关文章

图解PyTorch中的torch.gather函数和 scatter 函数

前言 torch.gather在目前基于 transformer or query based 的目标检测中&#xff0c;在最后获取目标结果时&#xff0c;经常用到。 这里记录下用法&#xff0c;防止之后又忘了。 介绍 torch.gather 官方文档对torch.gather()的定义非常简洁 定义&#xff1a;从原tensor中获…

MarkDown之时序图并行、条件、循环、可选高级语法(三十)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 优质专栏&#xff1a;多媒…

SpringBoot+uniApp宠物领养小程序系统 附带详细运行指导视频

文章目录 一、项目演示二、项目介绍三、运行截图四、主要代码1.保存宠物信息代码2.提交订单信息代码3.查询评论信息代码 一、项目演示 项目演示地址&#xff1a; 视频地址 二、项目介绍 项目描述&#xff1a;这是一个基于SpringBootuniApp框架开发的宠物领养微信小程序系统。…

在课堂中使用 ChatGPT 的 80 个方式(上)

原文&#xff1a;80 Ways to Use ChatGPT in the Classroom 译者&#xff1a;飞龙 协议&#xff1a;CC BY-NC-SA 4.0 第一章&#xff1a;介绍 chatGPT 于 2022 年 11 月崭露头角。它已经开始颠覆高等教育等行业和企业&#xff0c;类似于印刷机和互联网。在核心上&#xff0c;c…

[实战经验]Mybatis的mapper.xml参数#{para}与#{para, jdbcType=BIGINT}有什么区别?

在MyBatis框架中&#xff0c;传入参数使用#{para}和#{para, jdbcTypeBIGINT}的有什么区别呢&#xff1f; #{para}&#xff1a;这种写法表示使用MyBatis自动推断参数类型&#xff0c;并根据参数的Java类型自动匹配数据库对应的类型。例如&#xff0c;如果参数para的Java类型是Lo…

【BUG】No module named ‘dnf‘

报错内容&#xff1a; 类型一 # git clone https://github.com/pytorch/vision.git Cloning into vision... /usr/libexec/git-core/git-remote-https: symbol lookup error: /usr/lib64/libldap.so.2: undefined symbol: EVP_md2, version OPENSSL_1_1_0类型二 # yum reins…

题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

题目&#xff1a;有一个已经排好序的数组。现输入一个数&#xff0c;要求按原来的规律将它插入数组中。 There is no nutrition in the blog content. After reading it, you will not only suffer from malnutrition, but also impotence. The blog content is all parallel g…

Python:百度AI开放平台——OCR图像文字识别应用

一、注册百度AI开放平台 使用百度AI服务的步骤为&#xff1a; 注册&#xff1a;注册成为百度AI开放平台开发者&#xff1b;创建AI应用&#xff1a;在百度API开放平台上创建相关类型的的AI应用&#xff0c;获得AppID、API Key和Secret Key&#xff1b;调用API&#xff1a;调用…