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)

elementHighlightByX

Previous
elementHighlightByColor
Next
elementSelect

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

概述

elementHighlightByX 交互的对象是图表元素 element,当鼠标悬浮在元素上时,高亮和鼠标悬浮的元素拥有相同 x 通道值的元素。

  • 触发:鼠标悬浮在元素上。

  • 结束:鼠标移出元素。

  • 影响状态:

悬浮的元素变为 active 状态。

其他元素变为 inactive 状态。

交互内置状态:

({
// active 状态下的元素为1px黑色边框
state: { active: { lineWidth: '1', stroke: '#000' } },
});

开始使用

example
import { Chart } from '@antv/g2';
const chart = new Chart({ container: 'container' });
chart.options({
type: 'interval',
paddingLeft: 50,
data: {
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/f129b517-158d-41a9-83a3-3294d639b39e.csv',
format: 'csv',
},
encode: { x: 'state', y: 'population', color: 'age' },
transform: [
{ type: 'sortX', by: 'y', reverse: true, slice: 6 },
{ type: 'dodgeX' },
],
state: { active: { fill: 'red' }, inactive: { opacity: 0.5 } },
axis: { y: { labelFormatter: '~s' } },
interaction: { elementHighlightByX: true },
});
chart.render();

使用方式

配置 elementHighlightByX 交互有两种方式:

第一种,传入 boolean 设置是否开启交互。

({
type: 'interval',
interaction: { elementHighlightByX: true }, // 采用默认配置
});

第二种,传入 配置项 对交互进行配置。

({
type: 'line',
interaction: {
elementHighlightByX: {
background: true,
},
},
});

配置层级

交互可以配置在 Mark 层级:

({
type: 'interval',
interaction: { elementHighlightByX: true },
});

也可以配置在 View 层级,视图上声明的交互会传递给 children 声明的标记,如果该标记有声明对应的交互,就合并;否则不影响。

({
type: 'view',
interaction: { elementHighlightByX: true },
});

配置项

交互配置

属性描述类型默认值
background是否高亮背景booleanfalse
region鼠标移动到元素空白区域时是否触发高亮(效果见下图)booleanfalse

元素高亮样式配置

属性描述类型默认值
offset主方向上的便偏移量number0
background是否高亮背景backgroundStyle详见backgroundStyle
example

事件

监听事件

支持以下的事件:

  • element:highlight - 元素高亮时触发
  • element:unhighlight - 元素取消高亮时触发
chart.on('element:highlight', (e) => {
console.log(e.data.data);
console.log(e.data.group);
console.log(e.nativeEvent);
});
chart.on('element:unhighlight', (e) => {
console.log(e.nativeEvent);
});

触发交互

支持以下的事件:

  • element:highlight - 高亮数据
  • element:unhighlight - 取消高亮
chart.emit('element:highlight', {
data: { data: { population: 5038433 } },
});
chart.emit('element:unhighlight', {});

示例

基础高亮

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
paddingLeft: 50,
data: {
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/f129b517-158d-41a9-83a3-3294d639b39e.csv',
format: 'csv',
},
encode: { x: 'state', y: 'population', color: 'age' },
transform: [
{ type: 'sortX', by: 'y', reverse: true, slice: 6 },
{ type: 'dodgeX' },
],
axis: { y: { labelFormatter: '~s' } },
interaction: { elementHighlightByX: true },
});
chart.render();

自定义高亮

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
data: {
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/f129b517-158d-41a9-83a3-3294d639b39e.csv',
format: 'csv',
},
encode: { x: 'state', y: 'population', color: 'age' },
transform: [
{ type: 'stackY' },
{ type: 'sortX', by: 'y', reverse: true, slice: 5 },
],
state: {
active: { fill: 'red', linkFillOpacity: 0.5 },
inactive: { opacity: 0.5 },
},
axis: { y: { labelFormatter: '~s' } },
interaction: { elementHighlightByX: true },
});
chart.render();