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

brushAxisHighlight

上一篇
概览
下一篇
brushHighlight

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

概述

brushAxisHighlight 交互的对象是坐标轴,通过在坐标轴上框选区域来高亮显示对应的图表元素。这种交互方式特别适合于平行坐标系图表,可以帮助用户快速筛选和分析多维数据。

  • 触发:在坐标轴上进行框选操作。
  • 结束:点击非框选范围的坐标轴区域或双击已有的框选区域。
  • 影响状态:
    • 框选范围内的元素变为 active 状态。
    • 框选范围外的元素变为 inactive 状态。

交互内置状态:

({
// 定义 inactive 状态下的元素透明度为0.5
state: { inactive: { opacity: 0.5 } },
});
example

使用方式

配置 brushAxisHighlight 交互有两种方式:

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

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

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

({
type: 'line',
interaction: {
brushAxisHighlight: {
maskFill: 'red',
maskOpacity: 0.8,
},
},
});

配置层级

交互可以配置在 Mark 层级:

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

也可以配置在 View 层级,视图上声明的交互会传递给 children 声明的标记:

({
type: 'view',
interaction: { brushAxisHighlight: true },
children: [
{
type: 'line',
// 继承父级的交互配置
},
],
});

配置项

属性描述类型默认值必选
reversebrush 是否反转,反转后选中区域外的元素会被高亮booleanfalse
facetbrush 是否跨分面,控制交互方式booleanfalse
selectedHandles可以 resize 的手柄方向string[]['handle-n','handle-e','handle-s','handle-w','handle-nw','handle-ne','handle-se','handle-sw']
brushRegion自定义框选区域,一般不用配置,G2 内部用来配置 brushXHighlight 和 brushYHighlight(x, y, x1, y1, extent) => any(x, y, x1, y1) => [x, y, x1, y1]
mask框选区域的蒙版样式对象详见下方 mask 部分
maskHandle框选区域的手柄样式对象详见下方 maskHandle 部分

mask

配置框选区域的蒙版的样式。

属性描述类型默认值必选
maskFill蒙版的填充色string#777
maskFillOpacity蒙版的填充透明度number0.3
maskStroke蒙版的描边string#fff
maskStrokeOpacity描边透明度number
maskLineWidth蒙版描边的宽度number
maskLineDash描边的虚线配置,第一个值为虚线每个分段的长度,第二个值为分段间隔的距离[number,number]
maskOpacity蒙版的整体透明度number
maskShadowColor蒙版阴影颜色string
maskShadowBlur蒙版阴影的高斯模糊系数number
maskShadowOffsetX设置阴影距蒙版的水平距离number
maskShadowOffsetY设置阴影距蒙版的垂直距离number
maskCursor鼠标样式。同 css 的鼠标样式stringdefault

在配置框选区域的蒙版样式的时候,不是以对象的形式来配置,而是以 mask前缀加属性的方式来配置。

({
interaction: {
brushAxisHighlight: {
maskFill: '#000',
maskFillOpacity: 0.2,
maskStroke: 'red',
maskStrokeOpacity: 0.9,
maskLineWidth: 2,
maskLineDash: [4, 8],
maskOpacity: 0.2,
maskShadowColor: '#d3d3d3',
maskShadowBlur: 10,
maskShadowOffsetX: 10,
maskShadowOffsetY: 10,
maskCursor: 'pointer',
},
},
});

maskHandle

八个方向的 handle 的名字分别如下(按照东南西北命名),按照 mask[handleName][styleAttribute] 格式设置对应的属性,也可以通过 maskHandleSize 设置宽度。

custom-style

属性描述类型默认值必选
mask[handleName]Render自定义蒙版手柄的渲染函数(g, options, document) => DisplayObject
mask[handleName]Size蒙版手柄的宽度string
mask[handleName]Fill蒙版手柄的填充色string
mask[handleName]FillOpacity蒙版手柄的填充透明度number
mask[handleName]Stroke蒙版手柄的描边string
mask[handleName]StrokeOpacity描边透明度number
mask[handleName]LineWidth蒙版手柄描边的宽度number
mask[handleName]LineDash描边的虚线配置,第一个值为虚线每个分段的长度,第二个值为分段间隔的距离[number,number]
mask[handleName]Opacity蒙版手柄的整体透明度number
mask[handleName]ShadowColor蒙版手柄阴影颜色string
mask[handleName]ShadowBlur蒙版手柄阴影的高斯模糊系数number
mask[handleName]ShadowOffsetX设置阴影距蒙版手柄的水平距离number
mask[handleName]ShadowOffsetY设置阴影距蒙版手柄的垂直距离number
mask[handleName]Cursor鼠标样式。同 css 的鼠标样式stringdefault

事件

监听事件

支持以下的事件:

  • brushAxis:start - 开始创建 brush 的时候触发
  • brushAxis:end - brush 更新大小和位置完成时候触发
  • brushAxis:remove - brush 移除的时候触发
  • brushAxis:highlight - brush 改变大小和位置时触发
chart.on('brushAxis:highlight', (event) => {
const { data, nativeEvent } = event;
if (nativeEvent) console.log('brushAxis:highlight', data);
});
chart.on('brushAxis:remove', (event) => {
const { data, nativeEvent } = event;
if (nativeEvent) console.log('brushAxis:remove', data);
});

触发交互

支持以下的事件:

  • brushAxis:highlight - 高亮数据
  • brushAxis:remove - 移除 brush
chart.emit('brushAxis:highlight', {
data: { selection: [[20, 30], undefined, [100, 300]] },
});
chart.emit('brushAxis:remove', {});

示例

平行坐标系框选高亮

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
const axis = {
zIndex: 1,
titlePosition: 'right',
line: true,
style: {
labelStroke: '#fff',
labelStrokeWidth: 5,
labelFontSize: 10,
labelStrokeLineJoin: 'round',
titleStroke: '#fff',
titleFontSize: 10,
titleStrokeWidth: 5,
titleStrokeLineJoin: 'round',
titleTransform: 'translate(-50%, 0) rotate(-90)',
lineStroke: 'black',
tickStroke: 'black',
lineStrokeWidth: 1,
},
};
chart.options({
type: 'line',
data: {
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/cars3.json',
},
coordinate: { type: 'parallel' },
encode: {
position: [
'economy (mpg)',
'cylinders',
'displacement (cc)',
'power (hp)',
'weight (lb)',
'0-60 mph (s)',
'year',
],
color: 'weight (lb)',
},
style: {
lineWidth: 1.5,
strokeOpacity: 0.4,
},
scale: {
color: {
type: 'sequential',
palette: 'brBG',
offset: (t) => 1 - t,
},
},
legend: {
color: {
length: 400,
layout: { justifyContent: 'center' },
},
},
state: {
active: { lineWidth: 5 },
inactive: { stroke: '#eee', opacity: 0.5 },
},
axis: Object.fromEntries(
Array.from({ length: 7 }, (_, i) => [
`position${i === 0 ? '' : i}`,
{
...axis,
title: true,
},
]),
),
interaction: {
brushAxisHighlight: {
maskFill: '#000',
maskFillOpacity: 0.2,
maskStroke: '#1890ff',
maskLineWidth: 1,
},
tooltip: false,
},
});
chart.render();

自定义手柄样式

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'point',
data: [
{ x: 10, y: 5, category: 'A' },
{ x: 15, y: 8, category: 'A' },
{ x: 20, y: 12, category: 'A' },
{ x: 25, y: 15, category: 'A' },
{ x: 30, y: 10, category: 'B' },
{ x: 35, y: 7, category: 'B' },
{ x: 40, y: 13, category: 'B' },
{ x: 45, y: 18, category: 'B' },
{ x: 50, y: 20, category: 'C' },
{ x: 55, y: 16, category: 'C' },
{ x: 60, y: 9, category: 'C' },
{ x: 65, y: 6, category: 'C' },
],
encode: {
color: 'category',
x: 'x',
y: 'y',
},
state: {
inactive: { stroke: 'gray', opacity: 0.5 },
},
interaction: {
brushAxisHighlight: {
maskHandleSize: 20,
maskHandleNFill: 'blue',
maskHandleEFill: 'red',
maskHandleSFill: 'green',
maskHandleWFill: 'yellow',
maskHandleNWFill: 'black',
maskHandleNEFill: 'steelblue',
maskHandleSEFill: 'pink',
maskHandleSWFill: 'orange',
},
},
});
chart.render();