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)

Overview

Previous
timingKeyframe
Next
academy

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

Introduction

In G2, Theme is a set of predefined style configurations used to control the overall appearance of charts, including visual attributes such as colors, fonts, and margins. The theme system enables charts to maintain consistent visual style while providing flexible customization capabilities to meet design requirements in different scenarios.

The role of themes in G2 is mainly reflected in the following aspects:

  1. Unified Visual Style: Provides consistent visual language for charts, ensuring that multiple charts within the same application maintain style consistency
  2. Adaptation to Different Scenarios: Provides light and dark basic themes to adapt to different application backgrounds and usage environments
  3. Simplified Configuration: Reduces repetitive style definition work through preset style configurations
  4. Enhanced Readability: Improves chart readability and aesthetics through carefully designed color schemes and layouts

G2's theme system supports multi-level configuration, can be set at the view level or mark level, and supports partial overrides and complete customization.

Theme Hierarchy

G2 themes can be configured at different levels:

  1. View Level: Applied to the entire chart or view

    chart.options({
    theme: { type: 'dark' },
    });
    // Or use API form
    chart.theme({ type: 'dark' });
  2. Mark Level: Applied to specific graphic marks

    chart.options({
    type: 'interval',
    theme: { color: 'steelblue' },
    });
    // Or use API form
    chart.interval().theme({ color: 'steelblue' });

When multiple levels of theme configuration exist simultaneously, mark-level configuration will override view-level configuration.

Built-in Themes

G2 has multiple built-in themes that can be switched using the type property:

Theme NameDescriptionApplicable Scenarios
lightDefault light themeSuitable for application interfaces with light backgrounds
darkDark themeSuitable for application interfaces with dark backgrounds
classicClassic themeA variant based on light theme with classic color scheme
classicDarkClassic dark themeA variant based on dark theme with classic color scheme
academyAcademic themeSuitable for academic papers, reports and other scenarios

How to use built-in themes:

// Spec form
chart.options({
theme: { type: 'dark' },
});
// API form
chart.theme({ type: 'classicDark' });

Configuration Options

Theme configuration options can be divided into four parts: basic configuration, view configuration, component configuration, and mark configuration.

PropertyDescriptionTypeDefaultRequired
typeSpecifies the theme type to use'light' | 'dark' | 'classic' | 'classicDark' | 'academy''light'
paddingChart inner padding'auto' | number'auto'
marginChart outer marginnumber16
insetSpacing between chart graphics and axis'auto' | number'auto'
colorDefault colorstringTheme dependent
sizeDefault sizenumber1
category10Categorical color scheme (10 colors)string | string[]Theme dependent
category20Categorical color scheme (20 colors)string | string[]Theme dependent
viewView area configurationview-
enterEnter animation configurationanimation-
updateUpdate animation configurationanimation-
exitExit animation configurationanimation-

view

View area configuration options.

PropertyDescriptionTypeDefaultRequired
viewFillFill color of entire view areastring'transparent'
plotFillFill color of plot areastring'transparent'
mainFillFill color of main areastring'transparent'
contentFillFill color of content areastring'transparent'

animation

Animation configuration options.

PropertyDescriptionTypeDefaultRequired
durationAnimation duration (milliseconds)number300
fillAnimation fill mode'none' | 'forwards' | 'backwards' | 'both''both'
delayAnimation delay time (milliseconds)number0

Custom Themes

G2 provides two ways to customize themes: partial override and complete customization.

Partial Override

The simplest customization approach is to override some configuration options when using a theme:

// Spec form
chart.options({
theme: {
type: 'light', // Based on light theme
color: 'steelblue', // Override default color
margin: 20, // Override default margin
},
});
// API form
chart.theme({
type: 'dark', // Based on dark theme
category10: ['#ff0000', '#00ff00', '#0000ff'], // Custom color scheme
});

This approach is suitable for scenarios where only a small amount of style adjustment is needed, simple and direct.

Complete Customization

For scenarios that require complete control over themes, you can create custom theme functions, then register and use them:

// 1. Define theme function
function CustomTheme() {
// Can modify based on existing themes
const light = G2.Light();
// Return modified theme configuration
return {
...light,
color: '#3366cc',
category10: [
'#3366cc',
'#dc3912',
'#ff9900',
'#109618',
'#990099',
'#0099c6',
'#dd4477',
'#66aa00',
'#b82e2e',
'#316395',
],
// Other custom configurations...
};
}
// 2. Register theme
G2.register('theme.custom', CustomTheme);
// 3. Use custom theme
chart.options({
theme: { type: 'custom' },
});

This approach is suitable for scenarios that require comprehensive control over theme styles, providing maximum flexibility.

Examples

Using Built-in Themes

The following example shows how to use the built-in dark theme:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
theme: {
type: 'dark',
view: {
viewFill: '#1f1f1f', // Set dark background
},
},
type: 'interval',
data: [
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'RPG', sold: 120 },
{ genre: 'Action', sold: 350 },
{ genre: 'Simulation', sold: 150 },
],
encode: {
x: 'genre',
y: 'sold',
color: 'genre',
},
});
chart.render();

Custom Color

The following example shows how to customize the default color:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
theme: { color: '#8a2be2' }, // Set default color to purple
type: 'line',
data: [
{ year: '2018', value: 30 },
{ year: '2019', value: 40 },
{ year: '2020', value: 45 },
{ year: '2021', value: 50 },
{ year: '2022', value: 56 },
{ year: '2023', value: 70 },
],
encode: {
x: 'year',
y: 'value',
},
});
chart.render();

Custom Color Scheme

The following example shows how to customize categorical color schemes:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
theme: {
// Custom categorical color scheme
category10: [
'#ff9999',
'#99ff99',
'#9999ff',
'#ffff99',
'#ff99ff',
'#99ffff',
'#ffcc99',
'#99ccff',
'#ff9966',
'#66ff99',
],
},
type: 'interval',
data: [
{ category: 'A', value: 10 },
{ category: 'B', value: 20 },
{ category: 'C', value: 15 },
{ category: 'D', value: 25 },
{ category: 'E', value: 18 },
],
encode: {
x: 'category',
y: 'value',
color: 'category',
},
});
chart.render();

Custom View Area Style

The following example shows how to customize view area styles:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
theme: {
view: {
viewFill: '#f5f5f5', // Set view background color
plotFill: '#ffffff', // Set plot area background color
},
},
type: 'point',
data: [
{ x: 1, y: 4 },
{ x: 2, y: 5 },
{ x: 3, y: 7 },
{ x: 4, y: 3 },
{ x: 5, y: 6 },
{ x: 6, y: 8 },
{ x: 7, y: 5 },
{ x: 8, y: 9 },
{ x: 9, y: 6 },
],
encode: {
x: 'x',
y: 'y',
shape: 'point',
size: 10,
},
});
chart.render();

For more theme-related examples, you can visit the Chart Examples - Theme page.