【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;

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

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
React实现列表片的懒加载,我们可以结合使用 `react-lazyload` 和 `Intersection Observer API`。以下是基于这两个工具的实现步骤: 1. 首先,安装 `react-lazyload` 。 ```bash npm install react-lazyload ``` 2. 在列表组件中引入 `react-lazyload` 组件,并使用 `Intersection Observer API` 监听片是否进入可视区域。 ```jsx import React from 'react'; import LazyLoad from 'react-lazyload'; const ListItem = ({ imageUrl, altText }) => { return ( <LazyLoad> <img src={imageUrl} alt={altText} /> </LazyLoad> ); }; const ListComponent = ({ items }) => { return ( <ul> {items.map((item) => ( <li key={item.id}> <ListItem imageUrl={item.imageUrl} altText={item.altText} /> </li> ))} </ul> ); }; export default ListComponent; ``` 在上述代码中,我们使用 `react-lazyload` 组件包裹了 `<img>` 元素,并将片的地址和替代文本作为属性传递给子组件 `ListItem`。当片进入可视区域时,`react-lazyload` 会自动加载片。 3. 在父组件中使用 `ListComponent`,并传递片数据给列表组件。 ```jsx import React from 'react'; import ListComponent from './ListComponent'; const App = () => { const items = [ { id: 1, imageUrl: 'image1.jpg', altText: 'Image 1' }, { id: 2, imageUrl: 'image2.jpg', altText: 'Image 2' }, { id: 3, imageUrl: 'image3.jpg', altText: 'Image 3' }, // 更多片数据 ]; return ( <div> <h1>List of Images</h1> <ListComponent items={items} /> </div> ); }; export default App; ``` 在父组件中,我们创建了一个包含片数据的数组,并将其传递给列表组件 `ListComponent`。列表组件会根据传递的数据生成相应的片项。 通过以上步骤,我们可以在 React实现列表片的懒加载。 `react-lazyload` 会自动处理片的加载,只有当片进入可视区域时才进行加载,从而提升页面性能和用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

陀螺蚁

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

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

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

打赏作者

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

抵扣说明:

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

余额充值