logo

G2

  • 图表介绍
  • 文档
  • 图表示例
  • 主题
  • 周边生态
  • 所有产品antv logo arrow
  • 5.3.4
  • 快速上手
  • 简介
    • 什么是 G2
    • 在前端框架中使用
    • Spec 和 API
  • 图表 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
      • connector
      • 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)
  • 图表组件
    • 标题(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

classicDark

上一篇
classic
下一篇
事件(Event)

资源

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

经典暗黑主题。在使用暗黑主题时,通常需要配置一些额外的视图样式来确保图表在暗色背景下有更好的视觉效果。

配置视图背景

import { Chart } from '@antv/g2';
const chart = new Chart({ container: 'container' });
chart.options({
type: 'interval',
theme: { type: 'classicDark', view: { viewFill: '#141414' } },
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();

使用 viewStyle 配置更详细的视图样式

import { Chart } from '@antv/g2';
const chart = new Chart({ container: 'container' });
chart.options({
type: 'interval',
theme: 'classicDark',
data: [
{ category: 'A', value: 23 },
{ category: 'B', value: 45 },
{ category: 'C', value: 56 },
{ category: 'D', value: 78 },
{ category: 'E', value: 32 },
],
encode: { x: 'category', y: 'value', color: 'category' },
axis: {
x: { grid: true, gridStroke: '#404040' },
y: { grid: true, gridStroke: '#404040' },
},
viewStyle: {
viewFill: '#1f1f1f',
plotFill: '#2a2a2a',
plotStroke: '#404040',
plotLineWidth: 1,
},
});
chart.render();

完整的暗黑主题配置示例

import { Chart } from '@antv/g2';
const chart = new Chart({ container: 'container' });
chart.options({
type: 'line',
theme: {
type: 'classicDark',
view: { viewFill: '#0f0f0f', plotFill: '#1a1a1a' },
},
data: [
{ year: '2018', value: 30 },
{ year: '2019', value: 40 },
{ year: '2020', value: 35 },
{ year: '2021', value: 50 },
{ year: '2022', value: 49 },
{ year: '2023', value: 70 },
],
encode: { x: 'year', y: 'value' },
style: { stroke: '#60a5fa', lineWidth: 3 },
axis: {
x: {
grid: true,
gridStroke: '#fff',
gridLineWidth: 2,
labelFill: '#d1d5db',
},
y: {
grid: true,
gridStroke: '#fff',
gridLineWidth: 2,
labelFill: '#d1d5db',
},
},
});
chart.render();

与容器背景色协调

如果你的网页或应用使用了暗色主题,建议确保图表的背景色与容器背景色协调:

import { Chart } from '@antv/g2';
// 模拟暗色容器背景
const container = document.getElementById('container');
if (container) {
container.style.backgroundColor = '#111827';
container.style.padding = '20px';
container.style.borderRadius = '8px';
}
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'area',
theme: { type: 'classicDark', view: { viewFill: '#111827' } },
data: [
{ month: 'Jan', sales: 120 },
{ month: 'Feb', sales: 140 },
{ month: 'Mar', sales: 110 },
{ month: 'Apr', sales: 180 },
{ month: 'May', sales: 160 },
{ month: 'Jun', sales: 200 },
],
encode: { x: 'month', y: 'sales' },
style: {
fill: 'linear-gradient(to top, rgba(59, 130, 246, 0.3), rgba(59, 130, 246, 0.1))',
stroke: '#3b82f6',
lineWidth: 2,
},
});
chart.render();