logo

G2

  • 图表介绍
  • 文档
  • 图表示例
  • 主题
  • 周边生态
  • 所有产品antv logo arrow
  • 5.3.5
  • 快速上手
  • 简介
    • 什么是 G2
    • 在前端框架中使用
    • Spec 和 API
  • 图表 API
  • 核心概念
    • G2 配置项总览
    • 图表组成和使用(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
      • connector
      • 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)
  • 图表组件
    • 标题(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

lineY

上一篇
lineX
下一篇
link

资源

Ant Design
Galacea Effects
Umi-React 应用开发框架
Dumi-组件/文档研发工具
ahooks-React Hooks 库
WeaveFox-前端智能研发

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会
weavefoxWeaveFox-智能研发技术社区

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
weavefoxWeaveFox-前端智能研发
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

概述

lineY和lineX图形标记配置相似,lineY图形标记用于绘制垂直于 y 轴的辅助线,常用于绘制平均值或其他聚合数据辅助线。

数据配置方式

lineY 支持两种配置数据的方式:

方式一:直接配置 data(推荐用于简单场景)

当您需要绘制固定位置的水平线时,可以直接配置 data 为数字数组。G2 会自动将数组转换为 y 通道编码。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'view',
height: 200,
autoFit: true,
children: [
{
type: 'interval',
data: [
{ year: '1951 年', sales: 38 },
{ year: '1952 年', sales: 52 },
{ year: '1956 年', sales: 61 },
{ year: '1957 年', sales: 120 },
{ year: '1958 年', sales: 48 },
{ year: '1959 年', sales: 38 },
],
encode: { x: 'year', y: 'sales' },
},
{
type: 'lineY',
data: [100, 59], // 自动转换为 encode: { y: [100, 59] }
style: {
stroke: (v) => {
if (v >= 60) {
return 'green';
} else {
return 'red';
}
},
lineWidth: 2,
},
labels: [
// 文本具体配置可以参考 mark text
{
text: (v) => (v >= 60 ? 'lineY 分割线1' : 'lineY 分割线2'),
position: 'top-right',
textBaseline: 'bottom',
fill: '#000',
fillOpacity: 0.45,
background: true,
backgroundFill: '#000',
backgroundOpacity: 0.15,
},
],
},
],
});
chart.render();

方式二:显式配置 encode(用于复杂数据处理)

当您需要基于数据字段或配合数据变换时,应显式配置 encode 通道。这种方式更灵活,支持数据字段映射和各种数据变换。

/**
* A recreation of this demo: https://vega.github.io/vega-lite/examples/layer_precipitation_mean.html
*/
import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
autoFit: true,
});
chart.data({
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/seattle-weather.json',
});
chart
.interval()
.transform({ type: 'groupX', y: 'mean' })
.encode('x', (d) => new Date(d.date).getUTCMonth())
.encode('y', 'precipitation')
.scale('y', { tickCount: 5, domainMax: 6 })
.tooltip({ channel: 'y', valueFormatter: '.2f' });
chart
.lineY()
.transform({ type: 'groupX', y: 'mean' }) // 计算平均值
.encode('y', 'precipitation') // 显式配置y通道
.style('stroke', '#F4664A')
.style('strokeOpacity', 1)
.style('lineWidth', 2)
.style('lineDash', [3, 3]);
chart.render();

配置方式对比

配置方式适用场景示例自动转换
data: [value1, value2]固定位置的辅助线data: [50, 100]✅ 自动转换为 encode.y
encode: { y: field }基于数据字段或需要变换encode('y', 'fieldName')❌ 显式配置

重要说明:

  • 当同时配置 data 数组和 encode.y 时,encode.y 优先级更高
  • data 自动转换仅在数组元素为简单值(非对象)时生效
  • 配合 transform 进行数据聚合时,必须使用 encode 方式

更多的案例,可以查看图表示例 - 线标注页面。

配置项

属性描述类型默认值必选
data配置 lineY 标记的数据,支持数字数组(会自动转换为 y 通道)或对象数组Array-
encode配置 lineY 标记的视觉通道,包括y、color等,用于指定视觉元素属性和数据之间的关系encode-✓
style配置 lineY 标记的图形样式style-

encode

配置 lineY 标记的视觉通道。

属性描述类型默认值必选
y绑定 lineY 标记的 y 属性通道。支持数据字段映射或配合 transform 计算聚合值(如平均值 mean、中位数 median)。注意:当直接配置 data 为数组时,会自动转换为 y 通道,无需重复配置。encode-✓
color绑定 lineY 标记的 color 属性通道,如果将数据字段映射到颜色通道,会对数据进行分组,将数据拆分成多个不同颜色的区域encode-

style

属性描述类型默认值必选
stroke图形的描边string | Function<string>-
strokeOpacity描边透明度number | Function<number>-
lineWidth图形描边的宽度number | Function<number>-
lineDash描边的虚线配置,第一个值为虚线每个分段的长度,第二个值为分段间隔的距离。lineDash 设为[0, 0]的效果为没有描边。[number,number] | Function<[number, number]>-
opacity图形的整体透明度number | Function<number>-
shadowColor图形阴影颜色string | Function<string>-
shadowBlur图形阴影的高斯模糊系数number | Function<number>-
shadowOffsetX设置阴影距图形的水平距离number | Function<number>-
shadowOffsetY设置阴影距图形的垂直距离number | Function<number>-
cursor鼠标样式。同 css 的鼠标样式,默认 'default'。string | Function<string>default

示例

多重阈值线

展示如何在同一图表中绘制多条不同样式的阈值线,常用于数据监控和预警系统。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
autoFit: true,
});
// 模拟时间序列数据
const data = Array.from({ length: 30 }, (_, i) => ({
day: i + 1,
value: 50 + Math.sin(i * 0.3) * 20 + Math.random() * 10,
}));
chart.options({
type: 'view',
data,
children: [
// 绘制主要的折线图
{
type: 'line',
encode: { x: 'day', y: 'value' },
style: { stroke: '#1890ff', lineWidth: 2 },
},
// 绘制数据点
{
type: 'point',
encode: { x: 'day', y: 'value' },
style: { fill: '#1890ff', r: 3 },
},
// 危险阈值线(红色)
{
type: 'lineY',
data: [75],
style: {
stroke: '#ff4d4f',
strokeOpacity: 0.8,
lineWidth: 2,
lineDash: [8, 4],
},
labels: [
{
text: '危险阈值',
position: 'top-right',
fill: '#ff4d4f',
fontWeight: 'bold',
background: true,
backgroundFill: '#fff',
backgroundOpacity: 0.9,
},
],
},
// 警告阈值线(橙色)
{
type: 'lineY',
data: [65],
style: {
stroke: '#fa8c16',
strokeOpacity: 0.8,
lineWidth: 2,
lineDash: [5, 5],
},
labels: [
{
text: '警告阈值',
position: 'top-right',
fill: '#fa8c16',
fontWeight: 'bold',
background: true,
backgroundFill: '#fff',
backgroundOpacity: 0.9,
},
],
},
// 正常阈值线(绿色)
{
type: 'lineY',
data: [35],
style: {
stroke: '#52c41a',
strokeOpacity: 0.8,
lineWidth: 2,
lineDash: [3, 3],
},
labels: [
{
text: '正常下限',
position: 'bottom-right',
fill: '#52c41a',
fontWeight: 'bold',
background: true,
backgroundFill: '#fff',
backgroundOpacity: 0.9,
},
],
},
],
});
chart.render();