logo

G2

  • Chart Gallery
  • Docs
  • Examples
  • Theme
  • Ecosystem
  • Productsantv logo arrow
  • 5.3.3
  • Get Started
  • Introduction
    • What is G2
    • Using in Frontend Frameworks
    • Experimental Spec API
  • Chart 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
      • connector
      • 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
      • quantile
      • quantize
      • sqrt
      • threshold
      • time
      • pow
    • Transform
      • Overview
      • 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
      • Overview
      • fisheye
      • parallel
      • polar
      • theta
      • transpose
      • radial
      • 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
      • legendFilter
      • legendHighlight
      • poptip
      • scrollbarFilter
      • sliderFilter
      • fisheye
    • Composition
      • Overview
      • facetCircle
      • facetRect
      • repeatMatrix
      • spaceFlex
      • spaceLayer
      • timingKeyframe
    • Theme
      • Overview
      • academy
      • classic
      • classicDark
    • Events
    • Color Mapping
  • Chart Component
    • Title
    • Axis
    • Legend
    • Scrollbar
    • Slider
    • Tooltip
    • Data 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...

Overview

Treemap recursively divides space into rectangles based on the associated value of each node, suitable for displaying weighted tree data. Treemap is suitable for displaying data with hierarchical relationships and can intuitively reflect comparisons between peers. It transforms a tree structure into a state of planar space rectangles. The advantage of treemap is that compared with traditional tree structure diagrams, treemap can make more effective use of space and has the function of displaying proportions.

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'treemap',
data: {
type: 'custom',
callback: (data) => ({
name: 'Chart Types',
children: [
{
name: 'General Charts',
children: [
{ name: 'Bar Chart', value: 300 },
{ name: 'Line Chart', value: 600 },
{ name: 'Scatter Plot', value: 160 },
{ name: 'Area Chart', value: 160 },
{ name: 'Others', value: 180 },
],
},
{
name: 'Data Analysis',
children: [
{ name: 'Binning', value: 280 },
{ name: 'Grouping', value: 150 },
{ name: 'Regression', value: 210 },
{ name: 'Others', 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();

For more examples, you can check the Chart Examples - Graph page.

Options

PropertyDescriptionTypeDefaultRequired
layoutLayout configurationTreemapLayout-
encodeConfigure visual channels for treemap mark, including x, y, color, value, etc., to specify the relationship between visual element properties and dataencode-
styleConfigure graphics style and label style--
labelsCustom data label configurationlabel[][]
interactionConfigure treemap interactionsObject-

layout

PropertyDescriptionTypeDefaultRequired
tileLayout method'treemapBinary' | 'treemapDice' | 'treemapSlice' | 'treemapSliceDice' | 'treemapSquarify' | 'treemapResquarify''treemapSquarify'
paddingOuter margin, also includes paddingInner | paddingOuter | paddingTop | paddingBottom | paddingRight | paddingLeftnumber0
sortSorting rule(a: any, b: any): number(a, b) => b.value - a.value
layerRender levelnumber | (d) => number0
pathRender level(d) => d.name0

encode

Configure visual channels for the treemap mark.

PropertyDescriptionTypeDefaultRequired
colorBind the color property channel of the treemap mark. If data fields are mapped to the color channel, data will be grouped and split into multiple graphics with different colorsencode-
valueBind the value channel of the treemap markencode-

style

Composite graphic marks need to distinguish graphic configurations through different prefixes.

  • <label>: Data label prefix, for example: labelText sets the text content of the label.
PropertyDescriptionTypeDefaultRequired
labelFontSizeLabel text sizenumber10
labelTextLabel text content(d) => last(d.path)-
labelFontFamilyText fontstring-
labelFontWeightFont weightnumber-
labelLineHeightText line heightnumber-
labelTextAlignSet the current alignment of text contentcenter | end | left | right | start-
labelTextBaselineSet the current text baseline used when drawing texttop | middle | bottom | alphabetic | hanging
labelFillText fill colorstring-
labelFillOpacityText fill opacitynumber-
labelStrokeText strokestring-
labelLineWidthText stroke widthnumber-
labelLineDashStroke dash configuration, first value is the length of each dash segment, second value is the distance between segments. Setting labelLineDash to [0,0] has no stroke effect.[number,number] -
labelStrokeOpacityStroke opacitynumber-
labelOpacityOverall text opacitynumber-
labelShadowColorText shadow colorstring-
labelShadowBlurText shadow Gaussian blur coefficientnumber-
labelShadowOffsetXSet the horizontal distance of shadow from textnumber-
labelShadowOffsetYSet the vertical distance of shadow from textnumber-

For more styles, please check the Manual - Core Concepts - Style page.

interaction

Common interactions for treemap are treemapDrillDown and poptip.

treemapDrillDown is used to implement drill-down interaction for treemap. By clicking on a node in the treemap, you can display that node and its children on the canvas. Configuration is as follows:

PropertyDescriptionTypeDefaultRequired
breadCrumbFillBreadcrumb fill colorstringrgba(0, 0, 0, 0.85)
breadCrumbFontSizeBreadcrumb font sizenumber12
breadCrumbYBreadcrumb position on Y axisnumber12
activeFillFill color of currently active breadcrumbnumberrgba(0, 0, 0, 0.5)
chart.options({
// Other chart configurations...
interaction: {
treemapDrillDown: {
breadCrumbY: 12,
activeFill: '#873bf4',
},
},
});

poptip is used to display concise tooltip information during interaction.

chart.options({
// Other chart configurations...
interaction: {
poptip: {
// poptip configuration options
offsetX: 10,
offsetY: 10,
// tip style configuration
tipBackgroundColor: 'rgba(0, 0, 0, 0.75)',
tipColor: '#fff',
},
},
});

Example

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();