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

legendHighlight

上一篇
legendFilter
下一篇
poptip

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

开始使用

legendHighlight 交互的对象是图表组件 legend 。

  • 触发:鼠标悬停在图例项。
  • 结束:鼠标离开图例项。
  • 影响状态:

框选范围的元素变为 active 状态。

框选范围以外的元素变为 inactive 状态。

交互内置状态:

({
// 定义 inactive 状态下的元素透明度为0.5
state: { inactive: { opacity: 0.5 } },
});
example
import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart
.interval()
.data(profit)
.axis('y', { labelFormatter: '~s' })
.encode('x', 'month')
.encode('y', ['end', 'start'])
.encode(
'color',
d.month === 'Total' ? 'Total' : d.profit > 0 ? 'Increase' : 'Decrease',
)
.state('inactive', { opacity: 0.5 })
.legend('color', {
state: { inactive: { labelOpacity: 0.5, markerOpacity: 0.5 } },
});
chart.interaction('legendHighlight', true);
chart.render();

使用方式

传入 boolean 设置是否开启交互。

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

配置层级

图例高亮交互可以配置在 View 层级:

chart.interaction('legendHighlight', true);

配置项

当前版本的 LegendHighlgiht 交互无可配置参数。

需要设置 state 的 inactive 状态,可以参照元素状态 state

legend 组件配置

具体文档看图例 legend

事件

获得数据

  • legend:highlight - 当鼠标悬停在图例项上触发
  • legend:unhighlight - 当鼠标离开图例时触发
chart.on('legend:highlight', (e) => {
const { nativeEvent, data } = e;
if (!nativeEvent) return;
console.log(data);
});
chart.on('legend:unhighlight', (e) => {
const { nativeEvent } = e;
if (!nativeEvent) return;
console.log('unhighlight');
});

触发交互

  • legend:highlight - 高亮对应于图例值的数据
  • legend:unhighlight - 取消高亮状态
chart.emit('legend:highlight', {
data: { channel: 'color', value: 'Increase' },
});
chart.emit('legend:unhighlight', {});

示例

下面的示例展示了一个legendHighlight的基本交互功能。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
autoFit: true,
height: 300,
data: [
{ name: 'London', month: 'Jan.', value: 18.9 },
{ name: 'London', month: 'Feb.', value: 28.8 },
{ name: 'London', month: 'Mar.', value: 39.3 },
{ name: 'London', month: 'Apr.', value: 81.4 },
{ name: 'London', month: 'May', value: 47 },
{ name: 'London', month: 'Jun.', value: 20.3 },
{ name: 'London', month: 'Jul.', value: 24 },
{ name: 'London', month: 'Aug.', value: 35.6 },
{ name: 'Berlin', month: 'Jan.', value: 12.4 },
{ name: 'Berlin', month: 'Feb.', value: 23.2 },
{ name: 'Berlin', month: 'Mar.', value: 34.5 },
{ name: 'Berlin', month: 'Apr.', value: 99.7 },
{ name: 'Berlin', month: 'May', value: 52.6 },
{ name: 'Berlin', month: 'Jun.', value: 35.5 },
{ name: 'Berlin', month: 'Jul.', value: 37.4 },
{ name: 'Berlin', month: 'Aug.', value: 42.4 },
],
encode: {
x: 'month',
y: 'value',
color: 'name',
},
transform: [
{
type: 'dodgeX',
groupBy: 'x',
orderBy: 'value',
padding: 0.1,
},
],
interaction: {
legendHighlight: {
series: true,
},
},
state: { inactive: { opacity: 0.5 } },
});
chart.render();