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

概览

上一篇
样式(Style)
下一篇
fadeIn

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

动画作为可视化的重要组成部分,能显著提高数据可视化的表现力。G2 提供了完善的动画系统,支持图形在进入(enter)、更新(update)和退出(exit)三个阶段的动画效果设置,并且能根据数据特性进行动画编码,实现丰富的交互与过渡效果。

G2 动画系统的核心特性:

  • 状态驱动:基于图形的三种状态(进入、更新、退出)设置不同的动画效果
  • 数据驱动:可以将数据值映射到动画属性上,如持续时间、延迟时间等
  • 过渡效果:支持形变(morphing)、渐变、缩放等多种过渡效果
  • 时序控制:支持设置动画的顺序、缓动函数、延迟、持续时间等参数

动画属性可通过 animate 配置项设置,既可在图表整体配置中定义,也可针对具体标记设置。

动画配置

动画可在图表配置中通过 animate 属性进行设置:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'line',
autoFit: true,
data: {
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/551d80c6-a6be-4f3c-a82a-abd739e12977.csv',
},
encode: { x: 'date', y: 'close' },
animate: { enter: { type: 'pathIn', duration: 1000 } },
});
chart.render();

可把type设置为null、undefined 或 false 来关闭动画:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'line',
autoFit: true,
data: {
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/551d80c6-a6be-4f3c-a82a-abd739e12977.csv',
},
encode: { x: 'date', y: 'close' },
animate: { enter: { type: 'false' } },
});
chart.render();

可通过animate: false来关闭所有动画:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'line',
autoFit: true,
data: {
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/551d80c6-a6be-4f3c-a82a-abd739e12977.csv',
},
encode: { x: 'date', y: 'close' },
animate: false,
});
chart.render();

基本使用

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
autoFit: true,
data: [
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 },
],
encode: { x: 'genre', y: 'sold', color: 'genre' },
animate: {
enter: { type: 'fadeIn', duration: 1000 },
},
});
chart.render();

动画属性

标记是通过mark.animate指定动画属性的,一共有三个部分的动画可以指定:

enter- 新增的图形

update- 更新的图形

exit- 删除的图形

配置选项

动画配置完整选项

属性描述类型默认值必选
enter入场动画配置EnterAnimateOptions-
update更新动画配置UpdateAnimateOptions-
exit退出动画配置ExitAnimateOptions-
enterType入场动画类型stringfadeIn
enterDuration入场动画持续时间(毫秒)number300
enterDelay入场动画延迟时间(毫秒)number0
enterEasing入场动画缓动函数stringease
enterFill入场动画非运行状态的展示效果Fillboth
updateType更新动画类型stringmorphing
updateDuration更新动画持续时间(毫秒)number300
updateDelay更新动画延迟时间(毫秒)number0
updateEasing更新动画缓动函数stringease
updateFill更新动画非运行状态的展示效果Fillboth
exitType退出动画类型stringfadeOut
exitDuration退出动画持续时间(毫秒)number300
exitDelay退出动画延迟时间(毫秒)number0
exitEasing退出动画缓动函数stringease
exitFill退出动画非运行状态的展示效果Fillboth

复杂类型说明:

  • EnterAnimateOptions:入场动画配置对象,包含 type、duration、delay、easing、fill 属性
  • UpdateAnimateOptions:更新动画配置对象,包含 type、duration、delay、easing、fill 属性
  • ExitAnimateOptions:退出动画配置对象,包含 type、duration、delay、easing、fill 属性
  • Fill:动画填充模式,可选值为 'auto'、'none'、'forwards'、'backwards'、'both'

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
autoFit: true,
data: {
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/deaths.json',
},
encode: { x: 'Month', y: 'Death', color: 'Type' },
transform: [
{ type: 'stackY' },
{ type: 'stackEnter', groupBy: ['color', 'x'], duration: 2000 },
],
scale: { y: { type: 'sqrt' } },
coordinate: { type: 'polar' },
animate: { enter: { type: 'waveIn' } },
axis: { y: false },
});
chart.render();

动画类型 Type

动画类型 Type 决定了动画的视觉效果。G2 提供了多种内置动画类型,也可设置为 null、undefined 或 false 来关闭动画。常见的动画类型包括:

动画类型作用
fadeIn淡入效果,图形从透明到可见
growInX沿 X 轴方向生长效果
growInY沿 Y 轴方向生长效果
scaleInX沿 X 轴缩放进入效果
scaleInY沿 Y 轴缩放进入效果
zoomIn整体缩放进入效果
pathIn沿路径进入效果
waveIn波浪形进入效果
morphing形变效果,平滑过渡图形变化
fadeOut淡出效果,图形从可见到透明
scaleOutX沿 X 轴缩放退出效果
scaleOutY沿 Y 轴缩放退出效果
zoomOut整体缩放退出效果

缓动函数 Easing

缓动函数控制动画过程中视觉属性变化的插值方式。G2 支持以下内置缓动函数(来自 easings.net):

constantacceleratedecelerateaccelerate-deceleratedecelerate-accelerate
linearease-in / inease-out / outease-in-out / in-outease-out-in / out-in
easein-sineout-sinein-out-sineout-in-sine
stepsin-quadout-quadin-out-quadout-in-quad
step-startin-cubicout-cubicin-out-cubicout-in-cubic
step-endin-quartout-quartin-out-quartout-in-quart
in-quintout-quintin-out-quintout-in-quint
in-expoout-expoin-out-expoout-in-expo
in-circout-circin-out-circout-in-circ
in-backout-backin-out-backout-in-back
in-bounceout-bouncein-out-bounceout-in-bounce
in-elasticout-elasticin-out-elasticout-in-elastic
spring / spring-inspring-outspring-in-outspring-out-in

动画填充 Fill

动画填充属性规定了图形在动画非运行状态(如开始前、结束后)时的展示效果:

  • auto/none - 默认值,动画在第一帧开始前和最后一帧结束后不影响图形展示效果
  • forwards - 动画完成后停住,不恢复到初始状态
  • backwards - 动画开始前应用第一帧效果
  • both - 同时应用 forwards 和 backwards 的效果

动画编码

G2 支持将数据值映射到动画属性上,使动画也具备数据可视化的意义。通过 encode 可以将数据字段映射到动画属性,如 enterDuration、enterDelay 等。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
autoFit: true,
data: [
{ name: 'event planning', startTime: 1, endTime: 4 },
{ name: 'layout logistics', startTime: 3, endTime: 13 },
{ name: 'select vendors', startTime: 5, endTime: 8 },
{ name: 'hire venue', startTime: 9, endTime: 13 },
{ name: 'hire caterer', startTime: 10, endTime: 14 },
{ name: 'hire event decorators', startTime: 12, endTime: 17 },
{ name: 'rehearsal', startTime: 14, endTime: 16 },
{ name: 'event celebration', startTime: 17, endTime: 18 },
],
encode: {
x: 'name',
y: ['endTime', 'startTime'],
color: 'name',
enterDuration: (d) => (d.endTime - d.startTime) * 300,
enterDelay: (d) => d.startTime * 100,
},
coordinate: { transform: [{ type: 'transpose' }] },
});
chart.render();

分组动画

G2 提供了 stackEnter 标记转换来实现分组动画,使图形按照特定规则依次出现。该转换会先对图形进行分组,然后将它们的出现时间和持续时间在空间上进行堆叠。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'view',
autoFit: true,
data: {
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/doughnut-purchases.json',
},
children: [
{
type: 'line',
encode: { x: 'year', y: 'count', color: 'year', shape: 'smooth' },
scale: { y: { zero: true, nice: true } },
style: { gradient: 'x', gradientColor: 'start' },
animate: { enter: { type: 'pathIn', duration: 3000 } },
axis: { y: { labelFormatter: '~s' } },
},
{
type: 'point',
encode: { x: 'year', y: 'count', color: 'year', shape: 'point' },
transform: [{ type: 'stackEnter' }],
animate: { enter: { duration: 300 } },
},
{
type: 'text',
encode: { x: 'year', y: 'count', text: 'year' },
transform: [{ type: 'stackEnter' }],
style: { lineWidth: 5, stroke: '#fff', textAlign: 'center', dy: -8 },
animate: { enter: { duration: 300 } },
},
],
});
chart.render();

关键帧动画

G2 提供了制作关键帧动画的能力,可以在不同视图间平滑过渡,展示数据变化。通过 timingKeyframe 创建时间容器,使用 key 和 groupKey 通道指定元素之间的对应关系。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'timingKeyframe',
autoFit: true,
direction: 'alternate',
iterationCount: 2,
children: [
{
type: 'interval',
data: [
{ gender: 'female', height: 161.2, weight: 51.6 },
{ gender: 'female', height: 167.5, weight: 59 },
{ gender: 'female', height: 159.5, weight: 49.2 },
{ gender: 'female', height: 157, weight: 63 },
{ gender: 'female', height: 155.8, weight: 53.6 },
{ gender: 'female', height: 170, weight: 59 },
{ gender: 'man', height: 159.1, weight: 47.6 },
{ gender: 'man', height: 166, weight: 69.8 },
{ gender: 'man', height: 176.2, weight: 66.8 },
{ gender: 'man', height: 160.2, weight: 75.2 },
{ gender: 'man', height: 172.5, weight: 55.2 },
],
encode: { x: 'gender', y: 'weight', color: 'gender', key: 'gender' },
transform: [{ type: 'groupX', y: 'mean' }],
},
{
type: 'point',
data: [
{ gender: 'female', height: 161.2, weight: 51.6 },
{ gender: 'female', height: 167.5, weight: 59 },
{ gender: 'female', height: 159.5, weight: 49.2 },
{ gender: 'female', height: 157, weight: 63 },
{ gender: 'female', height: 155.8, weight: 53.6 },
{ gender: 'female', height: 170, weight: 59 },
{ gender: 'man', height: 159.1, weight: 47.6 },
{ gender: 'man', height: 166, weight: 69.8 },
{ gender: 'man', height: 176.2, weight: 66.8 },
{ gender: 'man', height: 160.2, weight: 75.2 },
{ gender: 'man', height: 172.5, weight: 55.2 },
],
encode: {
x: 'height',
y: 'weight',
color: 'gender',
shape: 'point',
groupKey: 'gender',
},
},
],
duration: 1000,
});
chart.render();

Lottie 动画

Lottie能极大丰富动画的描述能力。

lottie

具体实现可以参考:lottie。

时序动画

时序动画(timingSequence)还在开发中,敬请期待。