logo

G2

  • Docs
  • Chart Introduction
  • API
  • Examples
  • Theme
  • Ecosystem
  • Productsantv logo arrow
  • 5.3.3
  • Get Started
  • Introduction
    • What is G2
    • Use In Framework
    • Experimental Spec API
  • Core Concepts
    • Chart
      • Components of G2 Charts
      • How to Use Charts
    • Mark
      • overview
      • 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
      • overview
      • custom
      • ema
      • fetch
      • filter
      • fold
      • inline
      • join
      • kde
      • log
      • map
      • pick
      • rename
      • slice
      • sort
      • sortBy
    • Encode
    • Scale
      • overview
      • band
      • linear
      • log
      • ordinal
      • point
      • pow
      • quantile
      • quantize
      • sqrt
      • threshold
      • time
    • Transform
      • overview
      • bin
      • binX
      • diffY
      • dodgeX
      • flexX
      • group
      • groupColor
      • groupX
      • groupY
      • jitter
      • jitterX
      • jitterY
      • normalizeY
      • pack
      • sample
      • select
      • selectX
      • selectX
      • sortColor
      • sortX
      • sortY
      • stackEnter
      • stackY
      • symmetryY
    • Coordinate
      • overview
      • fisheye
      • parallel
      • polar
      • radial
      • theta
      • transpose
      • cartesian3D
      • helix
    • Style
    • Animate
      • overview
      • fadeIn
      • fadeOut
      • growInX
      • growInY
      • morphing
      • pathIn
      • scaleInX
      • scaleInY
      • scaleOutX
      • scaleOutY
      • waveIn
      • zoomIn
      • zoomOut
    • State
    • Interaction
      • Overview
      • brushAxisHighlight
      • brushHighlight
      • brushXHighlight
      • brushYHighlight
      • brushFilter
      • brushXFilter
      • brushYFilter
      • chartIndex
      • elementHighlight
      • elementHighlightByColor
      • elementHighlightByX
      • elementSelect
      • elementSelectByColor
      • elementSelectByX
      • fisheye
      • legendFilter
      • legendHighlight
      • poptip
      • scrollbarFilter
      • sliderFilter
    • Composition
      • overview
      • facetCircle
      • facetRect
      • repeatMatrix
      • spaceFlex
      • spaceLayer
      • timingKeyframe
    • Theme
      • overview
      • Academy
      • classic
      • classicDark
    • event
    • Color
  • Chart API
  • Chart Component
    • 标题(Title)
    • Axis
    • Legend
    • Scrollbar
    • Slider
    • Tooltip
    • Label
  • Extra Topics
    • Graph
      • forceGraph
      • pack
      • sankey
      • tree
      • treemap
    • Geo
      • geoPath
      • geoView
    • 3D
      • Draw 3D Chart
      • point3D
      • line3D
      • interval3D
      • surface3D
    • Plugin
      • renderer
      • rough
      • lottie
      • a11y
    • Package on demand
    • Set pattern
    • Server-side rendering(SSR)
    • Spec Function Expression Support (Available in 5.3.0)
  • Whats New
    • New Version Features
    • Migration from v4 to v5
  • Frequently Asked Questions (FAQ)

poptip

Previous
legendHighlight
Next
scrollbarFilter

Resources

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-互动图形解决方案
xtechLiven Experience technology
© 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();
});