logo

G6

  • 文档
  • API
  • 图表示例
  • 社区
  • 所有产品antv logo arrow
  • 5.0.49
  • 简介
  • 开始使用
    • 快速开始
    • 安装
    • 前端框架集成
      • 在 React 中使用
      • 在 Vue 中使用
      • 在 Angular 中使用
    • 详细教程
  • 图 Graph
    • Graph - 图
    • Options 配置项
    • 内置扩展
    • Extension - 扩展
  • 数据 Data
  • 元素 Element
    • 元素总览
    • 元素状态
    • 节点 Node
      • 节点总览
      • 节点通用配置项
      • 圆形节点 Circle
      • 菱形节点 Diamond
      • 甜甜圈节点 Donut
      • 椭圆形节点 Ellipse
      • 六边形节点 Hexagon
      • HTML节点 Html
      • 图片节点 Image
      • 矩形节点 Rect
      • 五角形节点 Star
      • 三角形节点 Triangle
      • 自定义节点
      • 使用 React 定义节点
    • 边 Edge
      • 边总览
      • 边通用配置项
      • 三次贝塞尔曲线边 Cubic
      • 水平三次贝塞尔曲线边 CubicHorizontal
      • 垂直三次贝塞尔曲线边 CubicVertical
      • 直线边 Line
      • 二次贝塞尔曲线边 Quadratic
      • 折线边 Polyline
      • 自定义边
    • 组合 Combo
      • 组合总览
      • 组合通用配置项
      • 圆形组合 Circle
      • 矩形组合 Rect
      • 自定义 Combo
    • 图形 Shape
      • 图形 Shape 与 KeyShape
      • 原子 Shape 以及其属性
      • 复合 Shape 的设计与实现
  • 布局 Layout
    • 布局总览
    • 布局通用配置项
    • AntV Dagre 布局 AntvDagre
    • 环形布局 Circular
    • 复合布局 ComboCombined
    • 紧凑树布局 CompactBox
    • 同心圆布局 Concentric
    • 3D D3力导向布局 D3Force3D
    • D3力导向布局 D3Force
    • Dagre 布局
    • 生态树 Dendrogram
    • 鱼骨布局 Fishbone
    • ForceAtlas2 力导向布局
    • Force 力导向布局
    • Fruchterman 力导向布局
    • 网格布局 Grid
    • 缩进树 Indented
    • 高维数据降维布局 MDS
    • 脑图树 Mindmap
    • 径向布局 Radial
    • 随机布局 Random
    • 蛇形布局 Snake
    • 自定义布局
  • 交互 Behavior
    • 交互总览
    • 缩放画布 ZoomCanvas
    • 标签自适应显示 AutoAdaptLabel
    • 框选 BrushSelect
    • 点击选中 ClickSelect
    • 展开/收起元素 CollapseExpand
    • 创建边 CreateEdge
    • 拖拽画布 DragCanvas
    • 拖拽元素 DragElement
    • 力导向拖拽元素 DragElementForce
    • 缩放画布时固定元素大小 FixElementSize
    • 聚焦元素 FocusElement
    • 悬停激活 HoverActivate
    • 套索选择 LassoSelect
    • 优化视口变换 OptimizeViewportTransform
    • 滚动画布 ScrollCanvas
    • 自定义交互
  • 插件 Plugin
    • 插件总览
    • 背景 Background
    • 气泡集 BubbleSets
    • 上下文菜单 Contextmenu
    • 边绑定 EdgeBundling
    • 边过滤镜 EdgeFilterLens
    • 鱼眼放大镜 Fisheye
    • 全屏展示 Fullscreen
    • 网格线 GridLine
    • 历史记录 History
    • 轮廓包围 Hull
    • 图例 Legend
    • 小地图 Minimap
    • 对齐线 Snapline
    • 时间条 Timebar
    • 工具栏 Toolbar
    • 提示框 Tooltip
    • 水印 Watermark
    • 自定义插件
  • 数据处理 Transform
    • 数据处理总览
    • 动态调整节点大小 MapNodeSize
    • 径向标签 PlaceRadialLabels
    • 平行边 ProcessParallelEdges
    • 自定义数据处理
  • 主题 Theme
    • 主题总览
    • 自定义主题
    • 色板
    • 自定义色板
  • 动画 Animation
    • 动画总览
    • 自定义动画
  • 扩展阅读
    • 事件
    • 渲染器
    • 坐标系
    • 下载图片
    • 使用 iconfont
    • 使用 3D
    • 项目打包
  • 版本特性
    • 新版本特性
    • 升级到 5.0
  • 常见问题
  • 参与贡献

使用 React 定义节点

上一篇
自定义节点
下一篇
边总览

资源

Ant Design
Galacea Effects
Umi-React 应用开发框架
Dumi-组件/文档研发工具
ahooks-React Hooks 库

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
xtech蚂蚁体验科技
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

在 G6 中,自定义节点通常需要操作 DOM 或 Canvas 元素,但借助 @antv/g6-extension-react 一方生态库,可以直接使用 React 组件作为节点内容,提升开发效率与可维护性。

自定义节点方案选择

G6 节点

✅ 推荐场景:

  • 节点只是简单的几何图形
  • 需要高效渲染超过 2,000 个节点的场景
  • 需要直接操作图形实例进行精细控制

有关如何使用 Canvas 图形自定义节点的详细信息,请参阅 自定义节点 文档

React Node

✅ 推荐场景:

  • 需要集成 Ant Design 等 UI 库的业务系统
  • 节点包含表单输入、状态切换等交互逻辑
  • 已有 React 设计系统需要复用的场景

快速入门

环境准备

在开始之前,请确保您已经:

  • 安装 React 项目:确保已安装并创建 React 项目。
  • React 版本要求:确保使用的 React 版本 >=16.8.0。

安装依赖

要使用 @antv/g6-extension-react,请运行以下命令:

npm
yarn
pnpm
npm install @antv/g6-extension-react

组件集成

1. 注册 React 节点类型

通过扩展机制注册 React 节点类型:

import { ExtensionCategory, register } from '@antv/g6';
import { ReactNode } from '@antv/g6-extension-react';
register(ExtensionCategory.NODE, 'react-node', ReactNode);

register 方法需要三个参数:

  • 扩展类别:ExtensionCategory.NODE 表示这是一个节点类型
  • 类型名称:react-node 是我们给这个自定义节点起的名字,后续会在配置中使用
  • 类定义:ReactNode 是 @antv/g6-extension-react 导出的实现类

2. 定义业务组件

定义一个简单的 React 组件作为节点的内容:

const MyReactNode = () => {
return <div>node</div>;
};

3. 使用组件

在图配置中使用自定义的 React 节点。通过在图配置中指定节点类型和样式,来使用自定义的 React 组件。

  • type:指定节点类型为 react-node (使用与注册时起的名字)
  • style.component:定义节点的 React 组件内容
const graph = new Graph({
node: {
type: 'react-node',
style: {
component: () => <MyReactNode />,
},
},
});
graph.render();

高级功能

状态管理

在复杂图可视化场景中,节点需要动态响应交互状态。我们提供两种互补的状态管理方案:

响应内置交互状态

G6 提供内置的交互状态管理状态,如 hover-activate 和 click-select。可以通过节点数据中的 data.states 字段获取当前节点状态,并根据状态调整节点样式。

示例:在节点被 hover 时改变背景颜色。

import { ExtensionCategory, register, Graph } from '@antv/g6';
import { ReactNode } from '@antv/g6-extension-react';
register(ExtensionCategory.NODE, 'react-node', ReactNode);
const StatefulNode = ({ data }) => {
const isActive = data.states?.includes('active');
return (
<div
style={{
width: 100,
padding: 5,
border: '1px solid #eee',
boxShadow: isActive ? '0 0 8px rgba(24,144,255,0.8)' : 'none',
transform: `scale(${isActive ? 1.05 : 1})`,
}}
>
{data.data.label}
</div>
);
};
const graph = new Graph({
data: {
nodes: [
{ id: 'node1', style: { x: 100, y: 200 }, data: { label: 'node1' } },
{ id: 'node2', style: { x: 300, y: 200 }, data: { label: 'node2' } },
],
},
node: {
type: 'react-node',
style: {
component: (data) => <StatefulNode data={data} />,
},
},
behaviors: ['hover-activate'],
});
graph.render();

自定义业务状态

当需要管理业务相关状态(如审批状态、风险等级)时,可通过扩展节点数据实现:

示例:通过 data 添加 selected 变量,实现节点选中和取消选中的样式变化。

import { ExtensionCategory, register, Graph } from '@antv/g6';
import { ReactNode } from '@antv/g6-extension-react';
register(ExtensionCategory.NODE, 'react-node', ReactNode);
const MyReactNode = ({ data, graph }) => {
const handleClick = () => {
graph.updateNodeData([{ id: data.id, data: { selected: !data.data.selected } }]);
graph.draw();
};
return (
<div
style={{
width: 200,
padding: 10,
border: '1px solid red',
borderColor: data.data.selected ? 'orange' : '#ddd', // 根据选中状态设置边框颜色
cursor: 'pointer', // 添加鼠标指针样式
}}
onClick={handleClick}
>
Node
</div>
);
};
const graph = new Graph({
data: {
nodes: [
{
id: 'node1',
style: { x: 100, y: 100 },
data: { selected: true },
},
],
},
node: {
type: 'react-node',
style: {
component: (data) => <MyReactNode data={data} graph={graph} />,
},
},
});
graph.render();

事件交互

实现节点与图实例的双向通信,使节点和图实例可以相互更新。

示例:通过自定义节点操作图数据,并重新渲染图形。

const IDCardNode = ({ id, selected, graph }) => {
const handleSelect = () => {
graph.updateNodeData([{ id, data: { selected: true } }]);
graph.draw();
};
return <Select onChange={handleSelect} style={{ background: selected ? 'orange' : '#eee' }} />;
};
const graph = new Graph({
node: {
type: 'react-node',
style: {
component: ({ id, data }) => <IDCardNode id={id} selected={data.selected} graph={graph} />,
},
},
});

实际案例

import { DatabaseFilled } from '@ant-design/icons';
import { ExtensionCategory, Graph, register } from '@antv/g6';
import { ReactNode } from '@antv/g6-extension-react';
import { Badge, Flex, Input, Tag, Typography } from 'antd';
import { useEffect, useRef } from 'react';
import { createRoot } from 'react-dom/client';
const { Text } = Typography;
register(ExtensionCategory.NODE, 'react', ReactNode);
const Node = ({ data, onChange }) => {
const { status, type } = data.data;
return (
<Flex
style={{
width: '100%',
height: '100%',
background: '#fff',
padding: 10,
borderRadius: 5,
border: '1px solid gray',
}}
vertical
>
<Flex align="center" justify="space-between">
<Text>
<DatabaseFilled />
Server
<Tag>{type}</Tag>
</Text>
<Badge status={status} />
</Flex>
<Text type="secondary">{data.id}</Text>
<Flex align="center">
<Text style={{ flexShrink: 0 }}>
<Text type="danger">*</Text>URL:
</Text>
<Input
style={{ borderRadius: 0, borderBottom: '1px solid #d9d9d9' }}
variant="borderless"
value={data.data?.url}
onChange={(event) => {
const url = event.target.value;
onChange?.(url);
}}
/>
</Flex>
</Flex>
);
};
export const ReactNodeDemo = () => {
const containerRef = useRef();
useEffect(() => {
const graph = new Graph({
container: containerRef.current,
data: {
nodes: [
{
id: 'local-server-1',
data: { status: 'success', type: 'local', url: 'http://localhost:3000' },
style: { x: 50, y: 50 },
},
{
id: 'remote-server-1',
data: { status: 'warning', type: 'remote' },
style: { x: 350, y: 50 },
},
],
edges: [{ source: 'local-server-1', target: 'remote-server-1' }],
},
node: {
type: 'react',
style: {
size: [240, 100],
component: (data) => <Node data={data} />,
},
},
behaviors: ['drag-element', 'zoom-canvas', 'drag-canvas'],
});
graph.render();
}, []);
return <div style={{ width: '100%', height: '100%' }} ref={containerRef}></div>;
};
const root = createRoot(document.getElementById('container'));
root.render(<ReactNodeDemo />);

import { UserOutlined } from '@ant-design/icons';
import { ExtensionCategory, Graph, register } from '@antv/g6';
import { ReactNode } from '@antv/g6-extension-react';
import { Avatar, Button, Card, Descriptions, Select, Space, Typography } from 'antd';
import React, { useEffect, useRef } from 'react';
import { createRoot } from 'react-dom/client';
const { Title, Text } = Typography;
const { Option } = Select;
register(ExtensionCategory.NODE, 'react-node', ReactNode);
const IDCardNode = ({ id, data }) => {
const { name, idNumber, address, expanded, selected, graph } = data;
const toggleExpand = (e) => {
e.stopPropagation();
graph.updateNodeData([
{
id,
data: { expanded: !expanded },
},
]);
graph.render();
};
const handleSelect = (value) => {
graph.updateNodeData([
{
id,
data: { selected: value !== 0 },
},
]);
if (value === 2) {
// 获取与当前节点相连的所有节点
const connectedNodes = graph.getNeighborNodesData(id);
connectedNodes.forEach((node) => {
graph.updateNodeData([
{
id: node.id,
data: { selected: true },
},
]);
});
}
graph.render();
};
const CardTitle = (
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
<Space>
<Avatar shape="square" size="small" icon={<UserOutlined />} />
<Title level={5} style={{ margin: 0 }}>
{name}
</Title>
<Select
value={selected ? data.selectedOption || 1 : 0}
style={{ width: 150, marginRight: 8 }}
onChange={handleSelect}
>
<Option value={0}>None</Option>
<Option value={1}>Node</Option>
<Option value={2}>Connected</Option>
</Select>
</Space>
<Button type="link" onClick={toggleExpand} style={{ padding: 0 }}>
{expanded ? 'fold' : 'expand'}
</Button>
</div>
);
return (
<Card
size="small"
title={CardTitle}
style={{
width: 340,
padding: 10,
borderRadius: 8,
borderWidth: 2,
borderColor: selected ? 'orange' : '#eee', // 根据选中状态设置边框颜色
cursor: 'pointer',
}}
>
{expanded ? (
<Descriptions bordered column={1} style={{ width: '100%', textAlign: 'center' }}>
<Descriptions.Item label="ID Number">{idNumber}</Descriptions.Item>
<Descriptions.Item label="Address">{address}</Descriptions.Item>
</Descriptions>
) : (
<Text style={{ textAlign: 'center' }}>IDCard Information</Text>
)}
</Card>
);
};
// 定义 Graph 数据
const data = {
nodes: [
{
id: 'node1',
data: {
name: 'Alice',
idNumber: 'IDUSAASD2131734',
address: '1234 Broadway, Apt 5B, New York, NY 10001',
expanded: false, // 初始状态为收缩
selected: false, // 初始状态为未选中
selectedOption: 1, // 初始选择本节点
},
style: { x: 50, y: 50 },
},
{
id: 'node2',
data: {
name: 'Bob',
idNumber: 'IDUSAASD1431920',
address: '3030 Chestnut St, Philadelphia, PA 19104',
expanded: false, // 初始状态为收缩
selected: false, // 初始状态为未选中
selectedOption: 0, // 初始不选择
},
style: { x: 700, y: 100 },
},
{
id: 'node3',
data: {
name: 'Charlie',
idNumber: 'IDUSAASD1431921',
address: '4040 Elm St, Chicago, IL 60611',
expanded: false,
selected: true,
selectedOption: 0,
},
},
{
id: 'node4',
data: {
name: 'David',
idNumber: 'IDUSAASD1431922',
address: '5050 Oak St, Houston, TX 77002',
expanded: false,
selected: false,
selectedOption: 0,
},
},
{
id: 'node5',
data: {
name: 'Eve',
idNumber: 'IDUSAASD1431923',
address: '6060 Pine St, Phoenix, AZ 85001',
expanded: false,
selected: false,
selectedOption: 0,
},
},
],
edges: [
{ source: 'node1', target: 'node2' },
{ source: 'node2', target: 'node3' },
{ source: 'node3', target: 'node4' },
{ source: 'node4', target: 'node5' },
],
};
export const ReactNodeDemo = () => {
const containerRef = useRef();
const graphRef = useRef(null);
useEffect(() => {
// 创建 Graph 实例
const graph = new Graph({
autoFit: 'view',
container: containerRef.current,
data,
node: {
type: 'react-node',
style: {
size: (datum) => (datum.data.expanded ? [340, 236] : [340, 105]), // 调整大小以适应内容
component: (data) => <IDCardNode id={data.id} data={{ ...data.data, graph: graph }} />,
},
},
behaviors: ['drag-element', 'zoom-canvas', 'drag-canvas'],
layout: {
type: 'snake',
cols: 2,
rowGap: 100,
colGap: 220,
},
});
// 渲染 Graph
graph.render();
// 保存 graph 实例
graphRef.current = graph;
return () => {
graph.destroy();
};
}, []);
return <div style={{ width: '100%', height: '100%' }} ref={containerRef}></div>;
};
// 渲染 React 组件到 DOM
const root = createRoot(document.getElementById('container'));
root.render(<ReactNodeDemo />);