logo

G2

  • Docs
  • Chart Introduction
  • API
  • Examples
  • Theme
  • Ecosystem
  • Productsantv logo arrow
  • 5.3.3
  • Get Started
  • Introduction
    • What is G2
    • Use In Framework
    • Experimental Spec API
  • Core Concepts
    • Chart
      • Components of G2 Charts
      • How to Use Charts
    • Mark
      • overview
      • 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
      • overview
      • custom
      • ema
      • fetch
      • filter
      • fold
      • inline
      • join
      • kde
      • log
      • map
      • pick
      • rename
      • slice
      • sort
      • sortBy
    • Encode
    • Scale
      • overview
      • band
      • linear
      • log
      • ordinal
      • point
      • pow
      • quantile
      • quantize
      • sqrt
      • threshold
      • time
    • Transform
      • overview
      • bin
      • binX
      • diffY
      • dodgeX
      • flexX
      • group
      • groupColor
      • groupX
      • groupY
      • jitter
      • jitterX
      • jitterY
      • normalizeY
      • pack
      • sample
      • select
      • selectX
      • selectX
      • sortColor
      • sortX
      • sortY
      • stackEnter
      • stackY
      • symmetryY
    • Coordinate
      • overview
      • fisheye
      • parallel
      • polar
      • radial
      • theta
      • transpose
      • cartesian3D
      • helix
    • Style
    • Animate
      • overview
      • fadeIn
      • fadeOut
      • growInX
      • growInY
      • morphing
      • pathIn
      • scaleInX
      • scaleInY
      • scaleOutX
      • scaleOutY
      • waveIn
      • zoomIn
      • zoomOut
    • State
    • Interaction
      • Overview
      • brushAxisHighlight
      • brushHighlight
      • brushXHighlight
      • brushYHighlight
      • brushFilter
      • brushXFilter
      • brushYFilter
      • chartIndex
      • elementHighlight
      • elementHighlightByColor
      • elementHighlightByX
      • elementSelect
      • elementSelectByColor
      • elementSelectByX
      • fisheye
      • legendFilter
      • legendHighlight
      • poptip
      • scrollbarFilter
      • sliderFilter
    • Composition
      • overview
      • facetCircle
      • facetRect
      • repeatMatrix
      • spaceFlex
      • spaceLayer
      • timingKeyframe
    • Theme
      • overview
      • Academy
      • classic
      • classicDark
    • event
    • Color
  • Chart API
  • Chart Component
    • 标题(Title)
    • Axis
    • Legend
    • Scrollbar
    • Slider
    • Tooltip
    • Label
  • Extra Topics
    • Graph
      • forceGraph
      • pack
      • sankey
      • tree
      • treemap
    • Geo
      • geoPath
      • geoView
    • 3D
      • Draw 3D Chart
      • point3D
      • line3D
      • interval3D
      • surface3D
    • Plugin
      • renderer
      • rough
      • lottie
      • a11y
    • Package on demand
    • Set pattern
    • Server-side rendering(SSR)
    • Spec Function Expression Support (Available in 5.3.0)
  • Whats New
    • New Version Features
    • Migration from v4 to v5
  • Frequently Asked Questions (FAQ)

pack

Previous
normalizeY
Next
sample

Resources

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-互动图形解决方案
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

概述

pack 是一种网格布局转换,它可以让图形元素在容器空间中按照行列结构紧凑排列。pack 转换主要通过以下步骤工作:

  1. 根据容器的宽高比和元素数量,计算最优的排列网格(行数和列数)
  2. 将元素按照指定方向(行优先或列优先)在网格中排列
  3. 对每个元素应用缩放和平移变换,使其适应网格单元并保持适当的间距

使用场景

pack 转换主要用于单元可视化(Unit Visualization),能够有效解决大量离散元素的布局问题,常见的使用场景如下:

  • 在有限空间内展示大量的离散元素:如数据点密集且需要避免重叠的散点图
  • 分类数据的分布展示:突出显示各类别数量差异的可视化,如人口统计分析

例如下面的案例展示了泰坦尼克号乘客按照舱位等级和生存状态的分布情况,通过 pack 转换让每个乘客点有序地排列,清晰地展示了各个类别的数量分布。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'facetRect',
autoFit: true,
data: {
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/titanic.json',
transform: [
{ type: 'sortBy', fields: ['survived'] },
{
type: 'map',
callback: ({ survived, ...d }) => ({
...d,
survived: survived + '',
}),
},
],
},
encode: { x: 'pclass' },
children: [
{
type: 'point',
encode: { color: 'survived', shape: 'point', size: 3 },
transform: [{ type: 'pack' }],
legend: {
color: { labelFormatter: (d) => (d === '1' ? 'Yes' : 'No') },
},
tooltip: { title: '', items: ['pclass', 'survived'] },
},
],
});
chart.render();

更多的案例,可以查看单元可视化页面。

配置项

属性描述类型默认值必选
padding每个元素之间的间距,单位为像素number0
direction元素的堆叠方向row | colcol

padding

padding 用于控制每个元素之间的间距,单位为像素。增加 padding 值可以让元素之间有更明显的分隔。当值为 0 时,元素会紧密排列。

direction

direction 决定元素在网格中的堆叠方向:

  • col:列(垂直方向)堆叠
  • row:行(水平方向)堆叠

通过调整 direction,可以改变图中数据点的布局方向,以适应不同的阅读偏好和数据特点。

示例

以下案例展示了泰坦尼克号乘客性别和生存状态的分布,通过配置 pack 转换的 padding 和 direction 参数,使得结果更为直观。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'facetRect',
autoFit: true,
shareData: true,
data: {
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/titanic.json',
transform: [
{ type: 'sortBy', fields: ['survived'] },
{
type: 'map',
callback: ({ survived, ...d }) => ({
...d,
survived: survived + '',
}),
},
],
},
encode: { x: 'sex' },
children: [
{
type: 'point',
encode: { color: 'survived', shape: 'point', size: 3 },
transform: [{ type: 'pack', padding: 5, direction: 'row' }],
legend: {
color: { labelFormatter: (d) => (d === '1' ? 'Yes' : 'No') },
},
tooltip: { title: '', items: ['sex', 'survived'] },
},
],
});
chart.render();