forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecharts.ts
More file actions
247 lines (224 loc) · 7.68 KB
/
echarts.ts
File metadata and controls
247 lines (224 loc) · 7.68 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import type {
AriaComponentOption,
BarSeriesOption,
ComposeOption,
GridComponentOption,
LegendComponentOption,
LineSeriesOption,
PieSeriesOption,
ScatterSeriesOption,
TitleComponentOption,
ToolboxComponentOption,
VisualMapComponentOption,
} from "echarts"
import type { AxisBaseOption } from 'echarts/types/src/coord/axisCommonTypes.js'
import { isRtl } from "./document"
export const processAria = (option: ComposeOption<AriaComponentOption>, chartDecal: boolean) => {
if (!option) return
const ariaOption = generateAriaOption(chartDecal)
option.aria = ariaOption
}
const generateAriaOption = (chartDecal: boolean): AriaComponentOption => {
if (!chartDecal) {
return { enabled: false }
}
const color = "rgba(0, 0, 0, 0.2)"
return {
enabled: true,
decal: {
show: true,
decals: [{
color,
dashArrayX: [1, 0],
dashArrayY: [2, 5],
rotation: .5235987755982988,
}, {
color,
symbol: 'circle',
dashArrayX: [[8, 8], [0, 8, 8, 0]],
dashArrayY: [6, 0],
symbolSize: .8,
}, {
color,
dashArrayX: [1, 0],
dashArrayY: [4, 3],
rotation: -.7853981633974483
}, {
color,
dashArrayX: [[6, 6], [0, 6, 6, 0]],
dashArrayY: [6, 0],
}, {
color,
dashArrayX: [[1, 0], [1, 6]],
dashArrayY: [1, 0, 6, 0],
rotation: .7853981633974483,
}, {
color,
symbol: 'triangle',
dashArrayX: [[9, 9], [0, 9, 9, 0]],
dashArrayY: [7, 2],
symbolSize: .75,
}]
}
}
}
type SupportedSeriesOption = PieSeriesOption | LineSeriesOption | BarSeriesOption | ScatterSeriesOption
type GlobalEcOption = ComposeOption<
| TitleComponentOption | GridComponentOption | LegendComponentOption | ToolboxComponentOption | VisualMapComponentOption
| SupportedSeriesOption
>
export const processFont = (toProcess: unknown, elFont: string) => {
const options = toProcess as GlobalEcOption
const { series, title, legend, xAxis, yAxis } = options
processArrayLike(title, t => {
t.textStyle = {
...t.textStyle,
fontFamily: t.textStyle?.fontFamily ?? elFont,
}
t.subtextStyle = {
...t.subtextStyle,
fontFamily: t.subtextStyle?.fontFamily ?? elFont,
}
})
processArrayLike(series, s => {
s.label = {
...s.label,
fontFamily: s.label?.fontFamily ?? elFont,
}
})
processArrayLike(legend, l => {
l.textStyle = {
...l.textStyle,
fontFamily: l.textStyle?.fontFamily ?? elFont,
}
})
processArrayLike(xAxis, a => processAxisFont(a, elFont))
processArrayLike(yAxis, a => processAxisFont(a, elFont))
}
const processAxisFont = (a: AxisBaseOption, elFont: string) => {
a.axisLabel = {
...a.axisLabel,
fontFamily: a.axisLabel?.fontFamily ?? elFont,
}
}
export const processRtl = (toProcess: unknown) => {
if (!isRtl() || !toProcess) return
const option = toProcess as GlobalEcOption
const { grid, legend, series, toolbox, visualMap } = option
processArrayLike(grid, processGridRtl)
processArrayLike(legend, processLegendRtl)
processArrayLike(series, opt => processSeriesRtl(opt, option))
processArrayLike(toolbox, processToolboxRtl)
processArrayLike(visualMap, processVisualMapRtl)
}
export const processAnimation = (toProcess: unknown, duration: number) => {
const option = toProcess as GlobalEcOption
const { series } = option || {}
processArrayLike(series, s => {
if (duration > 0) {
s.animation = true
s.animationDuration = duration
} else {
s.animation = false
}
})
}
const processArrayLike = <T,>(arr: T | T[] | undefined, processor: (t: T) => void) => {
if (!arr) return
if (Array.isArray(arr)) {
arr.filter(e => !!e).forEach(processor)
} else {
processor?.(arr)
}
}
const someArrayLike = <T,>(arr: T | T[], test: (t: T) => boolean) => {
if (!arr || !test) return false
if (Array.isArray(arr)) {
arr.some(test)
} else {
return test(arr)
}
}
const processGridRtl = (option: GridComponentOption) => {
// Swap right and left
swapPosition(option)
}
const processLegendRtl = (option: LegendComponentOption) => {
swapPosition(option)
swapAlign(option, 'align', 'left')
}
const processToolboxRtl = (option: ToolboxComponentOption) => {
swapPosition(option)
const feature = option.feature || {}
const newFeature: ToolboxComponentOption['feature'] = {}
Object.entries(feature).reverse().forEach(([k, v]) => newFeature[k] = v)
option.feature = newFeature
}
const processVisualMapRtl = (option: VisualMapComponentOption) => {
swapPosition(option)
}
const processSeriesRtl = (option: SupportedSeriesOption, global: GlobalEcOption) => {
if (option.type === 'pie') {
processPieSeriesOption(option)
} else if (option.type === 'line') {
processLineSeriesOption(global)
} else if (option.type === 'bar') {
processBarSeriesOption(option, global)
} else if (option.type === 'scatter') {
// Let yAxis stay right
processArrayLike(global.yAxis, yOpt => swapAlign(yOpt, 'position', 'left'))
processArrayLike(global.xAxis, xOpt => xOpt.inverse = true)
}
}
const processPieSeriesOption = (option: PieSeriesOption) => {
const center = option.center
if (!Array.isArray(center)) return
const left = center[0]
if (typeof left !== 'string' || !left.endsWith('%')) return
try {
const originPercentStr = left.substring(0, left.length - 1)
const originPercent = left.includes('.') ? parseFloat(originPercentStr) : parseInt(originPercentStr)
center[0] = `${100 - originPercent}%`
} catch (ignored) { }
}
const processLineSeriesOption = (global: GlobalEcOption) => {
processArrayLike(global.xAxis, xOpt => xOpt.inverse = true)
}
const processBarSeriesOption = (option: BarSeriesOption, global: GlobalEcOption) => {
const isHorizontal = someArrayLike(global.xAxis, x => x?.type === 'value')
if (isHorizontal) {
// Let yAxis stay right
processArrayLike(global.yAxis, yOpt => swapAlign(yOpt, 'position', 'left'))
}
// Swap border radius
processArrayLike(option.data, data => processArrayLike(data, item => {
if (!(typeof item === 'object')) return
const { itemStyle: { borderRadius } = {} } = item as { itemStyle?: { borderRadius: number[] | any } }
Array.isArray(borderRadius) && swapBorderRadius(borderRadius)
}))
// Inverse xAxis
processArrayLike(global.xAxis, xOpt => xOpt.inverse = true)
}
type AlignVal = 'right' | 'left'
const swapAlign = <K extends string>(option: { [k in K]?: AlignVal | string }, key: K, defaultVal?: AlignVal) => {
const { [key]: align = defaultVal } = option || {}
if (align === 'left') {
option[key] = 'right'
} else if (align === 'right') {
option[key] = 'left'
}
}
const swapPosition = (option: { left?: string | number, right?: string | number }) => {
const right = option.right
option.right = option.left
option.left = right
}
const swapBorderRadius = (option: number[]) => {
if (option?.length !== 4) return
let t = option[0]
option[0] = option[1]
option[1] = t
t = option[2]
option[2] = option[3]
option[3] = t
}