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)

Label

Previous
Tooltip
Next
forceGraph

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

Label in G2 is one of the means to annotate charts. Multiple labels can be added to a mark:

({
type: 'interval',
labels: [
{
text: 'genre', // Specify the bound field
dy: -15, // Specify style
},
{
text: 'sold', // Specify the bound field
fill: '#fff', // Specify style
dy: 5,
},
],
});
// API method
// First way
chart
.interval()
.label({
text: 'genre', // Specify the bound field
dy: -15, //Specify style
})
.label({
text: 'sold', // Specify the bound field
fill: '#fff', // Specify style
dy: 5,
});
// Second way
chart.interval().label([
{
text: 'genre', // Specify the bound field
dy: -15, // Specify style
},
{
text: 'sold', // Specify the bound field
fill: '#fff', // Specify style
dy: 5,
},
]);

At the level of View, you can declare label transform through labelTransform:

({
type: 'view',
labelTransform: [{ type: 'overlapHide' }, { type: 'contrastReverse' }],
});
// API method
// First way
chart
.labelTransform({ type: 'overlapHide' })
.labelTransform({ type: 'contrastReverse' });
// Second way
chart.labelTransform([{ type: 'overlapHide' }, { type: 'contrastReverse' }]);

Mark Label

Each mark can have multiple labels. The configuration of a label is roughly as follows:

({
type: 'interval',
labels: [
{
text: 'name', // Bound field or a constant string
dy: -2, // @antv/g supported styles
fill: 'red', // @antv/g supported styles
selector: 'last', // Selector
transform: [], // Label transform
},
],
});

Here's a simple example:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart
.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')
.encode('y', 'sold')
// Declare the first label
.label({
text: 'genre', // Specify the bound field
style: {
dy: -15, // Specify style
},
})
// Declare the second label
.label({
text: 'sold', // Specify the bound field
style: {
fill: '#fff', // Specify style
dy: 5,
},
});
chart.render();

Selector

For the mark of a graph corresponding to multiple data items, we can select the mark that needs to be retained through selector. Currently supported values ​​are as follows:

  • first - the first one
  • last - the last one
  • function - custom selector

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart
.line()
.data({
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/indices.json',
})
.transform({ type: 'normalizeY', basis: 'first', groupBy: 'color' })
.encode('x', (d) => new Date(d.Date))
.encode('y', 'Close')
.encode('color', 'Symbol')
.axis('y', { title: '↑ Change in price (%)' })
.label({
text: 'Symbol',
selector: 'last', // Select the last one
style: {
fontSize: 10,
},
});
chart.render();

Label Transform

When the display of labels does not meet expectations, such as overlapping or unclear colors, we can use Label Transform to optimize label display.

It can be found that in the example below, the labels corresponding to times such as 2004 have overlapped.

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart
.line()
.data({
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/cb99c4ab-e0a3-4c76-9586-fe7fa2ff1a8c.csv',
})
.transform({ type: 'groupX', y: 'mean' })
.encode('x', (d) => new Date(d.date).getFullYear())
.encode('y', 'price')
.encode('color', 'symbol')
.label({
text: 'price',
fontSize: 10,
})
.tooltip({ channel: 'y', valueFormatter: '.1f' });
chart.render();

At this time, we can set the label transform for the corresponding label: overlapDodgeY, which is used to prevent the labels from overlapping in the y direction.

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart
.line()
.data({
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/cb99c4ab-e0a3-4c76-9586-fe7fa2ff1a8c.csv',
})
.transform({ type: 'groupX', y: 'mean' })
.encode('x', (d) => new Date(d.date).getFullYear())
.encode('y', 'price')
.encode('color', 'symbol')
.label({
text: 'price',
transform: [{ type: 'overlapDodgeY' }], // Appoint labelTransform
fontSize: 10,
})
.tooltip({ channel: 'y', valueFormatter: '.1f' });
chart.render();

View Level Label Transform

Label transform can also be declared at the level of view to process labels for the entire view.

({
type: 'view',
labelTransform: [],
});