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)

text

Previous
shape
Next
vector

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

Text is a graphic element used to draw text marks in charts. It allows users to directly add textual annotations, labels, or other explanatory content to charts, thereby enhancing the effectiveness and readability of data visualization. Text marks have numerous visual mapping channels: x, y, color, fontSize, rotate, etc. In addition, there are numerous text style-related configurations that can provide stronger expressiveness to text visualization through visual mapping. It is generally used in several scenarios:

  • Text visualization
  • Data annotation and assistance

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'view', // Chart type is 'view'
autoFit: true,
data: {
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/fb9db6b7-23a5-4c23-bbef-c54a55fee580.csv',
},
children: [
// Configure bar chart
{
type: 'interval',
encode: { x: 'letter', y: 'frequency' },
axis: { y: { labelFormatter: '.0%' } },
},
{
type: 'text', // Child view type is 'text', representing text labels
encode: { x: 'letter', y: 'frequency', text: 'frequency' }, // Data encoding configuration: x-axis corresponds to 'letter' field, y-axis corresponds to 'frequency' field, text content is the value of 'frequency' field
style: { fill: 'black', textAlign: 'center', dy: -5 }, // Text style configuration: fill color is black, text horizontally centered, y-direction offset -5 pixels
},
],
});
chart.render();

For more examples, please check the Chart Examples page.

Configuration

PropertyDescriptionTypeDefaultRequired
encodeConfigure visual channels for text marks, including x, y, color, shape, etc., to specify the relationship between visual element properties and dataencode-
styleConfigure text graphic stylesstyle-
scaleConfigure graphic scaling for text marks, including text, fontSize, etc.scale-

encode

Configure visual channels for text marks, an important configuration that defines the mapping relationship between data fields and visual attributes, determining how data is transformed into visual representation.

PropertyDescriptionTypeDefault
xBind the x property channel for text marksencode-
yBind the y property channel for text marksencode-
textBind the text property channel for text marksencode-
shapeBind the shape property channel for text marksencode-

shape

Configure the shape of the text container

ShapeDescriptionExample
textDraw textlink
badgeDraw text with markers, shaped like a balloonlink

For more encode configurations, please check the encode introduction page.

scale

scale is used to define how data maps to visual properties.

PropertyDescriptionTypeDefault
textDefine text mapping rulesscale>{type: identity}
fontSizeDefine font size mapping rules for textscale>{type: identity}
rotateDefine rotation mapping rules for textscale>{type: identity}

For more scale configurations, please check the scale introduction page.

style

style is used to set the appearance styles of text, including fill color, border styles, shadow effects, etc.

PropertyDescriptionTypeDefault
shapeModify text container graphics, consistent with shape property channel configuration in encodestring | Function<string>-
fontSizeText sizenumber | Function<number>-
fontFamilyText fontstring | Function<string>-
fontWeightFont weightnumber | Function<number>-
lineHeightLine height of textnumber | Function<number>-
textAlignSet current alignment of text content, supported properties: center | end | left | right | start, default is startstring | Function<string>-
textBaselineSet current text baseline used when drawing text, supported properties: top | middle | bottom | alphabetic | hanging. Default is bottomstring | Function<string>-
fillFill color of the graphicstring | Function<string>-
fillOpacityFill opacity of the graphicnumber | Function<number>-
strokeStroke of the graphicstring | Function<string>-
strokeOpacityStroke opacitynumber | Function<number>-
lineWidthWidth of the graphic strokenumber | Function<number>-
lineDashDashed stroke configuration. First value is the length of each dash segment, second value is the distance between segments. Setting lineDash to [0, 0] results in no stroke.[number,number] | Function<[number, number]>-
opacityOverall opacity of the graphicnumber | Function<number>-
shadowColorShadow color of the graphicstring | Function<string>-
shadowBlurGaussian blur coefficient of the graphic shadownumber | Function<number>-
shadowOffsetXHorizontal distance of shadow from the graphicnumber | Function<number>-
shadowOffsetYVertical distance of shadow from the graphicnumber | Function<number>-
cursorMouse cursor style. Same as CSS cursor style, default 'default'.string | Function<string>'default'
dxOffset of text in x directionnumber-
dyOffset of text in y directionnumber-
textText content to be drawnstring-
xX coordinate of textstring-
yY coordinate of textstring-
wordWrapWhether to enable text wrappingboolean-
wordWrapWidthMaximum width for text wrappingnumber-
backgroundBackground color of textstring-
backgroundRadiusBorder radius of text backgroundboolean-
markProperties of link markersmark-
transformConfigure text transformation propertiesstring-
colorText colorstring-

mark

When shape is badge, style also has the following configurations.

PropertyDescriptionTypeDefault
markerSizeMarker sizenumber24
markerFillMarker fill colorstring
markerFillOpacityMarker fill opacitynumber
markerStrokeMarker stroke colorstring
markerStrokeOpacityMarker stroke opacitynumber

For more style configurations, please check the style introduction page.

Try it out:

Examples

  • How to specify arrow icon length?

There are two ways to specify arrow icon length: one is by filling in pixel values, such as 40, to specify a fixed length; another is by specifying a percentage, such as 30%, to specify the relative length referring to the arrow length. The default value is 40%. Example:

chart
.vector()
// ...
.shape('vector')
.style({
arrowSize: 40,
// arrowSize: '30%',
});
  • How to draw peak value markers?

Configure selectY data transformation, set grouping groupBy: 'color' to group by color channel, and set maximum value selector selector: 'max' to use the max selector for specified data extraction, outputting to the y channel. This way you can draw text marks at the maximum value of each line.

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'view',
data: [
{ month: 'Jan', city: 'Tokyo', temperature: 7 },
{ month: 'Jan', city: 'London', temperature: 3.9 },
{ month: 'Feb', city: 'Tokyo', temperature: 6.9 },
{ month: 'Feb', city: 'London', temperature: 4.2 },
{ month: 'Mar', city: 'Tokyo', temperature: 9.5 },
{ month: 'Mar', city: 'London', temperature: 5.7 },
{ month: 'Apr', city: 'Tokyo', temperature: 14.5 },
{ month: 'Apr', city: 'London', temperature: 8.5 },
{ month: 'May', city: 'Tokyo', temperature: 18.4 },
{ month: 'May', city: 'London', temperature: 11.9 },
{ month: 'Jun', city: 'Tokyo', temperature: 21.5 },
{ month: 'Jun', city: 'London', temperature: 15.2 },
{ month: 'Jul', city: 'Tokyo', temperature: 25.2 },
{ month: 'Jul', city: 'London', temperature: 17 },
{ month: 'Aug', city: 'Tokyo', temperature: 26.5 },
{ month: 'Aug', city: 'London', temperature: 16.6 },
{ month: 'Sep', city: 'Tokyo', temperature: 23.3 },
{ month: 'Sep', city: 'London', temperature: 14.2 },
{ month: 'Oct', city: 'Tokyo', temperature: 18.3 },
{ month: 'Oct', city: 'London', temperature: 10.3 },
{ month: 'Nov', city: 'Tokyo', temperature: 13.9 },
{ month: 'Nov', city: 'London', temperature: 6.6 },
{ month: 'Dec', city: 'Tokyo', temperature: 9.6 },
{ month: 'Dec', city: 'London', temperature: 4.8 },
],
encode: { x: 'month', y: 'temperature', color: 'city' },
scale: { x: { range: [0, 1] }, y: { nice: true } },
axis: { y: { labelFormatter: (d) => d + '°C' } },
children: [
{ type: 'line', encode: { shape: 'smooth' } },
{
type: 'text',
encode: {
x: 'month',
y: 'temperature',
text: (d) => `Peak: ${d.temperature}`,
},
transform: [
{
type: 'selectY',
groupBy: 'color',
selector: 'max',
},
],
style: {
fill: 'orange',
fontSize: 16,
dy: -15,
},
tooltip: false,
},
{
type: 'point',
encode: { x: 'month', y: 'temperature' },
transform: [
{
type: 'selectY',
groupBy: 'color',
selector: 'max',
},
],
tooltip: false,
},
],
});
chart.render();