logo

G2

  • 文档
  • 图表介绍
  • API
  • 图表示例
  • 主题
  • 周边生态
  • 所有产品antv logo arrow
  • 5.3.3
  • 快速上手
  • 简介
    • 什么是 G2
    • 在前端框架中使用
    • Spec 和 API
  • 核心概念
    • 图表(Chart)
      • G2 图表组成
      • 如何使用图表
    • 标记(Mark)
      • 概览
      • area
      • box
      • boxplot
      • cell
      • chord
      • density
      • gauge
      • heatmap
      • image
      • interval
      • line
      • lineX
      • lineY
      • link
      • liquid
      • sunburst
      • point
      • polygon
      • range
      • rangeX
      • rangeY
      • rect
      • shape
      • text
      • vector
      • wordCloud
    • 视图(View)
    • 数据(Data)
      • 概览
      • custom
      • ema
      • fetch
      • filter
      • fold
      • inline
      • join
      • kde
      • log
      • map
      • pick
      • rename
      • slice
      • sort
      • sortBy
    • 编码(Encode)
    • 比例尺(Scale)
      • 概览
      • band
      • linear
      • log
      • ordinal
      • point
      • quantile
      • quantize
      • sqrt
      • threshold
      • time
      • pow
    • 转换(Transform)
      • 概览
      • bin
      • binX
      • diffY
      • dodgeX
      • flexX
      • group
      • groupColor
      • groupX
      • groupY
      • jitter
      • jitterX
      • jitterY
      • normalizeY
      • pack
      • sample
      • select
      • selectX
      • selectY
      • sortColor
      • sortX
      • sortY
      • stackEnter
      • stackY
      • symmetryY
    • 坐标系(Coordinate)
      • 概览
      • fisheye
      • parallel
      • polar
      • theta
      • transpose
      • radial
      • cartesian3D
      • helix
    • 样式(Style)
    • 动画(Animate)
      • 概览
      • fadeIn
      • fadeOut
      • growInX
      • growInY
      • morphing
      • pathIn
      • scaleInX
      • scaleInY
      • scaleOutX
      • scaleOutY
      • waveIn
      • zoomIn
      • zoomOut
    • 状态(State)
    • 交互(Interaction)
      • 概览
      • brushAxisHighlight
      • brushHighlight
      • brushXHighlight
      • brushYHighlight
      • brushFilter
      • brushXFilter
      • brushYFilter
      • chartIndex
      • elementHighlight
      • elementHighlightByColor
      • elementHighlightByX
      • elementSelect
      • elementSelectByColor
      • elementSelectByX
      • fisheye
      • legendFilter
      • legendHighlight
      • poptip
      • scrollbarFilter
      • sliderFilter
    • 复合(Composition)
      • 概览
      • facetCircle
      • facetRect
      • repeatMatrix
      • spaceFlex
      • spaceLayer
      • timingKeyframe
    • 主题(Theme)
      • 概览
      • academy
      • classic
      • classicDark
    • 事件(Event)
    • 颜色映射(Color)
  • 图表 API
  • 图表组件
    • 标题(Title)
    • 坐标轴(Axis)
    • 图例(Legend)
    • 滚动条(Scrollbar)
    • 缩略轴(Slider)
    • 提示信息(Tooltip)
    • 数据标签(Label)
  • 进阶主题
    • 关系图(Graph)
      • forceGraph
      • pack
      • sankey
      • tree
      • treemap
    • 地图(Geo)
      • geoPath
      • geoView
    • 3D 图表(3D Chart)
      • 绘制 3D 图表
      • point3D
      • line3D
      • interval3D
      • surface3D
    • 插件扩展(Plugin)
      • renderer
      • rough
      • lottie
      • a11y
    • 按需打包
    • 设置纹理
    • 服务端渲染(SSR)
    • Spec 函数表达式支持 (5.3.0 支持)
  • 版本特性
    • 新版本特性
    • v4 升级 v5 指南
  • 常见问题 FAQ

poptip

上一篇
legendHighlight
下一篇
scrollbarFilter

Resources

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

概述

poptip 是 G2 中的一种交互类型,用于在用户与图表元素交互时显示简洁的提示信息。它提供了一种轻量级的方式来展示数据点的详细信息,而不会干扰用户对整体图表的浏览体验。与完整的 tooltip 相比,poptip 更加简洁,通常只显示最基本的信息。

使用方式

要启用 poptip 交互,只需在图表配置中添加 interaction: 'poptip' 或使用 chart.interaction('poptip', true) 方法。

chart.options({
// 其他配置...
interaction: { poptip: true },
});

或者使用方法调用的方式:

chart.interaction('poptip', true);

配置层级

poptip 交互的配置可以在图表的 options 对象中的 interaction 属性下进行设置:

chart.options({
// 其他图表配置...
interaction: {
poptip: {
// poptip 配置项
offsetX: 10,
offsetY: 10,
// tip 样式配置
tipBackgroundColor: 'rgba(0, 0, 0, 0.75)',
tipColor: '#fff',
},
},
});

配置项

poptip 交互支持以下配置项:

属性描述类型默认值必选
offsetX提示框相对于触发点的 X 方向偏移量number8
offsetY提示框相对于触发点的 Y 方向偏移量number8

样式配置项

poptip 的样式配置项需要以 tip 为前缀:

属性描述类型默认值必选
tipBackgroundColor提示框背景色string'rgba(0,0,0,0.75)'
tipColor文本颜色string'#fff'
tipWidth提示框宽度string'max-content'
tipPadding提示框内边距string'1px 4px'
tipFontSize文本字体大小string'12px'
tipBorderRadius提示框圆角半径string'2.5px'
tipBoxShadow提示框阴影string'0 3px 6px -4px rgba(0,0,0,0.12), 0 6px 16px 0 rgba(0,0,0,0.08), 0 9px 28px 8px rgba(0,0,0,0.05)'

事件

poptip 交互会触发以下事件:

事件名描述回调参数
poptip:show当 poptip 显示时触发{ data, target, x, y }
poptip:hide当 poptip 隐藏时触发{ target }

可以通过以下方式监听这些事件:

chart.on('poptip:show', (event) => {
console.log('Poptip shown:', event.data);
});
chart.on('poptip:hide', (event) => {
console.log('Poptip hidden');
});

示例

示例一:矩形树图

example

示例二:散点图

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
// 生成模拟数据
const generateScatterData = () => {
const data = [];
const groups = ['组A', '组B', '组C', '组D'];
const centers = [
[3, 3],
[7, 7],
[3, 7],
[7, 3],
];
groups.forEach((group, i) => {
const [centerX, centerY] = centers[i];
for (let j = 0; j < 30; j++) {
// 生成围绕中心点的随机散点
const x = centerX + (Math.random() - 0.5) * 4;
const y = centerY + (Math.random() - 0.5) * 4;
const size = Math.random() * 20 + 5;
data.push({
x,
y,
size,
group,
id: `${group}-${j}`,
value: Math.round(x * y),
});
}
});
return data;
};
const data = generateScatterData();
chart.options({
type: 'point',
data,
encode: {
x: 'x',
y: 'y',
color: 'group',
size: 'size',
shape: 'circle',
},
scale: {
x: {
nice: true,
domain: [0, 10],
},
y: {
nice: true,
domain: [0, 10],
},
size: {
domain: [5, 25],
},
color: {
palette: 'category10',
},
},
style: {
fillOpacity: 0.65,
stroke: '#fff',
lineWidth: 1,
},
axis: {
x: {
title: 'X 轴',
grid: true,
},
y: {
title: 'Y 轴',
grid: true,
},
},
legend: {
color: {
position: 'top',
flipPage: false,
maxItemWidth: 80,
itemMarginBottom: 8,
layout: 'horizontal',
},
},
animate: {
enter: {
type: 'fadeIn',
duration: 800,
delay: (d, i) => i * 10,
},
},
});
chart.render();
chart.interaction('poptip', {
offsetX: 15,
offsetY: 15,
tipBackgroundColor: 'rgba(0, 0, 0, 0.85)',
tipBorderRadius: '6px',
tipPadding: '10px 12px',
tipFontSize: '12px',
tipColor: '#fff',
tipBoxShadow: '0 3px 6px -4px rgba(0, 0, 0, 0.48)',
});
chart.on('element:mouseenter', (event) => {
const { element } = event;
element.style.fillOpacity = 1;
element.style.stroke = '#000';
element.style.lineWidth = 2;
chart.render();
});
chart.on('element:mouseleave', (event) => {
const { element } = event;
element.style.fillOpacity = 0.65;
element.style.stroke = '#fff';
element.style.lineWidth = 1;
chart.render();
});

示例三:玫瑰图

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
// 简化的数据集
const data = [
{ category: '类别A', value: 27 },
{ category: '类别B', value: 25 },
{ category: '类别C', value: 23 },
{ category: '类别D', value: 21 },
{ category: '类别E', value: 19 },
{ category: '类别F', value: 17 },
{ category: '类别G', value: 12 },
{ category: '类别H', value: 10 },
{ category: '类别I', value: 6 },
];
chart.options({
type: 'interval',
data,
coordinate: {
type: 'polar',
innerRadius: 0.2,
endAngle: Math.PI * 2,
},
encode: {
x: 'category',
y: 'value',
color: 'category',
},
transform: [
{
type: 'stackY',
},
],
scale: {
color: {
palette: 'spectral',
},
},
style: {
stroke: '#fff',
lineWidth: 1,
},
legend: {
color: {
position: 'right',
},
},
animate: {
enter: {
type: 'fadeIn',
duration: 800,
},
},
});
chart.render();
chart.interaction('poptip', true);
chart.on('element:mouseenter', (event) => {
const { element } = event;
element.style.fillOpacity = 1;
element.style.stroke = '#000';
element.style.lineWidth = 2;
chart.render();
});
chart.on('element:mouseleave', (event) => {
const { element } = event;
element.style.fillOpacity = 0.85;
element.style.stroke = '#fff';
element.style.lineWidth = 1;
chart.render();
});