使用 React 定义节点
上一篇
自定义节点
下一篇
边总览
Loading...
在 G6 中,自定义节点通常需要操作 DOM 或 Canvas 元素,但借助 @antv/g6-extension-react
一方生态库,可以直接使用 React 组件作为节点内容,提升开发效率与可维护性。
✅ 推荐场景:
有关如何使用 Canvas 图形自定义节点的详细信息,请参阅 自定义节点 文档
✅ 推荐场景:
在开始之前,请确保您已经:
要使用 @antv/g6-extension-react
,请运行以下命令:
npm install @antv/g6-extension-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
是我们给这个自定义节点起的名字,后续会在配置中使用@antv/g6-extension-react
导出的实现类定义一个简单的 React 组件作为节点的内容:
const MyReactNode = () => {return <div>node</div>;};
在图配置中使用自定义的 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 (<divstyle={{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 (<divstyle={{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 (<Flexstyle={{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><Inputstyle={{ 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><Selectvalue={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 (<Cardsize="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,},});// 渲染 Graphgraph.render();// 保存 graph 实例graphRef.current = graph;return () => {graph.destroy();};}, []);return <div style={{ width: '100%', height: '100%' }} ref={containerRef}></div>;};// 渲染 React 组件到 DOMconst root = createRoot(document.getElementById('container'));root.render(<ReactNodeDemo />);