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
  • 常见问题
  • 参与贡献

HTML节点 Html

上一篇
六边形节点 Hexagon
下一篇
图片节点 Image

资源

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...

概述

HTML 节点是一个自定义的矩形区域,用于显示 HTML 内容。它允许您在图形节点中嵌入任意的 HTML 元素,提供了极大的灵活性来创建复杂的自定义节点。

适用场景:

  • 用于表示复杂的自定义节点,如表格、图表或富文本
  • 适合表示自定义的可视化元素或交互组件
  • 常用于自定义图表、UI 设计、仪表板等场景
  • 需要在节点中嵌入表单、按钮等交互元素时

框架支持说明

💡 提示:

  • React 项目:推荐使用 React Node 来实现更好的组件化开发体验
  • Vue 项目:目前暂不支持 Vue Node,欢迎社区共建贡献
  • 原生 HTML:本文档介绍的 HTML 节点适用于原生 HTML 开发

在线体验

createGraph(
{
data: {
nodes: [
{
id: 'node1',
style: {
x: 300,
y: 110,
size: [120, 40],
innerHTML: `
<div style="width: 100%; height: 100%; background: #7e3feb; display: flex; justify-content: center; align-items: center;">
<span style="color: #fff; font-size: 12px;">
HTML Node
</span>
</div>`,
},
},
],
},
node: { type: 'html' },
plugins: [{ type: 'grid-line', size: 30 }],
},
{ width: 600, height: 220 },
(gui, graph) => {
gui.add({ type: 'html' }, 'type').disable();
const options = {
size: 50,
innerHTML: `
<div style="width: 100%; height: 100%; background: #7863FF; display: flex; justify-content: center; align-items: center;">
<span style="color: #fff; font-size: 20px;">
'HTML Node'
</span>
</div>`,
};
const optionFolder = gui.addFolder('html.style');
optionFolder.add(options, 'size', 0, 100, 1);
optionFolder.add(options, 'innerHTML');
optionFolder.onChange(({ property, value }) => {
graph.updateNodeData([{ id: 'node1', style: { [property]: value } }]);
graph.render();
});
},
);

设置 node.type 为 html 以使用 HTML 节点。

样式配置

如果元素有其特定的属性,我们将在下面列出。对于所有的通用样式属性,见 BaseNode

属性描述类型默认值必选
dx横行偏移量。HTML 容器默认以左上角为原点,通过 dx 来进行横向偏移number0
dy纵向偏移量。HTML 容器默认以左上角为原点,通过 dy 来进行纵向偏移number0
innerHTMLHTML 内容,可以为字符串或者 HTMLElementstring | HTMLElement-✓

示例

基础HTML节点

import { Graph } from '@antv/g6';
const graph = new Graph({
container: 'container',
data: {
nodes: [
{ id: 'node-1', data: { location: 'East', status: 'error', ip: '192.168.1.2' } },
{ id: 'node-2', data: { location: 'West', status: 'overload', ip: '192.168.1.3' } },
{ id: 'node-3', data: { location: 'South', status: 'running', ip: '192.168.1.4' } },
],
},
node: {
type: 'html',
style: {
size: [240, 80],
dx: -120,
dy: -40,
innerHTML: (d) => {
const ICON_MAP = {
error: '&#10060;',
overload: '&#9889;',
running: '&#9989;',
};
const COLOR_MAP = {
error: '#f5222d',
overload: '#faad14',
running: '#52c41a',
};
const {
data: { location, status, ip },
} = d;
const color = COLOR_MAP[status];
return `
<div
style="
width:100%;
height: 100%;
background: ${color}bb;
border: 1px solid ${color};
color: #fff;
user-select: none;
display: flex;
padding: 10px;
border-radius: 8px;
"
>
<div style="display: flex;flex-direction: column;flex: 1;">
<div style="font-weight: bold; font-size: 14px;">
${location} Node
</div>
<div style="font-size: 12px; margin-top: 4px;">
status: ${status} ${ICON_MAP[status]}
</div>
</div>
<div>
<span style="border: 1px solid white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">
${ip}
</span>
</div>
</div>`;
},
},
},
layout: {
type: 'grid',
},
behaviors: ['drag-element', 'zoom-canvas'],
});
graph.render();

带交互按钮的HTML节点

import { Graph } from '@antv/g6';
const graph = new Graph({
container: 'container',
data: {
nodes: [
{ id: 'server-1', data: { name: 'Web Server', cpu: 45, memory: 67, status: 'online' } },
{ id: 'server-2', data: { name: 'Database', cpu: 78, memory: 89, status: 'warning' } },
{ id: 'server-3', data: { name: 'Cache Server', cpu: 23, memory: 34, status: 'offline' } },
],
},
node: {
type: 'html',
style: {
size: [280, 210],
dx: -140,
dy: -105,
innerHTML: (d) => {
const { data } = d;
const statusColors = {
online: '#52c41a',
warning: '#faad14',
offline: '#f5222d',
};
return `
<div style="
width: 100%;
height: 100%;
background: #fff;
border: 2px solid ${statusColors[data.status]};
border-radius: 12px;
padding: 16px;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px;">
<h3 style="margin: 0; font-size: 16px; color: #333;">${data.name}</h3>
<span style="
background: ${statusColors[data.status]};
color: white;
padding: 2px 8px;
border-radius: 12px;
font-size: 12px;
font-weight: bold;
">${data.status.toUpperCase()}</span>
</div>
<div style="margin-bottom: 12px;">
<div style="display: flex; justify-content: space-between; margin-bottom: 4px;">
<span style="font-size: 12px; color: #666;">CPU</span>
<span style="font-size: 12px; color: #333;">${data.cpu}%</span>
</div>
<div style="background: #f0f0f0; height: 6px; border-radius: 3px; overflow: hidden;">
<div style="background: ${data.cpu > 70 ? '#f5222d' : '#52c41a'}; height: 100%; width: ${data.cpu}%; transition: width 0.3s;"></div>
</div>
</div>
<div style="margin-bottom: 12px;">
<div style="display: flex; justify-content: space-between; margin-bottom: 4px;">
<span style="font-size: 12px; color: #666;">Memory</span>
<span style="font-size: 12px; color: #333;">${data.memory}%</span>
</div>
<div style="background: #f0f0f0; height: 6px; border-radius: 3px; overflow: hidden;">
<div style="background: ${data.memory > 80 ? '#f5222d' : '#1890ff'}; height: 100%; width: ${data.memory}%; transition: width 0.3s;"></div>
</div>
</div>
<div style="display: flex; gap: 8px;">
<button
onclick="handleRestart('${d.id}')"
style="
flex: 1;
padding: 6px 12px;
background: #1890ff;
color: white;
border: none;
border-radius: 6px;
font-size: 12px;
cursor: pointer;
transition: background 0.2s;
"
onmouseover="this.style.background='#40a9ff'"
onmouseout="this.style.background='#1890ff'"
>重启</button>
<button
onclick="handleMonitor('${d.id}')"
style="
flex: 1;
padding: 6px 12px;
background: #52c41a;
color: white;
border: none;
border-radius: 6px;
font-size: 12px;
cursor: pointer;
transition: background 0.2s;
"
onmouseover="this.style.background='#73d13d'"
onmouseout="this.style.background='#52c41a'"
>监控</button>
</div>
</div>`;
},
},
},
layout: {
type: 'grid',
cols: 2,
},
behaviors: ['drag-element', 'zoom-canvas'],
});
// 全局函数处理按钮点击
window.handleRestart = (nodeId) => {
console.log(`重启服务器: ${nodeId}`);
alert(`正在重启服务器 ${nodeId}...`);
};
window.handleMonitor = (nodeId) => {
console.log(`打开监控面板: ${nodeId}`);
alert(`打开服务器 ${nodeId} 的监控面板`);
};
graph.render();

表单输入HTML节点

import { Graph } from '@antv/g6';
const graph = new Graph({
container: 'container',
data: {
nodes: [
{ id: 'form-1', data: { title: '用户信息', type: 'user-form' } },
{ id: 'form-2', data: { title: '配置面板', type: 'config-form' } },
],
},
node: {
type: 'html',
style: {
size: [300, 400],
dx: -150,
dy: -200,
innerHTML: (d) => {
const { data } = d;
return `
<div style="
width: 100%;
height: 100%;
background: #fff;
border: 1px solid #d9d9d9;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
">
<h3 style="margin: 0 0 16px 0; color: #333; font-size: 16px;">${data.title}</h3>
<div style="margin-bottom: 12px;">
<label style="display: block; margin-bottom: 4px; font-size: 14px; color: #666;">姓名</label>
<input
type="text"
placeholder="请输入姓名"
style="
width: 100%;
padding: 8px 12px;
border: 1px solid #d9d9d9;
border-radius: 4px;
font-size: 14px;
box-sizing: border-box;
"
/>
</div>
<div style="margin-bottom: 12px;">
<label style="display: block; margin-bottom: 4px; font-size: 14px; color: #666;">邮箱</label>
<input
type="email"
placeholder="请输入邮箱"
style="
width: 100%;
padding: 8px 12px;
border: 1px solid #d9d9d9;
border-radius: 4px;
font-size: 14px;
box-sizing: border-box;
"
/>
</div>
<div style="margin-bottom: 16px;">
<label style="display: block; margin-bottom: 4px; font-size: 14px; color: #666;">角色</label>
<select style="
width: 100%;
padding: 8px 12px;
border: 1px solid #d9d9d9;
border-radius: 4px;
font-size: 14px;
box-sizing: border-box;
">
<option>管理员</option>
<option>用户</option>
<option>访客</option>
</select>
</div>
<div style="display: flex; gap: 8px;">
<button
onclick="handleSave('${d.id}')"
style="
flex: 1;
padding: 8px 16px;
background: #1890ff;
color: white;
border: none;
border-radius: 4px;
font-size: 14px;
cursor: pointer;
"
>保存</button>
<button
onclick="handleCancel('${d.id}')"
style="
flex: 1;
padding: 8px 16px;
background: #f5f5f5;
color: #333;
border: 1px solid #d9d9d9;
border-radius: 4px;
font-size: 14px;
cursor: pointer;
"
>取消</button>
</div>
</div>`;
},
},
},
layout: {
type: 'grid',
cols: 2,
},
behaviors: ['drag-element', 'zoom-canvas'],
});
// 全局函数处理表单操作
window.handleSave = (nodeId) => {
console.log(`保存表单: ${nodeId}`);
alert(`表单 ${nodeId} 已保存`);
};
window.handleCancel = (nodeId) => {
console.log(`取消表单: ${nodeId}`);
alert(`取消表单 ${nodeId} 操作`);
};
graph.render();

使用注意事项

1. 性能优化

  • HTML 节点相比普通图形节点有更高的渲染成本,建议在节点数量较少时使用
  • 复杂的 HTML 结构会影响性能,建议保持结构简洁
  • 避免在 HTML 中使用过多的动画效果

2. 事件处理

  • HTML 节点中的事件处理需要通过全局函数或事件委托来实现
  • 建议将事件处理函数挂载到 window 对象上,确保在 HTML 字符串中可以访问
  • 注意防止事件冒泡影响图的交互行为

3. 样式隔离

  • HTML 节点的样式可能会受到页面全局样式的影响
  • 建议使用内联样式或确保样式的特异性足够高
  • 考虑使用 CSS-in-JS 或样式命名空间来避免样式冲突

4. 响应式设计

  • HTML 节点的尺寸是固定的,不会自动适应内容
  • 需要根据内容动态计算节点尺寸,或使用响应式布局
  • 考虑在不同缩放级别下的显示效果

5. 框架集成建议

  • React 项目:推荐使用 React Node,可以直接使用 React 组件作为节点内容
  • Vue 项目:目前暂不支持 Vue Node,如有需求欢迎社区贡献
  • 原生项目:HTML 节点是最佳选择,提供了最大的灵活性