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

概览

上一篇
sliderFilter
下一篇
facetCircle

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 中视图复合(View Composition) 提供了在一个可视化中绘制多个图表的能力。G2 定义了一个视图树(View Graph) 去描述多视图图表(Multi-View Plot)。

({
type: 'spaceLayer',
children: [{ type: 'view' }, { type: 'view' }],
});
// API
const layer = chart.spaceLayer();
layer.view();
layer.view();

空间

最基础的视图复合方式就是空间复合(Space Composition),只是对空间进行划分。

一个比较常见的复合方式是 composition.spaceLayer:将多个图表重叠在一起。使用场景是这些视图拥有的不同的坐标系,比如下面的条形图和饼图。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
const layer = chart.spaceLayer();
// 条形图
layer
.interval()
.data([
{ genre: 'Shooter', sold: 350 },
{ genre: 'Sports', sold: 275 },
{ genre: 'Other', sold: 150 },
{ genre: 'Action', sold: 120 },
{ genre: 'Strategy', sold: 115 },
])
.encode('x', 'genre')
.encode('y', 'sold');
// 饼图
layer
.interval() // 创建一个 interval
.attr('paddingLeft', 300) // 设置位置
.attr('paddingBottom', 250)
.coordinate({ type: 'theta' }) // 指定坐标系
.transform({ type: 'stackY' })
.data([
{ genre: 'Shooter', sold: 350 },
{ genre: 'Sports', sold: 275 },
{ genre: 'Other', sold: 150 },
{ genre: 'Action', sold: 120 },
{ genre: 'Strategy', sold: 115 },
])
.encode('y', 'sold')
.encode('color', 'genre')
.legend('color', false);
chart.render();

同时也可以使用 composition.spaceFlex 去让视图水平或者竖直排列。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
const flex = chart.spaceFlex();
// 条形图
flex
.interval()
.data([
{ genre: 'Shooter', sold: 350 },
{ genre: 'Sports', sold: 275 },
{ genre: 'Other', sold: 150 },
{ genre: 'Action', sold: 120 },
{ genre: 'Strategy', sold: 115 },
])
.encode('x', 'genre')
.encode('y', 'sold');
// 饼图
flex
.interval() // 创建一个 interval
.coordinate({ type: 'theta' }) // 指定坐标系
.transform({ type: 'stackY' })
.data([
{ genre: 'Shooter', sold: 350 },
{ genre: 'Sports', sold: 275 },
{ genre: 'Other', sold: 150 },
{ genre: 'Action', sold: 120 },
{ genre: 'Strategy', sold: 115 },
])
.encode('y', 'sold')
.encode('color', 'genre')
.legend('color', false);
chart.render();

同时这些复合方式是可以嵌套的,所以很容易通过一个单独的声明去实现一个报表。

分面

分面复合(Facet Composition) 和空间复合的不同在于:它还会对数据划分,每个视图展现原始数据的一个子集。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
height: 260,
width: 800,
paddingLeft: 40,
paddingBottom: 50,
});
const facetRect = chart
.facetRect()
.data({
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/anscombe.json',
})
// 将数据按照 series 字段划分成一个个子集,
// 并且是 x 方向排列
.encode('x', 'series');
facetRect
.point()
.attr('padding', 'auto')
.attr('inset', 10)
.encode('x', 'x')
.encode('y', 'y')
.style('stroke', '#000');
chart.render();

重复

重复复合(Repeat Composition) 和分面的区别在于:它的每个视图展现的是全量数据,只不过会对编码进行重复,从而绘制出多个视图。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
width: 900,
height: 900,
padding: 'auto',
paddingLeft: 55,
paddingBottom: 45,
});
const repeatMatrix = chart
.repeatMatrix()
.data({
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/penguins.json',
// 数据处理
})
// 指定需要重复的编码
// 一共会生成 4 * 4 = 16 个视图
// 每个视图的 x 和 y 编码是下面字段的叉乘
.encode('position', [
'culmen_length_mm',
'culmen_depth_mm',
'flipper_length_mm',
'body_mass_g',
]);
repeatMatrix.point().attr('padding', 'auto').encode('color', 'species');
chart.render();

时间

时间复合在空间上管理视图,用于做动画。

import { Chart } from '@antv/g2';
(async () => {
const data = await fetch(
'https://gw.alipayobjects.com/os/antvdemo/assets/data/scatter.json',
).then((res) => res.json());
const chart = new Chart({
container: 'container',
});
// 参考 css animation 的描述
const keyframe = chart
.timingKeyframe() // 创建容器
.attr('iterationCount', 2) // 迭代次数
.attr('direction', 'alternate') // 方向
.attr('duration', 1000); // 持续时间
keyframe
.interval()
.transform({ type: 'groupX', y: 'mean' })
.data(data)
.encode('x', 'gender')
.encode('y', 'weight')
.encode('color', 'gender')
.encode('key', 'gender'); // 指定 key
keyframe
.point()
.data(data)
.encode('x', 'height')
.encode('y', 'weight')
.encode('color', 'gender')
.encode('shape', 'point')
.encode('groupKey', 'gender'); // 指定 groupKey
chart.render();