logo

G6

  • 文档
  • API
  • 图表示例
  • 社区
  • 所有产品antv logo arrow
  • 5.0.49
  • 简介
  • 开始使用
    • 快速开始
    • 安装
    • 前端框架集成
      • 在 React 中使用
      • 在 Vue 中使用
      • 在 Angular 中使用
    • 详细教程
  • 图 Graph
    • Graph - 图
    • Options 配置项
    • 内置扩展
    • Extension - 扩展
  • 数据 Data
  • 元素 Element
    • 元素总览
    • 元素状态
    • 节点 Node
      • 节点总览
      • 节点通用配置项
      • 圆形节点 Circle
      • 菱形节点 Diamond
      • 甜甜圈节点 Donut
      • 椭圆形节点 Ellipse
      • 六边形节点 Hexagon
      • HTML节点 Html
      • 图片节点 Image
      • 矩形节点 Rect
      • 五角形节点 Star
      • 三角形节点 Triangle
      • 自定义节点
      • 使用 React 定义节点
    • 边 Edge
      • 边总览
      • 边通用配置项
      • 三次贝塞尔曲线边 Cubic
      • 水平三次贝塞尔曲线边 CubicHorizontal
      • 垂直三次贝塞尔曲线边 CubicVertical
      • 直线边 Line
      • 二次贝塞尔曲线边 Quadratic
      • 折线边 Polyline
      • 自定义边
    • 组合 Combo
      • 组合总览
      • 组合通用配置项
      • 圆形组合 Circle
      • 矩形组合 Rect
      • 自定义 Combo
    • 图形 Shape
      • 图形 Shape 与 KeyShape
      • 原子 Shape 以及其属性
      • 复合 Shape 的设计与实现
  • 布局 Layout
    • 布局总览
    • 布局通用配置项
    • AntV Dagre 布局 AntvDagre
    • 环形布局 Circular
    • 复合布局 ComboCombined
    • 紧凑树布局 CompactBox
    • 同心圆布局 Concentric
    • 3D D3力导向布局 D3Force3D
    • D3力导向布局 D3Force
    • Dagre 布局
    • 生态树 Dendrogram
    • 鱼骨布局 Fishbone
    • ForceAtlas2 力导向布局
    • Force 力导向布局
    • Fruchterman 力导向布局
    • 网格布局 Grid
    • 缩进树 Indented
    • 高维数据降维布局 MDS
    • 脑图树 Mindmap
    • 径向布局 Radial
    • 随机布局 Random
    • 蛇形布局 Snake
    • 自定义布局
  • 交互 Behavior
    • 交互总览
    • 缩放画布 ZoomCanvas
    • 标签自适应显示 AutoAdaptLabel
    • 框选 BrushSelect
    • 点击选中 ClickSelect
    • 展开/收起元素 CollapseExpand
    • 创建边 CreateEdge
    • 拖拽画布 DragCanvas
    • 拖拽元素 DragElement
    • 力导向拖拽元素 DragElementForce
    • 缩放画布时固定元素大小 FixElementSize
    • 聚焦元素 FocusElement
    • 悬停激活 HoverActivate
    • 套索选择 LassoSelect
    • 优化视口变换 OptimizeViewportTransform
    • 滚动画布 ScrollCanvas
    • 自定义交互
  • 插件 Plugin
    • 插件总览
    • 背景 Background
    • 气泡集 BubbleSets
    • 上下文菜单 Contextmenu
    • 边绑定 EdgeBundling
    • 边过滤镜 EdgeFilterLens
    • 鱼眼放大镜 Fisheye
    • 全屏展示 Fullscreen
    • 网格线 GridLine
    • 历史记录 History
    • 轮廓包围 Hull
    • 图例 Legend
    • 小地图 Minimap
    • 对齐线 Snapline
    • 时间条 Timebar
    • 工具栏 Toolbar
    • 提示框 Tooltip
    • 水印 Watermark
    • 自定义插件
  • 数据处理 Transform
    • 数据处理总览
    • 动态调整节点大小 MapNodeSize
    • 径向标签 PlaceRadialLabels
    • 平行边 ProcessParallelEdges
    • 自定义数据处理
  • 主题 Theme
    • 主题总览
    • 自定义主题
    • 色板
    • 自定义色板
  • 动画 Animation
    • 动画总览
    • 自定义动画
  • 扩展阅读
    • 事件
    • 渲染器
    • 坐标系
    • 下载图片
    • 使用 iconfont
    • 使用 3D
    • 项目打包
  • 版本特性
    • 新版本特性
    • 升级到 5.0
  • 常见问题
  • 参与贡献

主题总览

上一篇
自定义数据处理
下一篇
自定义主题

资源

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

概述

G6 中的主题是 Graph Options 的子集,它包含了关于画布和元素样式的配置。多主题可以帮助你快速地切换不同的图样式。

主题结构

一个主题由以下四个部分组成:

  1. 画布背景色 (background)

    • 控制整个画布的背景颜色
  2. 节点配置 (node)

    • 基础样式:填充色、描边、标签等静态视觉属性
    • 调色板:用于节点分组的颜色配置
    • 状态样式:不同状态下的样式配置(选中、激活、禁用等)
    • 动画配置:节点的动画效果配置
  3. 边配置 (edge)

    • 基础样式:线条样式、箭头、标签等静态视觉属性
    • 调色板:用于边分组的颜色配置
    • 状态样式:不同状态下的样式配置
    • 动画配置:边的动画效果配置
  4. Combo 配置 (combo)

    • 基础样式:填充、描边、折叠按钮等静态视觉属性
    • 状态样式:不同状态下的样式配置
    • 动画配置:Combo 的动画效果配置

注意:主题中的样式配置仅支持静态值,不支持回调函数形式的动态配置。如需动态样式,请使用图的配置项。

内置主题

G6 默认提供两种内置主题:

亮色主题(默认)

亮色主题
查看亮色主题完整配置项
const lightTheme = {
background: '#ffffff',
node: {
palette: {
type: 'group',
color: [
'#1783FF',
'#00C9C9',
'#F08F56',
'#D580FF',
'#7863FF',
'#DB9D0D',
'#60C42D',
'#FF80CA',
'#2491B3',
'#17C76F',
],
},
style: {
donutOpacity: 1,
badgeBackgroundOpacity: 1,
badgeFill: '#fff',
badgeFontSize: 8,
badgePadding: [0, 4],
badgePalette: ['#7E92B5', '#F4664A', '#FFBE3A'],
fill: '#1783ff',
fillOpacity: 1,
halo: false,
iconFill: '#fff',
iconOpacity: 1,
labelBackground: false,
labelBackgroundFill: '#ffffff',
labelBackgroundLineWidth: 0,
labelBackgroundOpacity: 0.75,
labelFill: '#000000',
labelFillOpacity: 0.85,
labelLineHeight: 16,
labelPadding: [0, 2],
labelFontSize: 12,
labelFontWeight: 400,
labelOpacity: 1,
labelOffsetY: 2,
lineWidth: 0,
portFill: '#1783ff',
portLineWidth: 1,
portStroke: '#000000',
portStrokeOpacity: 0.65,
size: 32,
stroke: '#000000',
strokeOpacity: 1,
zIndex: 2,
},
state: {
selected: {
halo: true,
haloLineWidth: 24,
haloStrokeOpacity: 0.25,
labelFontSize: 12,
labelFontWeight: 'bold',
lineWidth: 4,
stroke: '#000000',
},
active: {
halo: true,
haloLineWidth: 12,
haloStrokeOpacity: 0.15,
},
highlight: {
labelFontWeight: 'bold',
lineWidth: 4,
stroke: '#000000',
strokeOpacity: 0.85,
},
inactive: {
badgeBackgroundOpacity: 0.25,
donutOpacity: 0.25,
fillOpacity: 0.25,
iconOpacity: 0.85,
labelFill: '#000000',
labelFillOpacity: 0.25,
strokeOpacity: 0.25,
},
disabled: {
badgeBackgroundOpacity: 0.25,
donutOpacity: 0.06,
fill: '#1B324F',
fillOpacity: 0.06,
iconFill: '#1B324F',
iconOpacity: 0.25,
labelFill: '#000000',
labelFillOpacity: 0.25,
strokeOpacity: 0.06,
},
},
animation: {
enter: 'fade',
exit: 'fade',
show: 'fade',
hide: 'fade',
expand: 'node-expand',
collapse: 'node-collapse',
update: [{ fields: ['x', 'y', 'fill', 'stroke'] }],
translate: [{ fields: ['x', 'y'] }],
},
},
edge: {
palette: {
type: 'group',
color: [
'#99ADD1',
'#1783FF',
'#00C9C9',
'#F08F56',
'#D580FF',
'#7863FF',
'#DB9D0D',
'#60C42D',
'#FF80CA',
'#2491B3',
'#17C76F',
],
},
style: {
badgeBackgroundFill: '#99ADD1',
badgeFill: '#fff',
badgeFontSize: 8,
badgeOffsetX: 10,
fillOpacity: 1,
halo: false,
haloLineWidth: 12,
haloStrokeOpacity: 1,
increasedLineWidthForHitTesting: 2,
labelBackground: false,
labelBackgroundFill: '#ffffff',
labelBackgroundLineWidth: 0,
labelBackgroundOpacity: 0.75,
labelBackgroundPadding: [4, 4, 4, 4],
labelFill: '#000000',
labelFontSize: 12,
labelFontWeight: 400,
labelOpacity: 1,
labelPlacement: 'center',
labelTextBaseline: 'middle',
lineWidth: 1,
stroke: '#99ADD1',
strokeOpacity: 1,
zIndex: 1,
},
state: {
selected: {
halo: true,
haloStrokeOpacity: 0.25,
labelFontSize: 14,
labelFontWeight: 'bold',
lineWidth: 3,
},
active: {
halo: true,
haloStrokeOpacity: 0.15,
},
highlight: {
labelFontWeight: 'bold',
lineWidth: 3,
},
inactive: {
stroke: '#1B324F',
fillOpacity: 0.08,
labelOpacity: 0.25,
strokeOpacity: 0.08,
badgeBackgroundOpacity: 0.25,
},
disabled: {
stroke: '#d9d9d9',
fillOpacity: 0.45,
strokeOpacity: 0.45,
labelOpacity: 0.25,
badgeBackgroundOpacity: 0.45,
},
},
animation: {
enter: 'fade',
exit: 'fade',
expand: 'path-in',
collapse: 'path-out',
show: 'fade',
hide: 'fade',
update: [{ fields: ['sourceNode', 'targetNode'] }, { fields: ['stroke'], shape: 'key' }],
translate: [{ fields: ['sourceNode', 'targetNode'] }],
},
},
combo: {
style: {
collapsedMarkerFill: '#ffffff',
collapsedMarkerFontSize: 12,
collapsedMarkerFillOpacity: 1,
collapsedSize: 32,
collapsedFillOpacity: 1,
fill: '#99ADD1',
halo: false,
haloLineWidth: 12,
haloStroke: '#99ADD1',
haloStrokeOpacity: 0.25,
labelBackground: false,
labelBackgroundFill: '#ffffff',
labelBackgroundLineWidth: 0,
labelBackgroundOpacity: 0.75,
labelBackgroundPadding: [2, 4, 2, 4],
labelFill: '#000000',
labelFontSize: 12,
labelFontWeight: 400,
labelOpacity: 1,
lineDash: 0,
lineWidth: 1,
fillOpacity: 0.04,
strokeOpacity: 1,
padding: 10,
stroke: '#99ADD1',
},
state: {
selected: {
halo: true,
labelFontSize: 14,
labelFontWeight: 700,
lineWidth: 4,
},
active: {
halo: true,
},
highlight: {
labelFontWeight: 700,
lineWidth: 4,
},
inactive: {
fillOpacity: 0.65,
labelOpacity: 0.25,
strokeOpacity: 0.65,
},
disabled: {
fill: '#d9d9d9',
fillOpacity: 0.25,
labelOpacity: 0.25,
stroke: '#d9d9d9',
strokeOpacity: 0.25,
},
},
animation: {
enter: 'fade',
exit: 'fade',
show: 'fade',
hide: 'fade',
expand: 'combo-expand',
collapse: 'combo-collapse',
update: [{ fields: ['x', 'y'] }, { fields: ['fill', 'stroke', 'lineWidth'], shape: 'key' }],
translate: [{ fields: ['x', 'y'] }],
},
},
};

暗色主题

暗色主题
查看暗色主题完整配置项
const darkTheme = {
background: '#000000',
node: {
palette: {
type: 'group',
color: [
'#1783FF',
'#00C9C9',
'#F08F56',
'#D580FF',
'#7863FF',
'#DB9D0D',
'#60C42D',
'#FF80CA',
'#2491B3',
'#17C76F',
],
},
style: {
donutOpacity: 1,
badgeBackgroundOpacity: 1,
badgeFill: '#fff',
badgeFontSize: 8,
badgePadding: [0, 4],
badgePalette: ['#7E92B5', '#F4664A', '#FFBE3A'],
fill: '#1783ff',
fillOpacity: 1,
halo: false,
iconFill: '#fff',
iconOpacity: 1,
labelBackground: false,
labelBackgroundFill: '#000000',
labelBackgroundLineWidth: 0,
labelBackgroundOpacity: 0.75,
labelFill: '#ffffff',
labelFillOpacity: 0.85,
labelLineHeight: 16,
labelPadding: [0, 2],
labelFontSize: 12,
labelFontWeight: 400,
labelOpacity: 1,
labelOffsetY: 2,
lineWidth: 0,
portFill: '#1783ff',
portLineWidth: 1,
portStroke: '#d0e4ff',
portStrokeOpacity: 0.65,
size: 32,
stroke: '#d0e4ff',
strokeOpacity: 1,
zIndex: 2,
},
state: {
selected: {
halo: true,
haloLineWidth: 24,
haloStrokeOpacity: 0.45,
labelFontSize: 12,
labelFontWeight: 'bold',
lineWidth: 4,
stroke: '#d0e4ff',
},
active: {
halo: true,
haloLineWidth: 12,
haloStrokeOpacity: 0.25,
},
highlight: {
labelFontWeight: 'bold',
lineWidth: 4,
stroke: '#d0e4ff',
strokeOpacity: 0.85,
},
inactive: {
badgeBackgroundOpacity: 0.45,
donutOpacity: 0.45,
fillOpacity: 0.45,
iconOpacity: 0.45,
labelFill: '#ffffff',
labelFillOpacity: 0.45,
strokeOpacity: 0.45,
},
disabled: {
badgeBackgroundOpacity: 0.25,
donutOpacity: 0.25,
fill: '#D0E4FF',
fillOpacity: 0.25,
iconFill: '#D0E4FF',
iconOpacity: 0.25,
labelFill: '#ffffff',
labelFillOpacity: 0.25,
strokeOpacity: 0.25,
},
},
animation: {
enter: 'fade',
exit: 'fade',
show: 'fade',
hide: 'fade',
expand: 'node-expand',
collapse: 'node-collapse',
update: [{ fields: ['x', 'y', 'fill', 'stroke'] }],
translate: [{ fields: ['x', 'y'] }],
},
},
edge: {
palette: {
type: 'group',
color: [
'#637088',
'#0F55A6',
'#008383',
'#9C5D38',
'#8B53A6',
'#4E40A6',
'#8F6608',
'#3E801D',
'#A65383',
'#175E75',
'#0F8248',
],
},
style: {
badgeBackgroundFill: '#637088',
badgeFill: '#fff',
badgeFontSize: 8,
badgeOffsetX: 10,
fillOpacity: 1,
halo: false,
haloLineWidth: 12,
haloStrokeOpacity: 1,
increasedLineWidthForHitTesting: 2,
labelBackground: false,
labelBackgroundFill: '#000000',
labelBackgroundLineWidth: 0,
labelBackgroundOpacity: 0.75,
labelBackgroundPadding: [4, 4, 4, 4],
labelFill: '#ffffff',
labelFontSize: 12,
labelFontWeight: 400,
labelOpacity: 1,
labelPlacement: 'center',
labelTextBaseline: 'middle',
lineWidth: 1,
stroke: '#637088',
strokeOpacity: 1,
zIndex: 1,
},
state: {
selected: {
halo: true,
haloStrokeOpacity: 0.25,
labelFontSize: 14,
labelFontWeight: 'bold',
lineWidth: 3,
},
active: {
halo: true,
haloStrokeOpacity: 0.15,
},
highlight: {
labelFontWeight: 'bold',
lineWidth: 3,
},
inactive: {
stroke: '#D0E4FF',
fillOpacity: 0.08,
labelOpacity: 0.25,
strokeOpacity: 0.08,
badgeBackgroundOpacity: 0.25,
},
disabled: {
stroke: '#637088',
fillOpacity: 0.45,
strokeOpacity: 0.45,
labelOpacity: 0.25,
badgeBackgroundOpacity: 0.45,
},
},
animation: {
enter: 'fade',
exit: 'fade',
expand: 'path-in',
collapse: 'path-out',
show: 'fade',
hide: 'fade',
update: [{ fields: ['sourceNode', 'targetNode'] }, { fields: ['stroke'], shape: 'key' }],
translate: [{ fields: ['sourceNode', 'targetNode'] }],
},
},
combo: {
style: {
collapsedMarkerFill: '#000000',
collapsedMarkerFontSize: 12,
collapsedMarkerFillOpacity: 1,
collapsedSize: 32,
collapsedFillOpacity: 1,
fill: '#fdfdfd',
halo: false,
haloLineWidth: 12,
haloStroke: '#99add1',
haloStrokeOpacity: 0.25,
labelBackground: false,
labelBackgroundFill: '#000000',
labelBackgroundLineWidth: 0,
labelBackgroundOpacity: 0.75,
labelBackgroundPadding: [2, 4, 2, 4],
labelFill: '#ffffff',
labelFontSize: 12,
labelFontWeight: 400,
labelOpacity: 1,
lineDash: 0,
lineWidth: 1,
fillOpacity: 0.04,
strokeOpacity: 1,
padding: 10,
stroke: '#99add1',
},
state: {
selected: {
halo: true,
labelFontSize: 14,
labelFontWeight: 700,
lineWidth: 4,
},
active: {
halo: true,
},
highlight: {
labelFontWeight: 700,
lineWidth: 4,
},
inactive: {
fillOpacity: 0.65,
labelOpacity: 0.25,
strokeOpacity: 0.65,
},
disabled: {
fill: '#d0e4ff',
fillOpacity: 0.25,
labelOpacity: 0.25,
stroke: '#969696',
strokeOpacity: 0.25,
},
},
animation: {
enter: 'fade',
exit: 'fade',
show: 'fade',
hide: 'fade',
expand: 'combo-expand',
collapse: 'combo-collapse',
update: [{ fields: ['x', 'y'] }, { fields: ['fill', 'stroke', 'lineWidth'], shape: 'key' }],
translate: [{ fields: ['x', 'y'] }],
},
},
};

使用主题

配置主题

在创建图时通过 theme 选项指定要使用的主题:

const graph = new Graph({
theme: 'light', // 或 'dark'
// ... 其他配置
});

切换主题

创建图后,可以通过 setTheme 方法动态切换主题:

// 切换到暗色主题
graph.setTheme('dark');
// 获取当前主题
const currentTheme = graph.getTheme(); // 'dark'

样式优先级

在 G6 中,元素的最终样式由多个层级的样式合并而成,按优先级从低到高排序:

⭐️ 主题默认样式 < 调色板样式 < 数据样式 < 图的默认样式 < ⭐️ 主题状态样式 < 图的状态样式

详细说明:

  1. 主题默认样式:主题系统提供的基础样式
  2. 调色板样式:基于主题调色板配置的自动着色样式
  3. 数据样式:在数据中定义的样式
  4. 图的默认样式:通过图的配置项设置的样式
  5. 主题状态样式:主题中定义的状态样式
  6. 图的状态样式:通过图的配置项设置的状态样式

更多关于自定义主题的内容,请参考自定义主题。