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

chartIndex

上一篇
brushYFilter
下一篇
elementHighlight

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

概述

chartIndex 是一种交互组件,用于查看折线图相对于某个时间点的相对趋势。

example
import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
paddingLeft: 50,
});
chart.options({
type: 'line',
data: {
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/indices.json',
},
encode: {
x: (d) => new Date(d.Date),
y: 'Close',
color: 'Symbol',
key: 'Symbol',
title: (d) => d.Date.toLocaleString(),
},
axis: {
y: {
title: '↑ Change in price (%)',
labelAutoRotate: false,
},
},
scale: {
y: {
type: 'log',
},
},
label: {
text: 'Symbol',
selector: 'last',
style: {
fontSize: 10,
},
},
interaction: {
chartIndex: {
ruleStroke: '#aaa',
labelDx: 5,
labelTextAlign: 'center',
labelStroke: '#fff',
labelLineWidth: 5,
labelFormatter: (d) => `${d.toLocaleDateString()}`,
},
},
});
chart.render();

使用方式

chartIndex 通过传入 配置项 对交互进行配置

({
type: 'line',
interaction: {
chartIndex: {},
},
});

配置层级

交互可以配置在 Mark 层级:

({
type: 'line',
interaction: { chartIndex: {} },
});

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

({
type: 'view',
children: [
{
type: 'line',
interaction: { chartIndex: {} },
},
],
});

配置项

属性描述类型默认值必选
ruleStroke指示线的颜色stringblack
ruleLineWidth指示线宽number1
ruleLineDash虚线配置,第一个值为虚线每个分段的长度,第二个值为分段间隔的距离。ruleLineDash 设为[0,0]的效果为没有描边。[number,number]
ruleStrokeOpacity指示线的透明度number
ruleOpacity整体透明度number
ruleShadowColor指示线阴影颜色string
ruleShadowBlur指示线高斯模糊系数number
ruleShadowOffsetX指示线阴影距图形的水平距离number
ruleShadowOffsetY指示线阴影距图形的垂直距离number
labelFontSize文字大小number
labelFontFamily文字字体string
labelFontWeight字体粗细number
labelLineHeight文字的行高number
labelTextAlign设置文本内容的当前对齐方式center | end | left | right | start
labelTextBaseline设置在绘制文本时使用的当前文本基线top | middle | bottom | alphabetic | hanging
labelFill文字的填充色string
labelFillOpacity文字的填充透明度number
labelStroke文字的描边string
labelLineWidth文字描边的宽度number
labelLineDash描边的虚线配置,第一个值为虚线每个分段的长度,第二个值为分段间隔的距离。labelLineDash 设为[0,0]的效果为没有描边。[number,number]
labelStrokeOpacity描边透明度number
labelOpacity文字的整体透明度number
labelShadowColor文字阴影颜色string
labelShadowBlur文字阴影的高斯模糊系数number
labelShadowOffsetX设置阴影距文字的水平距离number
labelShadowOffsetY设置阴影距文字的垂直距离number
labelDx文字在 x 方向偏移number
labelDy文字在 y 方向偏移number
labelFormatter格式化日期FormatterFunction

示例

指示线和文字

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'line',
autoFit: true,
data: {
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/indices.json',
},
encode: {
x: (d) => new Date(d.Date),
y: 'Close',
color: 'Symbol',
key: 'Symbol',
title: (d) => d.Date.toLocaleString(),
},
axis: {
y: {
title: '↑ Change in price (%)',
labelAutoRotate: false,
},
},
scale: {
y: {
type: 'log',
},
},
label: {
text: 'Symbol',
selector: 'last',
style: {
fontSize: 10,
},
},
interaction: {
tooltip: {
crosshairs: false, // 关闭辅助线
},
chartIndex: {
ruleStroke: 'pink',
ruleLineWidth: 8,
ruleLineDash: [4, 8],
ruleShadowColor: 'green',
ruleShadowBlur: 5,
ruleShadowOffsetX: 5,
ruleShadowOffsetY: 5,
ruleOpacity: 0.9,
labelDy: 30,
labelFontSize: 20,
labelTextAlign: 'center',
labelFill: 'red',
labelStroke: 'yellow',
labelLineWidth: 2,
labelFormatter: (d) => `${d.toLocaleDateString()}`,
},
},
});
chart.render();