chart-theme.js
5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* ECharts 明暗配色(暗黑下轴线/标签/图例/tooltip 用浅色)
* @param {boolean} isDark
*/
export function getChartTheme(isDark) {
if (isDark) {
return {
text: 'rgba(255,255,255,0.85)',
textSecondary: 'rgba(255,255,255,0.65)',
textMuted: 'rgba(255,255,255,0.45)',
axisLine: 'rgba(255,255,255,0.15)',
splitLine: 'rgba(255,255,255,0.08)',
pieBorder: '#1f1f1f',
tooltipBg: 'rgba(31,31,31,0.96)',
tooltipBorder: 'rgba(255,255,255,0.12)',
barBg: 'rgba(255,255,255,0.08)',
radarSplitArea: ['rgba(255,255,255,0.04)', 'rgba(255,255,255,0.02)'],
empty: 'rgba(255,255,255,0.45)'
}
}
return {
text: '#303133',
textSecondary: '#606266',
textMuted: '#909399',
axisLine: '#E4E7ED',
splitLine: '#E4E7ED',
pieBorder: '#fff',
tooltipBg: 'rgba(255,255,255,0.95)',
tooltipBorder: '#E4E7ED',
barBg: '#F2F6FC',
radarSplitArea: ['#f5f7fa', '#fff'],
empty: '#909399'
}
}
/** Vuex slideClass 是否为 dark */
export function isSlideDark(vm) {
return !!(vm && vm.$store && vm.$store.state &&
vm.$store.state.settings && vm.$store.state.settings.slideClass === 'dark')
}
function mergeAxisTheme(axis, t, isDark) {
if (!axis) return axis
const applyOne = (a) => {
if (!a || typeof a !== 'object') return a
const next = { ...a }
const axisLabelColor = isDark
? t.textSecondary
: ((a.axisLabel && a.axisLabel.color) || t.textSecondary)
next.axisLabel = { ...(a.axisLabel || {}), color: axisLabelColor }
const axisLineColor = isDark
? t.axisLine
: (((a.axisLine && a.axisLine.lineStyle && a.axisLine.lineStyle.color) || t.axisLine))
next.axisLine = {
...(a.axisLine || {}),
lineStyle: { ...((a.axisLine && a.axisLine.lineStyle) || {}), color: axisLineColor }
}
if (a.splitLine !== false && !(a.splitLine && a.splitLine.show === false)) {
const splitLineColor = isDark
? t.splitLine
: (((a.splitLine && a.splitLine.lineStyle && a.splitLine.lineStyle.color) || t.splitLine))
next.splitLine = {
...(a.splitLine || {}),
lineStyle: { ...((a.splitLine && a.splitLine.lineStyle) || {}), color: splitLineColor }
}
}
if (a.name || a.nameTextStyle) {
const nameColor = isDark
? t.textMuted
: ((a.nameTextStyle && a.nameTextStyle.color) || t.textMuted)
next.nameTextStyle = { ...(a.nameTextStyle || {}), color: nameColor }
}
return next
}
return Array.isArray(axis) ? axis.map(applyOne) : applyOne(axis)
}
/**
* 把明暗主题注入到 ECharts option(轴/图例/tooltip/饼图边框与标签)
* @param {object} option
* @param {boolean} isDark
*/
export function applyChartTheme(option, isDark) {
if (!option) return option
const t = getChartTheme(isDark)
const next = { ...option, backgroundColor: 'transparent' }
next.textStyle = isDark
? { ...(option.textStyle || {}), color: t.text }
: { color: t.text, ...(option.textStyle || {}) }
const tooltipTextStyle = (option.tooltip && option.tooltip.textStyle) || {}
next.tooltip = {
...(option.tooltip || {}),
backgroundColor: t.tooltipBg,
borderColor: t.tooltipBorder,
textStyle: isDark
? { ...tooltipTextStyle, color: t.text }
: { color: t.text, ...tooltipTextStyle }
}
if (option.legend) {
const legendTextStyle = (option.legend && option.legend.textStyle) || {}
next.legend = {
...option.legend,
textStyle: isDark
? { ...legendTextStyle, color: t.textSecondary }
: { color: t.textSecondary, ...legendTextStyle }
}
}
if (option.title && typeof option.title === 'object') {
const titleTextStyle = (option.title && option.title.textStyle) || {}
next.title = {
...option.title,
textStyle: isDark
? { ...titleTextStyle, color: t.empty, fontSize: titleTextStyle.fontSize || 14 }
: { color: t.empty, fontSize: 14, ...titleTextStyle }
}
}
if (option.xAxis) next.xAxis = mergeAxisTheme(option.xAxis, t, isDark)
if (option.yAxis) next.yAxis = mergeAxisTheme(option.yAxis, t, isDark)
if (option.visualMap) {
const vm = option.visualMap
const applyVm = (v) => ({
...v,
textStyle: { color: t.textSecondary, ...(v.textStyle || {}) }
})
next.visualMap = Array.isArray(vm) ? vm.map(applyVm) : applyVm(vm)
}
if (Array.isArray(option.series)) {
next.series = option.series.map(s => {
if (!s || typeof s !== 'object') return s
const series = { ...s }
if (s.type === 'pie' || s.type === 'funnel') {
const border = (s.itemStyle && s.itemStyle.borderColor === '#fff') ? t.pieBorder
: ((s.itemStyle && s.itemStyle.borderColor) || t.pieBorder)
series.itemStyle = { ...(s.itemStyle || {}), borderColor: border }
if (s.label) {
const labelColor = isDark
? (s.type === 'funnel' && s.label.position === 'inside' ? '#fff' : t.textSecondary)
: (s.label.color || (s.type === 'funnel' && s.label.position === 'inside' ? '#fff' : t.textSecondary))
series.label = { ...s.label, color: labelColor }
}
if (s.labelLine && s.labelLine.lineStyle) {
series.labelLine = {
...s.labelLine,
lineStyle: { ...s.labelLine.lineStyle, color: t.axisLine }
}
}
}
if (s.type === 'heatmap' && s.label) {
series.label = { ...s.label, color: t.text }
}
return series
})
}
return next
}