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

提示框 Tooltip

上一篇
工具栏 Toolbar
下一篇
水印 Watermark

资源

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

概述

Tooltip 插件用于在用户将鼠标悬停或点击图中的元素时,显示额外的信息。它可以帮助用户更好地理解图中的数据,提高交互体验。

使用场景

  • 详细信息展示:当用户需要了解元素的详细信息时,使用 Tooltip 提示框来展示这些信息
  • 数据可视化辅助:在数据可视化中,Tooltip 可以显示图表中数据点的详细信息,帮助用户更好地理解数据
  • 交互反馈:为用户的鼠标操作提供即时的视觉反馈

基本使用

最简单的 Tooltip 插件配置:

const graph = new Graph({
// 其他配置...
plugins: [
{
type: 'tooltip',
},
],
});

配置项

属性描述类型默认值必选
type插件类型stringtooltip✓
key标识符string-
position气泡框位置top | bottom | left | right | top-left | top-right | bottom-left | bottom-righttop-right
enable插件是否启用boolean | ((event: IElementEvent, items: NodeData | EdgeData | ComboData[]) => boolean)true
getContent自定义内容(event: IElementEvent, items: NodeData | EdgeData | ComboData[]) => Promise<HTMLElement | string>-
onOpenChange显示隐藏的回调(open: boolean) => void-
trigger触发行为hover | clickhover
containertooltip自定义渲染的容器string | HTMLElement-
offset偏移距离[number,number][10,10]
enterable指针是否可以进入booleanfalse
title标题string-
style样式对象Record<string,any>{'.tooltip': { visibility: 'hidden'}}

详细配置说明

enable - 条件启用

控制插件是否启用,支持传入函数动态调整启用逻辑。

示例:只对节点启用 Tooltip

import { Graph } from '@antv/g6';
const data = {
nodes: [
{ id: 'node1', style: { x: 100, y: 100 }, data: { name: '服务器节点' } },
{ id: 'node2', style: { x: 200, y: 100 }, data: { name: '数据库节点' } },
],
edges: [{ source: 'node1', target: 'node2', data: { type: '连接线' } }],
};
const graph = new Graph({
container: 'container',
width: 400,
height: 200,
data,
plugins: [
{
type: 'tooltip',
// 只对节点启用,边不显示tooltip
enable: (e) => e.targetType === 'node',
getContent: (e, items) => {
return `<div>节点: ${items[0].data.name}</div>`;
},
},
],
});
graph.render();

getContent - 自定义内容

自定义渲染 Tooltip 内容,支持返回 HTMLElement 或 string。

示例:动态渲染自定义 HTML 内容

import { Graph } from '@antv/g6';
const data = {
nodes: [
{
id: 'node1',
style: { x: 100, y: 100 },
data: { name: '服务器A', type: '应用服务器', status: '运行中', cpu: '45%', memory: '2.1GB' },
},
{
id: 'node2',
style: { x: 250, y: 100 },
data: { name: '数据库B', type: 'MySQL数据库', status: '正常', connections: 23, size: '500MB' },
},
],
edges: [{ source: 'node1', target: 'node2', data: { bandwidth: '1Gbps', latency: '5ms' } }],
};
const graph = new Graph({
container: 'container',
width: 400,
height: 200,
data,
plugins: [
{
type: 'tooltip',
getContent: (e, items) => {
const item = items[0];
if (e.targetType === 'node') {
return `
<div>
<h4 style="margin: 0 0 8px 0; color: #333; border-bottom: 1px solid #eee; padding-bottom: 4px;">
${item.data.name}
</h4>
<div style="margin: 4px 0; color: #666;">
<strong>类型:</strong> ${item.data.type}
</div>
<div style="margin: 4px 0; color: #666;">
<strong>状态:</strong>
<span style="color: ${item.data.status === '运行中' || item.data.status === '正常' ? '#52c41a' : '#ff4d4f'}">
${item.data.status}
</span>
</div>
${item.data.cpu ? `<div style="margin: 4px 0; color: #666;"><strong>CPU:</strong> ${item.data.cpu}</div>` : ''}
${item.data.memory ? `<div style="margin: 4px 0; color: #666;"><strong>内存:</strong> ${item.data.memory}</div>` : ''}
${item.data.connections ? `<div style="margin: 4px 0; color: #666;"><strong>连接数:</strong> ${item.data.connections}</div>` : ''}
${item.data.size ? `<div style="margin: 4px 0; color: #666;"><strong>大小:</strong> ${item.data.size}</div>` : ''}
</div>
`;
} else if (e.targetType === 'edge') {
return `
<div>
<h4 style="margin: 0 0 8px 0; color: #333;">连接信息</h4>
<div style="margin: 4px 0; color: #666;"><strong>带宽:</strong> ${item.data.bandwidth}</div>
<div style="margin: 4px 0; color: #666;"><strong>延迟:</strong> ${item.data.latency}</div>
</div>
`;
}
return '暂无信息';
},
},
],
});
graph.render();

trigger - 触发方式

控制 Tooltip 的触发行为。

可选值:

  • hover:鼠标移入元素时触发(默认)
  • click:鼠标点击元素时触发

示例:点击触发 Tooltip

import { Graph } from '@antv/g6';
const data = {
nodes: [
{ id: 'node1', style: { x: 100, y: 100 }, data: { name: '点击我' } },
{ id: 'node2', style: { x: 200, y: 100 }, data: { name: '也点击我' } },
],
edges: [{ source: 'node1', target: 'node2' }],
};
const graph = new Graph({
container: 'container',
width: 350,
height: 200,
data,
node: {
style: {
labelText: (d) => d.data.name,
},
},
plugins: [
{
type: 'tooltip',
trigger: 'click',
getContent: (e, items) => {
return `
<div>
<div style="color: #0369a1; font-weight: bold; margin-bottom: 4px;">
点击触发 🖱️
</div>
<div style="color: #0c4a6e;">
元素ID: ${items[0].id}<br/>
名称: ${items[0].data?.name || '未命名'}
</div>
</div>
`;
},
},
],
});
graph.render();

position - 显示位置

控制 Tooltip 相对于鼠标位置的显示位置。

可选值:

  • top: 顶部
  • bottom: 底部
  • left: 左侧
  • right: 右侧
  • top-left: 顶部靠左
  • top-right: 顶部靠右(默认)
  • bottom-left: 底部靠左
  • bottom-right: 底部靠右

示例:不同位置的 Tooltip

import { Graph } from '@antv/g6';
const data = {
nodes: [
{ id: 'node1', style: { x: 100, y: 100 }, data: { label: 'TOP' } },
{ id: 'node2', style: { x: 250, y: 100 }, data: { label: 'BOTTOM' } },
{ id: 'node3', style: { x: 100, y: 250 }, data: { label: 'LEFT' } },
{ id: 'node4', style: { x: 250, y: 250 }, data: { label: 'RIGHT' } },
],
};
const graph = new Graph({
container: 'container',
width: 800,
height: 400,
data,
node: { style: { labelText: (d) => d.data.label } },
plugins: [
{
key: 'tooltip-top',
type: 'tooltip',
position: 'top',
enable: (e, items) => items[0].id === 'node1',
getContent: () => `顶部显示 ⬆️`,
style: {
'.tooltip': {
background: ' #fff2e8',
border: '1px solid #ffa940',
borderRadius: 4,
},
},
},
{
key: 'tooltip-bottom',
type: 'tooltip',
position: 'bottom',
enable: (e, items) => items[0].id === 'node2',
getContent: () => `底部显示 ⬇️`,
style: {
'.tooltip': {
background: '#f6ffed',
border: '1px solid #73d13d',
borderRadius: 4,
},
},
},
{
key: 'tooltip-left',
type: 'tooltip',
position: 'left',
enable: (e, items) => items[0].id === 'node3',
getContent: () => `左侧显示 ⬅️`,
style: {
'.tooltip': {
background: '#fff1f0',
border: '1px solid #ff7875',
borderRadius: 4,
},
},
},
{
key: 'tooltip-right',
type: 'tooltip',
position: 'right',
enable: (e, items) => items[0].id === 'node4',
getContent: () => `右侧显示 ➡️`,
style: {
'.tooltip': {
background: '#f0f5ff',
border: '1px solid #597ef7',
borderRadius: 4,
},
},
},
],
});
graph.render();

offset - 偏移量

设置 Tooltip 显示位置的偏移量,以鼠标位置为基点。

import { Graph } from '@antv/g6';
const data = {
nodes: [
{ id: 'node1', style: { x: 100, y: 100 }, data: { label: '默认偏移' } },
{ id: 'node2', style: { x: 250, y: 100 }, data: { label: '自定义偏移' } },
],
};
const graph = new Graph({
container: 'container',
width: 800,
height: 200,
data,
plugins: [
{
key: 'tooltip-default',
type: 'tooltip',
enable: (e, items) => items[0].id === 'node1',
getContent: () => `默认偏移 [10,10]`,
},
{
key: 'tooltip-custom',
type: 'tooltip',
offset: [30, -10], // 向右偏移30px,向上偏移10px
enable: (e, items) => items[0].id === 'node2',
getContent: () => `自定义偏移 [30,-10]`,
},
],
});
graph.render();

enterable - 鼠标可进入

控制鼠标指针是否可以进入气泡框,常用于需要在 Tooltip 内进行交互的场景。

import { Graph } from '@antv/g6';
const data = {
nodes: [
{ id: 'node1', style: { x: 100, y: 100 }, data: { name: '用户A', email: 'usera@example.com' } },
{ id: 'node2', style: { x: 250, y: 100 }, data: { name: '用户B', email: 'userb@example.com' } },
],
};
const graph = new Graph({
container: 'container',
width: 400,
height: 200,
data,
plugins: [
{
type: 'tooltip',
enterable: true,
position: 'right',
getContent: (e, items) => {
const item = items[0];
return `
<div>
<h4 style="margin: 0 0 12px 0; color: #333;">用户操作</h4>
<div style="margin-bottom: 8px; color: #666;">
<strong>姓名:</strong> ${item.data.name}
</div>
<div style="margin-bottom: 12px; color: #666;">
<strong>邮箱:</strong> ${item.data.email}
</div>
<div style="display: flex; gap: 8px;">
<button onclick="alert('发送消息给 ${item.data.name}')"
style="padding: 4px 12px; background: #1890ff; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 12px;">
发消息
</button>
<button onclick="alert('查看 ${item.data.name} 的详情')"
style="padding: 4px 12px; background: #52c41a; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 12px;">
详情
</button>
</div>
</div>
`;
},
style: {
'.tooltip': {
background: '#fff',
borderRadius: '8px',
boxShadow: '0 4px 20px rgba(0,0,0,0.15)',
minWidth: '200px',
},
},
},
],
});
graph.render();

style - 样式自定义

自定义 Tooltip 的样式。

import { Graph } from '@antv/g6';
const data = {
nodes: [
{ id: 'node1', style: { x: 100, y: 100 }, data: { theme: 'dark', name: '深色主题' } },
{ id: 'node2', style: { x: 250, y: 100 }, data: { theme: 'light', name: '浅色主题' } },
],
};
const graph = new Graph({
container: 'container',
width: 400,
height: 200,
data,
plugins: [
{
key: 'tooltip-dark',
type: 'tooltip',
enable: (e, items) => items[0].data.theme === 'dark',
style: {
'.tooltip': {
background: '#1f1f1f',
color: '#fff',
border: '1px solid #333',
borderRadius: '8px',
fontSize: '14px',
fontFamily: 'Arial, sans-serif',
boxShadow: '0 4px 20px rgba(0,0,0,0.3)',
},
},
getContent: (e, items) => {
return `<div>🌙 ${items[0].data.name}</div>`;
},
},
{
key: 'tooltip-light',
type: 'tooltip',
enable: (e, items) => items[0].data.theme === 'light',
style: {
'.tooltip': {
background: '#ffffff',
color: '#333',
border: '1px solid #d9d9d9',
borderRadius: '8px',
fontSize: '14px',
fontFamily: 'Arial, sans-serif',
boxShadow: '0 2px 8px rgba(0,0,0,0.15)',
},
},
getContent: (e, items) => {
return `<div>☀️ ${items[0].data.name}</div>`;
},
},
],
});
graph.render();

实际案例

  • 基础提示框
  • 点击触发 Tooltip
  • 鼠标移入和点击同一元素时显示不同的提示框
  • 自定义样式的 Tooltip
  • 异步加载内容的 Tooltip