logo

G2

  • 文档
  • 图表介绍
  • API
  • 图表示例
  • 主题
  • 周边生态
  • 所有产品antv logo arrow
  • 5.3.3
  • 快速上手
  • 简介
    • 什么是 G2
    • 在前端框架中使用
    • Spec 和 API
  • 核心概念
    • 图表(Chart)
      • G2 图表组成
      • 如何使用图表
    • 标记(Mark)
      • 概览
      • 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)
      • 概览
      • custom
      • ema
      • fetch
      • filter
      • fold
      • inline
      • join
      • kde
      • log
      • map
      • pick
      • rename
      • slice
      • sort
      • sortBy
    • 编码(Encode)
    • 比例尺(Scale)
      • 概览
      • band
      • linear
      • log
      • ordinal
      • point
      • quantile
      • quantize
      • sqrt
      • threshold
      • time
      • pow
    • 转换(Transform)
      • 概览
      • 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)
      • 概览
      • fisheye
      • parallel
      • polar
      • theta
      • transpose
      • radial
      • cartesian3D
      • helix
    • 样式(Style)
    • 动画(Animate)
      • 概览
      • fadeIn
      • fadeOut
      • growInX
      • growInY
      • morphing
      • pathIn
      • scaleInX
      • scaleInY
      • scaleOutX
      • scaleOutY
      • waveIn
      • zoomIn
      • zoomOut
    • 状态(State)
    • 交互(Interaction)
      • 概览
      • brushAxisHighlight
      • brushHighlight
      • brushXHighlight
      • brushYHighlight
      • brushFilter
      • brushXFilter
      • brushYFilter
      • chartIndex
      • elementHighlight
      • elementHighlightByColor
      • elementHighlightByX
      • elementSelect
      • elementSelectByColor
      • elementSelectByX
      • fisheye
      • legendFilter
      • legendHighlight
      • poptip
      • scrollbarFilter
      • sliderFilter
    • 复合(Composition)
      • 概览
      • facetCircle
      • facetRect
      • repeatMatrix
      • spaceFlex
      • spaceLayer
      • timingKeyframe
    • 主题(Theme)
      • 概览
      • academy
      • classic
      • classicDark
    • 事件(Event)
    • 颜色映射(Color)
  • 图表 API
  • 图表组件
    • 标题(Title)
    • 坐标轴(Axis)
    • 图例(Legend)
    • 滚动条(Scrollbar)
    • 缩略轴(Slider)
    • 提示信息(Tooltip)
    • 数据标签(Label)
  • 进阶主题
    • 关系图(Graph)
      • forceGraph
      • pack
      • sankey
      • tree
      • treemap
    • 地图(Geo)
      • geoPath
      • geoView
    • 3D 图表(3D Chart)
      • 绘制 3D 图表
      • point3D
      • line3D
      • interval3D
      • surface3D
    • 插件扩展(Plugin)
      • renderer
      • rough
      • lottie
      • a11y
    • 按需打包
    • 设置纹理
    • 服务端渲染(SSR)
    • Spec 函数表达式支持 (5.3.0 支持)
  • 版本特性
    • 新版本特性
    • v4 升级 v5 指南
  • 常见问题 FAQ

概览

上一篇
timingKeyframe
下一篇
academy

Resources

Ant Design
Galacea Effects
Umi-React 应用开发框架
Dumi-组件/文档研发工具
ahooks-React Hooks 库

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
xtech蚂蚁体验科技
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

概述

G2 中 主题(Theme) 是一组预定义的样式配置,用于控制图表的整体外观,包括颜色、字体、边距等视觉属性。主题系统使得图表能够保持一致的视觉风格,同时提供了灵活的自定义能力,以满足不同场景下的设计需求。

主题在 G2 中的作用主要体现在以下几个方面:

  1. 统一视觉风格:为图表提供一致的视觉语言,使得多个图表在同一应用中能够保持风格统一
  2. 适应不同场景:提供明暗两种基础主题,适应不同的应用背景和使用环境
  3. 简化配置:通过预设的样式配置,减少重复的样式定义工作
  4. 增强可读性:通过精心设计的颜色方案和布局,提升图表的可读性和美观度

G2 的主题系统支持多层级配置,可以在视图层级或标记层级进行设置,并且支持局部覆盖和完全自定义。

主题层级

G2 的主题可以在不同层级进行配置:

  1. 视图层级:应用于整个图表或视图

    chart.options({
    theme: { type: 'dark' },
    });
    // 或使用API形式
    chart.theme({ type: 'dark' });
  2. 标记层级:应用于特定的图形标记

    chart.options({
    type: 'interval',
    theme: { color: 'steelblue' },
    });
    // 或使用API形式
    chart.interval().theme({ color: 'steelblue' });

当同时存在多个层级的主题配置时,标记层级的配置会覆盖视图层级的配置。

内置主题

G2 内置了多种主题,可以通过 type 属性进行切换:

主题名称描述适用场景
light默认亮色主题适用于浅色背景的应用界面
dark暗色主题适用于深色背景的应用界面
classic经典主题基于亮色主题的变体,使用经典配色方案
classicDark经典暗色主题基于暗色主题的变体,使用经典配色方案
academy学术主题适用于学术论文、报告等场景

使用内置主题的方式如下:

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

配置项

主题配置项可以分为基础配置、视图配置、组件配置和标记配置四个部分。

属性描述类型默认值必选
type指定使用的主题类型'light' | 'dark' | 'classic' | 'classicDark' | 'academy''light'
padding图表内边距'auto' | number'auto'
margin图表外边距number16
inset图表内部图形与坐标轴的间距'auto' | number'auto'
color默认颜色string取决于主题
size默认大小number1
category10分类颜色方案(10 色)string | string[]取决于主题
category20分类颜色方案(20 色)string | string[]取决于主题
view视图区域配置view-
enter入场动画配置animation-
update更新动画配置animation-
exit退场动画配置animation-

view

视图区域配置项。

属性描述类型默认值必选
viewFill整个视图区域的填充色string'transparent'
plotFill绘图区域的填充色string'transparent'
mainFill主区域的填充色string'transparent'
contentFill内容区域的填充色string'transparent'

animation

动画配置项。

属性描述类型默认值必选
duration动画持续时间(毫秒)number300
fill动画填充模式'none' | 'forwards' | 'backwards' | 'both''both'
delay动画延迟时间(毫秒)number0

自定义主题

G2 提供了两种自定义主题的方式:局部覆盖和完全自定义。

局部覆盖

最简单的自定义方式是在使用主题时覆盖部分配置项:

// Spec形式
chart.options({
theme: {
type: 'light', // 基于light主题
color: 'steelblue', // 覆盖默认颜色
margin: 20, // 覆盖默认外边距
},
});
// API形式
chart.theme({
type: 'dark', // 基于dark主题
category10: ['#ff0000', '#00ff00', '#0000ff'], // 自定义颜色方案
});

这种方式适合只需要调整少量样式的场景,简单直接。

完全自定义

对于需要完全控制主题的场景,可以创建自定义主题函数,然后注册并使用:

// 1. 定义主题函数
function CustomTheme() {
// 可以基于现有主题进行修改
const light = G2.Light();
// 返回修改后的主题配置
return {
...light,
color: '#3366cc',
category10: [
'#3366cc',
'#dc3912',
'#ff9900',
'#109618',
'#990099',
'#0099c6',
'#dd4477',
'#66aa00',
'#b82e2e',
'#316395',
],
// 其他自定义配置...
};
}
// 2. 注册主题
G2.register('theme.custom', CustomTheme);
// 3. 使用自定义主题
chart.options({
theme: { type: 'custom' },
});

这种方式适合需要全面控制主题样式的场景,提供了最大的灵活性。

示例

使用内置主题

下面的示例展示了如何使用内置的暗色主题:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
theme: {
type: 'dark',
view: {
viewFill: '#1f1f1f', // 设置暗色背景
},
},
type: 'interval',
data: [
{ genre: '运动', sold: 275 },
{ genre: '策略', sold: 115 },
{ genre: '角色扮演', sold: 120 },
{ genre: '动作', sold: 350 },
{ genre: '模拟', sold: 150 },
],
encode: {
x: 'genre',
y: 'sold',
color: 'genre',
},
});
chart.render();

自定义颜色

下面的示例展示了如何自定义默认颜色:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
theme: { color: '#8a2be2' }, // 设置默认颜色为紫色
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();

自定义颜色方案

下面的示例展示了如何自定义分类颜色方案:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
theme: {
// 自定义分类颜色方案
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();

自定义视图区域样式

下面的示例展示了如何自定义视图区域的样式:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
theme: {
view: {
viewFill: '#f5f5f5', // 设置视图背景色
plotFill: '#ffffff', // 设置绘图区背景色
},
},
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();

更多主题相关的示例,可以查看图表示例 - 主题页面。