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

elementSelectByX

上一篇
elementSelectByColor
下一篇
fisheye

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

概述

elementSelectByX 交互的对象是图表元素 element,当点击元素时,选中所有具有相同 x 通道值的元素。

  • 触发:点击元素。

  • 结束:再次点击选中的元素。

  • 影响状态:

选中的元素变为 selected 状态。

其他元素变为 unselected 状态。

交互内置状态:

({
// selected 状态下的元素为1px黑色边框
state: { selected: { lineWidth: '1', stroke: '#000' } },
});
example
import { Chart } from '@antv/g2';
const chart = new Chart({ container: 'container', paddingLeft: 50 });
chart.options({
type: 'interval',
data: {
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/f129b517-158d-41a9-83a3-3294d639b39e.csv',
format: 'csv',
},
transform: [
{ type: 'sortX', by: 'y', reverse: true, slice: 6 },
{ type: 'dodgeX' },
],
encode: { x: 'state', y: 'population', color: 'age' },
axis: { y: { labelFormatter: '~s' } },
state: { selected: { fill: 'red' }, unselected: { opacity: 0.5 } },
interaction: { elementSelectByX: true },
});
chart.render();

使用方式

配置 elementSelectByX 交互有两种方式:

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

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

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

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

配置层级

交互可以配置在 Mark 层级:

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

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

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

配置项

元素选择交互配置有两处:

  1. 交互配置
  2. 元素选中的样式

交互配置

属性描述类型默认值
background是否高亮背景booleanfalse
region点击空白区域是否触发选择booleanfalse
single是否单选,设置为 true 时每次只能选中一个 x 值对应的元素组booleanfalse
multipleSelectHotkey多选热键的 code,按住热键后变为多选,设置此属性后single无效。不设置时默认为多选模式string | string[]-

元素选中样式

元素选中样式,效果见示例自定义选中

属性描述类型默认值必选
offset主方向上的便偏移量number0
radius元素圆角number | (datum, index, data) => number0
fill元素填充色string | (datum, index, data) => string-
fillOpacity元素填充透明度number | (datum, index, data) => number-
stroke元素的描边string | (datum, index, data) => string-
strokeOpacity元素描边透明度number | (datum, index, data) => number-
lineWidth元素描边的宽度number | (datum, index, data) => number-
lineDash元素描边的虚线配置,第一个值为虚线每个分段的长度,第二个值为分段间隔的距离。lineDash 设为[0,0]的效果为没有描边。[number,number] | (datum, index, data) => [number , number]-
opacity元素的整体透明度number | (datum, index, data) => number-
shadowColor元素阴影颜色string | (datum, index, data) => string-
shadowBlur元素阴影的高斯模糊系数number | (datum, index, data) => number-
shadowOffsetX设置阴影距元素的水平距离number | (datum, index, data) => number-
shadowOffsetY设置阴影距元素的垂直距离number | (datum, index, data) => number-
cursor元素鼠标样式。同 css 的鼠标样式。string | (datum, index, data) => stringdefault
backgroundRadius背景圆角number | (datum, index, data) => number0
backgroundFill背景填充色string | (datum, index, data) => stringtransparent
backgroundFillOpacity背景填充透明度number | (datum, index, data) => number-
backgroundStroke背景的描边string | (datum, index, data) => string-
backgroundStrokeOpacity背景描边透明度number | (datum, index, data) => number-
backgroundLineWidth背景描边的宽度number | (datum, index, data) => number-
backgroundLineDash背景描边的虚线配置,第一个值为虚线每个分段的长度,第二个值为分段间隔的距离。lineDash 设为[0,0]的效果为没有描边。[number,number] | (datum, index, data) => [number , number]-
backgroundOpacity背景的整体透明度number | (datum, index, data) => number-
backgroundShadowColor背景阴影颜色string | (datum, index, data) => string-
backgroundShadowBlur背景阴影的高斯模糊系数number | (datum, index, data) => number-
backgroundShadowOffsetX设置阴影距背景的水平距离number | (datum, index, data) => number-
backgroundShadowOffsetY设置阴影距背景的垂直距离number | (datum, index, data) => number-
backgroundCursor背景鼠标样式。同 css 的鼠标样式。string | (datum, index, data) => stringdefault

在 selected 元素中配置背景的时候,不是以对象的形式来配置,而是以 background前缀加属性的方式来配置。

({
state: {
selected: {
offset: 10,
radius: 50,
fill: '#1890FF',
fillOpacity: 0.9,
stroke: '#40A9FF',
strokeOpacity: 0.9,
lineWidth: 2,
lineDash: [4, 8],
opacity: 1,
shadowColor: '#1890FF',
shadowBlur: 10,
shadowOffsetX: 5,
shadowOffsetY: 5,
cursor: 'pointer',
backgroundRadius: 50,
backgroundFill: '#E6F7FF',
backgroundFillOpacity: 0.9,
backgroundStroke: '#91D5FF',
backgroundStrokeOpacity: 0.9,
backgroundLineWidth: 2,
backgroundLineDash: [4, 8],
backgroundOpacity: 1,
backgroundShadowColor: '#91D5FF',
backgroundShadowBlur: 10,
backgroundShadowOffsetX: 5,
backgroundShadowOffsetY: 5,
backgroundCursor: 'pointer',
},
},
});

事件

监听事件

支持以下的事件:

  • element:select - 元素选中时触发
  • element:unselect - 元素取消选中时触发
chart.on('element:select', (e) => {
console.log(e.data.data);
console.log(e.nativeEvent);
});
chart.on('element:unselect', (e) => {
console.log(e.nativeEvent);
});

触发交互

支持以下的事件:

  • element:select - 选中数据
  • element:unselect - 取消选中
chart.emit('element:select', {
data: { data: [{ population: 5038433 }, { population: 3983091 }] },
});
chart.emit('element:unselect', {});

示例

基础选择

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: { elementSelectByX: true },
});
chart.render();

自定义选中

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' } },
state: {
selected: {
offset: 10,
radius: 50,
fill: (d) => (d.population > 1000000 ? '#F5222D' : '#1890FF'),
fillOpacity: 0.9,
stroke: (d) => (d.population > 1000000 ? '#FF4D4F' : '#40A9FF'),
strokeOpacity: 0.9,
lineWidth: 2,
lineDash: [4, 8],
opacity: 1,
shadowColor: (d) => (d.population > 1000000 ? '#F5222D' : '#1890FF'),
shadowBlur: 10,
shadowOffsetX: 5,
shadowOffsetY: 5,
cursor: 'pointer',
backgroundRadius: 50,
backgroundFill: (d) => (d.population > 1000000 ? '#FFF1F0' : '#E6F7FF'),
backgroundFillOpacity: 0.9,
backgroundStroke: (d) => (d.population > 1000000 ? '#FFA39E' : '#91D5FF'),
backgroundStrokeOpacity: 0.9,
backgroundLineWidth: 2,
backgroundLineDash: [4, 8],
backgroundOpacity: 1,
backgroundShadowColor: (d) =>
d.population > 1000000 ? '#FFA39E' : '#91D5FF',
backgroundShadowBlur: 10,
backgroundShadowOffsetX: 5,
backgroundShadowOffsetY: 5,
backgroundCursor: 'pointer',
},
},
interaction: {
elementSelectByX: {
background: true,
},
},
});
chart.render();

单选模式

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: {
elementSelectByX: {
single: true, // 设置为单选模式
background: true,
},
},
});
chart.render();

多选模式

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: {
elementSelectByX: {
multipleSelectHotkey: 'ShiftLeft', // 按住左 Shift 键进入多选模式
background: true,
},
},
});
chart.render();