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)

Title

Previous
Color Mapping
Next
Axis

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

In G2, Title is used to specify the title content of the chart, which can be used to display summary information about the chart in one sentence. Chart titles are commonly used components, consisting of a main title and a subtitle, both displayed as text. The style of the chart title can be customized by adjusting the text styles.

Usage

First, pass a boolean to set whether to display the chart title. Chart titles are hidden by default.

({
type: 'interval',
title: false; // Hide chart title
})

Second, pass titleOption to configure the chart title as a whole.

({
type: 'interval',
title: {
title: 'hello', // Main title text content
subtitle: 'world', // Subtitle text content
},
});

Titles can also be configured at the View level:

({
type: 'view',
title: {
title: 'hello',
subtitle: 'world',
},
});

Getting Started

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
marginTop: 40,
data: {
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/fb9db6b7-23a5-4c23-bbef-c54a55fee580.csv',
},
encode: {
x: 'letter',
y: 'frequency',
},
title: {
align: 'center', // Chart title alignment
size: 28, // Chart title height, default is 36
spacing: 4, // Spacing between main title and subtitle
// Title
title: "I am a title", // Chart title text
titleFontSize: 28, // Chart main title font size
titleFontFamily: 'sans-serif', // Chart main title font family
titleFontWeight: 600, // Chart main title font weight
titleFill: '#fff', // Chart main title text color
titleFillOpacity: 1, // Chart main title text opacity
titleStroke: '#000', // Chart main title text stroke color
titleLineWidth: 2, // Chart main title text stroke width
titleStrokeOpacity: 1, // Chart main title text stroke opacity
// Subtitle
subtitle: "I am a subtitle", // Chart subtitle text
subtitleFontSize: 16, // Chart subtitle font size
subtitleFontFamily: 'Arial', // Chart subtitle font family
subtitleFontWeight: 300, // Chart subtitle font weight
subtitleFill: '#2989FF', // Chart subtitle text color
subtitleFillOpacity: 1, // Chart subtitle text opacity
subtitleStroke: '#000', // Chart subtitle text stroke color
subtitleLineWidth: 1, // Chart subtitle text stroke width
subtitleStrokeOpacity: 0.5, // Chart subtitle text stroke opacity
},
});
chart.render();

The simplest way to set the title is to directly specify a string as the title, which uses default styles and positioning. You can also use complete configuration options for flexible customization.

More options about title, see the API document of title.

// API
chart.interval().title({
title: 'hello',
subtitle: 'world',
});
// API
chart.title({ title: 'hello', subtitle: 'world' });

Configuration Options

PropertyDescriptionTypeDefault Value
sizeHeight of the chart titlenumber36
alignChart title alignmentleft | center | rightleft
spacingVertical spacing between main title and subtitlenumber2
titleChart title configuration optionstitleSee title
subtitleChart subtitle configuration optionssubtitleSee subtitle

size

number optional

Used to configure the space height of the chart title. Default is 36px. If configured too small, it may cause overlap between the title and chart graphics.

align

string optional

Used to configure the horizontal alignment of the chart title. Default is left. You can choose left, center, or right, representing left-aligned, center-aligned, and right-aligned respectively.

Try it out:

(() => {
const chart = new G2.Chart({ width: 480, height: 160 });
const alignList = ['center', 'right', 'left'];
const alignMap = alignList.map((p) => {
return {
label: p,
value: p,
};
});
const data = [
264, 417, 438, 887, 309, 397, 550, 575, 563, 430, 525, 592, 492, 467, 513,
546, 983, 340, 539, 243, 226, 192,
];
chart.options({
data,
type: 'interval',
encode: {
x: (_, idx) => idx,
y: (d) => d,
},
title: {
align: 'center',
title: 'This is a chart title.',
subtitle: 'Displayed are sampled values.',
},
axis: false,
});
const handleSetAlign = (align) => {
chart.title({ align });
chart.render(); // Re-render the chart
};
const selectorContainer = document.createElement('div');
selectorContainer.textContent = 'Select title alignment ';
const selector = document.createElement('select');
selector.innerHTML = alignMap.map(
(align, index) =>
`<option value="${align.value}" ${index === 0 ? 'selected' : ''}>${
align.label
}</option>`,
);
selector.onchange = (e) => {
handleSetAlign(e.target.value);
};
selectorContainer.appendChild(selector);
const node = chart.getContainer();
node.insertBefore(selectorContainer, node.childNodes[0]);
chart.render();
return node;
})();

spacing

number optional

Used to configure the spacing between the chart main title and subtitle. Default is 2px. Appropriate spacing can make the chart look more harmonious overall.

title

The chart title, specifically the main title, can be customized with the following configurations for various title styles.

PropertyDescriptionTypeDefault Value
titleChart title text contentstring | (datum, index, data) => string-
titleFontSizeChart title font sizenumber | (datum, index, data) => number14
titleFontFamilyChart title font familystring | (datum, index, data) => stringsans-serif
titleFontWeightChart title font weightnumber | (datum, index, data) => numbernormal
titleLineHeightChart title line heightnumber | (datum, index, data) => number14
titleTextAlignChart title horizontal content alignmentstring | (datum, index, data) => stringcenter
titleTextBaselineChart title vertical baselinestring | (datum, index, data) => stringmiddle
titleFillChart title fill colorstring | (datum, index, data) => string#000
titleFillOpacityChart title fill opacitynumber | (datum, index, data) => number1
titleStrokeChart title stroke colorstring | (datum, index, data) => stringtransparent
titleStrokeOpacityChart title stroke opacitynumber | (datum, index, data) => number1
titleLineWidthChart title stroke widthnumber | (datum, index, data) => number0
titleLineDashChart title dash stylenumber[] | (datum, index, data) => number[][]
titleOpacityChart title overall opacitynumber | (datum, index, data) => number1
titleShadowColorChart title shadow colorstring | (datum, index, data) => stringtransparent
titleShadowBlurChart title shadow gaussian blur coefficientnumber | (datum, index, data) => number0
titleShadowOffsetXChart title shadow horizontal offsetnumber | (datum, index, data) => number0
titleShadowOffsetYChart title shadow vertical offsetnumber | (datum, index, data) => number0
titleCursorChart title mouse cursor stylestring | (datum, index, data) => stringdefault
titleDxChart title horizontal offsetnumber | (datum, index, data) => number0
titleDyChart title vertical offsetnumber | (datum, index, data) => number0

subtitle

The chart subtitle, which can be customized with the following configurations for various subtitle styles.

PropertyDescriptionTypeDefault Value
subtitleChart subtitle text contentstring | (datum, index, data) => string-
subtitleFontSizeChart subtitle font sizenumber | (datum, index, data) => number12
subtitleFontFamilyChart subtitle font familystring | (datum, index, data) => stringsans-serif
subtitleFontWeightChart subtitle font weightnumber | (datum, index, data) => numbernormal
subtitleLineHeightChart subtitle line heightnumber | (datum, index, data) => number12
subtitleTextAlignChart subtitle horizontal content alignmentstring | (datum, index, data) => stringcenter
subtitleTextBaselineChart subtitle vertical baselinestring | (datum, index, data) => stringmiddle
subtitleFillChart subtitle fill colorstring | (datum, index, data) => string#666
subtitleFillOpacityChart subtitle fill opacitynumber | (datum, index, data) => number1
subtitleStrokeChart subtitle stroke colorstring | (datum, index, data) => stringtransparent
subtitleStrokeOpacityChart subtitle stroke opacitynumber | (datum, index, data) => number1
subtitleLineWidthChart subtitle stroke widthnumber | (datum, index, data) => number0
subtitleLineDashChart subtitle dash stylenumber[] | (datum, index, data) => number[][]
subtitleOpacityChart subtitle overall opacitynumber | (datum, index, data) => number1
subtitleShadowColorChart subtitle shadow colorstring | (datum, index, data) => stringtransparent
subtitleShadowBlurChart subtitle shadow gaussian blur coefficientnumber | (datum, index, data) => number0
subtitleShadowOffsetXChart subtitle shadow horizontal offsetnumber | (datum, index, data) => number0
subtitleShadowOffsetYChart subtitle shadow vertical offsetnumber | (datum, index, data) => number0
subtitleCursorChart subtitle mouse cursor stylestring | (datum, index, data) => stringdefault
subtitleDxChart subtitle horizontal offsetnumber | (datum, index, data) => number0
subtitleDyChart subtitle vertical offsetnumber | (datum, index, data) => number0

Try it out: