logo

G2

  • Chart Gallery
  • Docs
  • Examples
  • Theme
  • Ecosystem
  • Productsantv logo arrow
  • 5.3.5
  • Get Started
  • Introduction
    • What is G2
    • Using in Frontend Frameworks
    • Experimental Spec API
  • Chart API
  • Core Concepts
    • G2's complete configuration system
    • Chart
      • Components of G2 Charts
      • Chart Lifecycle
    • 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

Resource

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

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference
weavefoxWeaveFox-AI Developer Community

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-Interactive solution
weavefoxWeaveFox-AI Coding Assistant
© 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.

Data Format

Treemap supports two data formats:

1. Hierarchical Structure Data (JSON)

For data that is already in hierarchical structure, it can be used directly without configuring path:

{
name: 'Root Node',
children: [
{
name: 'Child Node 1',
children: [
{ name: 'Leaf Node 1', value: 100 },
{ name: 'Leaf Node 2', value: 200 }
]
},
{ name: 'Child Node 2', value: 300 }
]
}

2. Flattened Data (CSV)

For flattened data that uses path strings to represent hierarchical relationships, you must configure the path function:

name,size
flare,
flare.analytics,
flare.analytics.cluster,
flare.analytics.cluster.AgglomerativeCluster,3938

For this data format, you must use the path configuration:

layout: {
path: (d) => d.name.replace(/\./g, '/'), // Convert dot separation to slash separation
}

Important Note: Using flattened data without configuring path will result in a "multiple roots" error. This is because:

  1. D3's stratify expects data to have id and parentId fields by default to establish hierarchical relationships
  2. Flattened data typically only has path strings (like flare.analytics.cluster) without explicit parent-child relationship fields
  3. Without path configuration, D3 cannot recognize the hierarchical structure and treats all records as root nodes
  4. When multiple root nodes exist, D3 throws a "multiple roots" error

The role of the path configuration is to tell D3 how to parse hierarchical structure from path strings and automatically infer parent-child relationships.

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
pathPath conversion function, used to parse hierarchical structure from flattened data. This configuration is required for flattened data using path strings(d) => stringundefined

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)-
labelFontFamilyLabel text fontstring-
labelFontWeightLabel text weightnumber-
labelLineHeightLabel text line heightnumber-
labelTextAlignSet the current alignment of label text contentcenter | end | left | right | start-
labelTextBaselineSet the current text baseline used when drawing label texttop | middle | bottom | alphabetic | hanging
labelFillLabel text fill colorstring-
labelFillOpacityLabel text fill opacitynumber-
labelStrokeLabel text strokestring-
labelLineWidthLabel text stroke widthnumber-
labelLineDashLabel text stroke 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] -
labelStrokeOpacityLabel text stroke opacitynumber-
labelOpacityLabel text overall opacitynumber-
labelShadowColorLabel text shadow colorstring-
labelShadowBlurLabel text shadow Gaussian blur coefficientnumber-
labelShadowOffsetXLabel text shadow horizontal offsetnumber-
labelShadowOffsetYLabel text shadow vertical offsetnumber-
labelCursorLabel text cursor stylestringdefault
labelDxLabel text horizontal offsetnumber-
labelDyLabel text vertical offsetnumber-

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