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)

treemap

Previous
tree
Next
geoPath

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

概述

矩阵树图根据每个节点的关联值递归地将空间划分为矩形,适用于展示带权的树形数据。 矩形树图适合展现具有层级关系的数据,能够直观体现同级之间的比较。一个 Tree 状结构转化为平面空间矩形的状态。矩形树图的好处在于,相比起传统的树形结构图,矩形树图能更有效得利用空间,并且拥有展示占比的功能。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'treemap',
data: {
type: 'custom',
callback: (data) => ({
name: '图表类型',
children: [
{
name: '基础图表',
children: [
{ name: '条形图', value: 300 },
{ name: '折线图', value: 600 },
{ name: '散点图', value: 160 },
{ name: '面积图', value: 160 },
{ name: '其他', value: 180 },
],
},
{
name: '数据分析',
children: [
{ name: '分箱', value: 280 },
{ name: '分组', value: 150 },
{ name: '回归线', value: 210 },
{ name: '其他', value: 40 },
],
},
],
}),
},
layout: {
tile: 'treemapBinary',
paddingInner: 1,
},
encode: { value: 'value' },
style: {
labelFill: '#000',
labelStroke: '#fff',
labelLineWidth: 1.5,
labelFontSize: 14,
labelPosition: 'top-left',
labelDx: 5,
labelDy: 5,
},
});
chart.render();

更多的案例,可以查看图表示例 - 关系图页面。

配置项

属性描述类型默认值必选
layout布局配置TreemapLayout-
encode配置 treemap 标记的视觉通道,包括x、y、color、value等,用于指定视觉元素属性和数据之间的关系encode-
style配置图形样式和标签样式--
labels自定义数据标签的配置label[][]
interaction配置 treemap 的交互Object-

layout

属性描述类型默认值必选
tile布局方式'treemapBinary' | 'treemapDice' | 'treemapSlice' | 'treemapSliceDice' | 'treemapSquarify' | 'treemapResquarify''treemapSquarify'
padding外间距,另外还有 paddingInner | paddingOuter | paddingTop | paddingBottom | paddingRight | paddingLeftnumber0
sort排序规则(a: any, b: any): number(a, b) => b.value - a.value
layer渲染层级number | (d) => number0
path渲染层级(d) => d.name0

encode

配置 treemap 标记的视觉通道。

属性描述类型默认值必选
color绑定 treemap 标记的 color 属性通道,如果将数据字段映射到颜色通道,会对数据进行分组,将数据拆分成多个不同颜色的图形encode-
value绑定 treemap 标记的数值通道encode-

style

复合图形标记需要通过不同的前缀来区分图形的配置。

  • <label>: 数据标签的前缀,例如:labelText 设置标签的 text 文本。
属性描述类型默认值必选
labelFontSize标签文字大小number10
labelText标签文字内容(d) => last(d.path)-
labelFontFamily文字字体string-
labelFontWeight字体粗细number-
labelLineHeight文字的行高number-
labelTextAlign设置文本内容的当前对齐方式center | end | left | right | start-
labelTextBaseline设置在绘制文本时使用的当前文本基线top | middle | bottom | alphabetic | hanging
labelFill文字的填充色string-
labelFillOpacity文字的填充透明度number-
labelStroke文字的描边string-
labelLineWidth文字描边的宽度number-
labelLineDash描边的虚线配置,第一个值为虚线每个分段的长度,第二个值为分段间隔的距离。labelLineDash 设为[0,0]的效果为没有描边。[number,number] -
labelStrokeOpacity描边透明度number-
labelOpacity文字的整体透明度number-
labelShadowColor文字阴影颜色string-
labelShadowBlur文字阴影的高斯模糊系数number-
labelShadowOffsetX设置阴影距文字的水平距离number-
labelShadowOffsetY设置阴影距文字的垂直距离number-

更多样式可以查看文档 - 核心概念 - 样式页面。

interaction

treemap 常用的交互是 treemapDrillDown 和 poptip

treemapDrillDown 用于实现矩形树图的下钻交互,通过点击矩形树图的某个节点,可以将该节点及其子节点展示在画布上。配置如下:

属性描述类型默认值必选
breadCrumbFill面包屑的填充色stringrgba(0, 0, 0, 0.85)
breadCrumbFontSize面包屑字体大小number12
breadCrumbY面包屑在 Y 轴的位置number12
activeFill当前激活的面包屑的填充色numberrgba(0, 0, 0, 0.5)
chart.options({
// 其他图表配置...
interaction: {
treemapDrillDown: {
breadCrumbY: 12,
activeFill: '#873bf4',
},
},
});

poptip用于交互时显示简洁的提示信息

chart.options({
// 其他图表配置...
interaction: {
poptip: {
// poptip 配置项
offsetX: 10,
offsetY: 10,
// tip 样式配置
tipBackgroundColor: 'rgba(0, 0, 0, 0.75)',
tipColor: '#fff',
},
},
});

示例

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'treemap',
data: {
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/flare-treemap.json',
},
layout: {
path: (d) => d.name.replace(/\./g, '/'),
tile: 'treemapBinary',
paddingInner: 1,
},
encode: { value: 'size' },
style: {
labelText: (d) =>
d.data.name
.split('.')
.pop()
.split(/(?=[A-Z][a-z])/g)[0],
labelFill: '#000',
labelPosition: 'top-left',
fillOpacity: 0.5,
},
});
chart.render();