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

按需打包

上一篇
a11y
下一篇
设置纹理

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 5.0.19 版本推出了按需打包的功能。可以借助 Rollup, Webpack 等打包工具的 Tree-Shaking 能力来按需使用 G2 的特性,从而减少打包后的体积。虽然从结果上看还有不少优化空间,该功能的推出有以下几个意义:

  • 防止已有 G2 5.0 用户的包体积无意义的增加。
  • 能更清晰地分析依赖和整理 G2 5.0 的总体架构。
  • 提供一种对 G2 5.0 能力扩展的思路。

开始使用

比如打包如下的一个网页,该网页使用 G2 绘制了一个条形图:

<html>
<div id="container"></div>
<script type="module">
import { corelib, extend, Runtime } from '@antv/g2';
// 根据 Runtime 扩展 corelib 的能力
const Chart = extend(Runtime, corelib());
// 初始化扩展后的图表实例
const chart = new Chart({
container: 'container',
});
// 声明可视化
chart.options({
type: 'interval',
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',
},
});
// 渲染可视化
chart.render();
</script>
</html>

和直接使用 import { Chart } from '@antv/g2' 暴露出的 Chart 对象的打包体积相比如下:

import { Chart } from '@antv/g2';
const data = [
{ lib: 'Chart', size: 957772, type: 'raw' },
{ lib: 'Chart', size: 288753, type: 'gzip' },
{ lib: 'Runtime', size: 855619, type: 'raw' },
{ lib: 'Runtime', size: 252045, type: 'gzip' },
];
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
data,
encode: {
x: 'lib',
y: 'size',
color: 'type',
},
transform: [{ type: 'dodgeX' }],
scale: {
y: { nice: true },
},
axis: {
y: { labelFormatter: (d) => d / 1000 + 'kb' },
x: { title: false },
},
legend: {
color: { title: false },
},
labels: [{ text: (d) => (d.size / 1000).toFixed(2) + 'kb' }],
});
chart.render();

可以发现体积减少了 100kb 左右。

原理

G2 的架构是由 Runtime + library 构成的。Runtime 负责渲染流程,library 是一个有一系列可视化组件构成的 JavaScript Object,用于在整个渲染的不同阶段处理或者绘制数据。

const library = {
'mark.interval': Interval,
'mark.forceGraph': ForceGraph,
'mark.geoPath': GeoPath,
'scale.linear': Linear,
'scale.log': Log,
//...
};

所以打包后 G2 的大小由 Runtime 的大小和 library 共同决定。Runtime 的大小是固定的,但是 library 的大小是可以变化的:如果我的项目中只用到了统计图表,就不会依赖地理或者图分析相关的能力,这部分能力对应的可视化组件就不用包含在使用的 library 里面。

基于上面的理论,我们可以通过控制 library 包含的可视化组件来控制最后的包体积。

使用指南

下面简单介绍一下和按需打包相关的 API。

G2.Runtime(options)

返回一个 G2 运行时。该运行时不包含任何 Library,需要配合 G2.extend 一起使用。

可以单独使用一个 library,比如只使用核心的能力:

import { Runtime, extend, corelib } from '@antv/g2';
const Chart = extend(Runtime, corelib());

也可以同时使用多个 library,比如使用核心和地理能力:

import { Runtime, extend, corelib, geolib } from '@antv/g2';
const Chart = extend(Runtime, {
...corelib(),
...geolib(),
});

G2.extend(Runtime, library)

根据指定的 library 对 Runtime 进行增强,包括增加类型和对应的 Chart API。

import { Runtime, extend, corelib } from '@antv/g2';
const Chart = extend(Runtime, corelib());

G2.stdlib()

返回标准库,包含 G2 非 3D 之外的所有能力,也就是包含 G2.corelib,G2.plotlib,G2.geolib,以及G2.graphlib 的所有可视化组件。G2.Chart 就是使用了这个 library。(源码 · 案例)

import { Runtime, extend, stdlib } from '@antv/g2';
const Chart = extend(Runtime, stdlib());
const chart = new Chart();
chart.interval(); // corelib
chart.sankey(); // plotlib
chart.geoPath(); // graphlib
chart.forceGraph(); // graphlib

G2.corelib()

返回核心库,只包含基础的 Mark。可以通过源码看包含的可视化组件。(源码 · 案例)

import { Runtime, extend, corelib } from '@antv/g2';
const Chart = extend(Runtime, corelib());

G2.plotlib()

返回高级统计分析库,包含一些复杂的统计标记(Snakey 等)和转换(Venn 等)。不能单独使用,需要配合 G2.corelib 使用。可以通过源码看包含的可视化组件。(源码 · 案例)

import { Runtime, extend, corelib, plotlib } from '@antv/g2';
const Chart = extend(Runtime, {
...corelib(),
...plotlib(),
});
const chart = new Chart();
chart.sankey();

G2.geolib()

返回地理分析库,包含地理标记(GeoPath 等)和投影(Projection)。不能单独使用,需要配合 G2.corelib 使用。可以通过源码看包含的可视化组件。(源码 · 案例)

import { Runtime, extend, corelib, geolib } from '@antv/g2';
const Chart = extend(Runtime, {
...corelib(),
...geolib(),
});
const chart = new Chart();
chart.geoPath();

G2.graphlib()

返回图分析库,包含图标记(ForceGraph 等)。不能单独使用,需要配合 G2.corelib 使用。可以通过源码看包含的可视化组件。(源码 · 案例)

import { Runtime, extend, corelib, graphlib } from '@antv/g2';
const Chart = extend(Runtime, {
...corelib(),
...graphlib(),
});
const chart = new Chart();
chart.forceGraph();

G2.autolib()

返回增强分析库,提供增强分析标记(Auto 等)。该 library 会依赖 @antv/ava ,提供自动绘制图表、自动标注等能力。不能单独使用,需要配合 G2.corelib 使用。

import { Runtime, extend, corelib } from '@antv/g2';
import { autolib } from '@antv/g2-extension-ava';
const Chart = extend(Runtime, {
...corelib(),
...autolib(),
});
const chart = new Chart();
chart.auto(); // Auto Mark

G2.threedlib()

开发中,预计 10 月底上线

返回 3D 分析库,提供 3D 可视化的能力。该 library 不会包含在 G2.stdlib 里面,同样不能单独使用,需要配合 G2.corelib 使用。示例

import { Runtime, extend, corelib } from '@antv/g2';
import { threedlib } from '@antv/g2-extension-3d';
import { Renderer } from '@antv/g-webgl';
const Chart = extend(Runtime, {
...corelib(),
...threedlib(),
});
const chart = new Chart({
renderer: new Renderer(), //使用 webgl 渲染器
depth: 400, // 设置深度
});
chart.point3D();

未来工作

目前是推出了按需打包的能力,但是可以发现效果不是很明显,只减少了 10% 左右的大小。通过分析如下 G2 5.0.18 使用 G2.stdlib 依赖图可以有以下几个可以进一步优化思路:

  • 减少 Runtime 的体积:把一些能力放在 library 里面可以按需使用。
  • 依赖治理:去掉一些重复依赖,比如 @antv/util;减少一些依赖的大小 @antv/component。
  • 提供比 corelib 更小的 library:可以实现 Mark 级别的按需打包。
dep

针对第三个方法目前的思路是:提供 G2.litelib,可以按需引入 Mark。

import { Runtime, extend, litelib, Interval } from '@antv/g2';
const Chart = extend(Runtime, {
...litelib,
'mark.interval': Interval,
});
const chart = new Chart();
chart.interval();

期望中的 litelib 只包含绘制一个可视化所需要的基本组件,其余所有的组件都需要通过 import 的形式按需使用,比如使用 tooltip 交互:

import { Runtime, extend, litelib, Interval, Tooltip } from '@antv/g2';
const Chart = extend(Runtime, {
...litelib,
'mark.interval': Interval,
'interaction.tooltip': Tooltip,
});
const chart = new Chart();
chart.options({
type: 'interval',
interaction: { tooltip: true }, // 使用 tooltip 交互
});

如果大家对相关的东西感兴趣,或者对优化 G2 5.0 的包大小有想法,可以提供相关的想法或者在 G2 5.0 treeshaking 的测试环境把玩把玩!