Loading...
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:
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.
G2 themes can be configured at different levels:
View Level: Applied to the entire chart or view
chart.options({theme: { type: 'dark' },});// Or use API formchart.theme({ type: 'dark' });
Mark Level: Applied to specific graphic marks
chart.options({type: 'interval',theme: { color: 'steelblue' },});// Or use API formchart.interval().theme({ color: 'steelblue' });
When multiple levels of theme configuration exist simultaneously, mark-level configuration will override view-level configuration.
G2 has multiple built-in themes that can be switched using the type
property:
Theme Name | Description | Applicable Scenarios |
---|---|---|
light | Default light theme | Suitable for application interfaces with light backgrounds |
dark | Dark theme | Suitable for application interfaces with dark backgrounds |
classic | Classic theme | A variant based on light theme with classic color scheme |
classicDark | Classic dark theme | A variant based on dark theme with classic color scheme |
academy | Academic theme | Suitable for academic papers, reports and other scenarios |
How to use built-in themes:
// Spec formchart.options({theme: { type: 'dark' },});// API formchart.theme({ type: 'classicDark' });
Theme configuration options can be divided into four parts: basic configuration, view configuration, component configuration, and mark configuration.
Property | Description | Type | Default | Required |
---|---|---|---|---|
type | Specifies the theme type to use | 'light' | 'dark' | 'classic' | 'classicDark' | 'academy' | 'light' | |
padding | Chart inner padding | 'auto' | number | 'auto' | |
margin | Chart outer margin | number | 16 | |
inset | Spacing between chart graphics and axis | 'auto' | number | 'auto' | |
color | Default color | string | Theme dependent | |
size | Default size | number | 1 | |
category10 | Categorical color scheme (10 colors) | string | string[] | Theme dependent | |
category20 | Categorical color scheme (20 colors) | string | string[] | Theme dependent | |
view | View area configuration | view | - | |
enter | Enter animation configuration | animation | - | |
update | Update animation configuration | animation | - | |
exit | Exit animation configuration | animation | - |
View area configuration options.
Property | Description | Type | Default | Required |
---|---|---|---|---|
viewFill | Fill color of entire view area | string | 'transparent' | |
plotFill | Fill color of plot area | string | 'transparent' | |
mainFill | Fill color of main area | string | 'transparent' | |
contentFill | Fill color of content area | string | 'transparent' |
Animation configuration options.
Property | Description | Type | Default | Required |
---|---|---|---|---|
duration | Animation duration (milliseconds) | number | 300 | |
fill | Animation fill mode | 'none' | 'forwards' | 'backwards' | 'both' | 'both' | |
delay | Animation delay time (milliseconds) | number | 0 |
G2 provides two ways to customize themes: partial override and complete customization.
The simplest customization approach is to override some configuration options when using a theme:
// Spec formchart.options({theme: {type: 'light', // Based on light themecolor: 'steelblue', // Override default colormargin: 20, // Override default margin},});// API formchart.theme({type: 'dark', // Based on dark themecategory10: ['#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.
For scenarios that require complete control over themes, you can create custom theme functions, then register and use them:
// 1. Define theme functionfunction CustomTheme() {// Can modify based on existing themesconst light = G2.Light();// Return modified theme configurationreturn {...light,color: '#3366cc',category10: ['#3366cc','#dc3912','#ff9900','#109618','#990099','#0099c6','#dd4477','#66aa00','#b82e2e','#316395',],// Other custom configurations...};}// 2. Register themeG2.register('theme.custom', CustomTheme);// 3. Use custom themechart.options({theme: { type: 'custom' },});
This approach is suitable for scenarios that require comprehensive control over theme styles, providing maximum flexibility.
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();
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 purpletype: '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();
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 schemecategory10: ['#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();
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 colorplotFill: '#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.