/
base.go
291 lines (244 loc) · 6.49 KB
/
base.go
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package charts
import (
"github.com/go-echarts/go-echarts/v2/datasets"
"github.com/go-echarts/go-echarts/v2/opts"
"github.com/go-echarts/go-echarts/v2/render"
)
// GlobalOpts sets the Global options for charts.
type GlobalOpts func(bc *BaseConfiguration)
// BaseConfiguration represents an option set needed by all chart types.
type BaseConfiguration struct {
opts.Legend `json:"legend"`
opts.Tooltip `json:"tooltip"`
opts.Toolbox `json:"toolbox"`
opts.Title `json:"title"`
opts.Polar `json:"polar"`
opts.AngleAxis `json:"angleAxis"`
opts.RadiusAxis `json:"radiusAxis"`
render.Renderer `json:"-"`
opts.Initialization `json:"-"`
opts.Assets `json:"-"`
opts.RadarComponent `json:"-"`
opts.GeoComponent `json:"-"`
opts.ParallelComponent `json:"-"`
opts.JSFunctions `json:"-"`
opts.SingleAxis `json:"-"`
MultiSeries
XYAxis
opts.XAxis3D
opts.YAxis3D
opts.ZAxis3D
opts.Grid3D
legends []string
// Colors is the color list of palette.
// If no color is set in series, the colors would be adopted sequentially and circularly
// from this list as the colors of series.
Colors []string
appendColor []string // append customize color to the Colors(reverse order)
DataZoomList []opts.DataZoom `json:"datazoom,omitempty"`
VisualMapList []opts.VisualMap `json:"visualmap,omitempty"`
// ParallelAxisList represents the component list which is the coordinate axis for parallel coordinate.
ParallelAxisList []opts.ParallelAxis
has3DAxis bool
hasXYAxis bool
hasGeo bool
hasRadar bool
hasParallel bool
hasSingleAxis bool
hasPolar bool
}
// JSON wraps all the options to a map so that it could be used in the base template
//
// Get data in bytes
// bs, _ : = json.Marshal(bar.JSON())
func (bc *BaseConfiguration) JSON() map[string]interface{} {
obj := map[string]interface{}{
"title": bc.Title,
"legend": bc.Legend,
"tooltip": bc.Tooltip,
"series": bc.MultiSeries,
}
if bc.hasPolar {
obj["polar"] = bc.Polar
obj["angleAxis"] = bc.AngleAxis
obj["radiusAxis"] = bc.RadiusAxis
}
if bc.hasGeo {
obj["geo"] = bc.GeoComponent
}
if bc.hasRadar {
obj["radar"] = bc.RadarComponent
}
if bc.hasParallel {
obj["parallel"] = bc.ParallelComponent
obj["parallelAxis"] = bc.ParallelAxisList
}
if bc.hasSingleAxis {
obj["singleAxis"] = bc.SingleAxis
}
if bc.Toolbox.Show {
obj["toolbox"] = bc.Toolbox
}
if len(bc.DataZoomList) > 0 {
obj["dataZoom"] = bc.DataZoomList
}
if len(bc.VisualMapList) > 0 {
obj["visualMap"] = bc.VisualMapList
}
if bc.hasXYAxis {
obj["xAxis"] = bc.XAxisList
obj["yAxis"] = bc.YAxisList
}
if bc.has3DAxis {
obj["xAxis3D"] = bc.XAxis3D
obj["yAxis3D"] = bc.YAxis3D
obj["zAxis3D"] = bc.ZAxis3D
obj["grid3D"] = bc.Grid3D
}
if bc.Theme == "white" {
obj["color"] = bc.Colors
}
if bc.BackgroundColor != "" {
obj["backgroundColor"] = bc.BackgroundColor
}
return obj
}
// GetAssets returns the Assets options
func (bc *BaseConfiguration) GetAssets() opts.Assets {
return bc.Assets
}
func (bc *BaseConfiguration) initBaseConfiguration() {
bc.initSeriesColors()
bc.InitAssets()
bc.initXYAxis()
bc.Initialization.Validate()
}
func (bc *BaseConfiguration) initSeriesColors() {
bc.Colors = []string{
"#5470c6", "#91cc75", "#fac858", "#ee6666", "#73c0de",
"#3ba272", "#fc8452", "#9a60b4", "#ea7ccc",
}
}
func (bc *BaseConfiguration) insertSeriesColors(colors []string) {
reversed := reverseSlice(colors)
for i := 0; i < len(reversed); i++ {
bc.Colors = append(bc.Colors, "")
copy(bc.Colors[1:], bc.Colors[0:])
bc.Colors[0] = reversed[i]
}
}
func (bc *BaseConfiguration) setBaseGlobalOptions(opts ...GlobalOpts) {
for _, opt := range opts {
opt(bc)
}
}
// WithPolarOps set angleAxis
func WithAngleAxisOps(opt opts.AngleAxis) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.AngleAxis = opt
}
}
// WithPolarOps set radiusAxis
func WithRadiusAxisOps(opt opts.RadiusAxis) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.RadiusAxis = opt
}
}
// WithPolarOps set polar
func WithPolarOps(opt opts.Polar) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.Polar = opt
}
}
// WithTitleOpts
func WithTitleOpts(opt opts.Title) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.Title = opt
}
}
// WithToolboxOpts
func WithToolboxOpts(opt opts.Toolbox) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.Toolbox = opt
}
}
// WithSingleAxisOpts
func WithSingleAxisOpts(opt opts.SingleAxis) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.SingleAxis = opt
}
}
// WithTooltipOpts
func WithTooltipOpts(opt opts.Tooltip) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.Tooltip = opt
}
}
// WithLegendOpts
func WithLegendOpts(opt opts.Legend) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.Legend = opt
}
}
// WithInitializationOpts
func WithInitializationOpts(opt opts.Initialization) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.Initialization = opt
if bc.Initialization.Theme != "" &&
bc.Initialization.Theme != "white" &&
bc.Initialization.Theme != "dark" {
bc.JSAssets.Add("themes/" + opt.Theme + ".js")
}
bc.Initialization.Validate()
}
}
// WithDataZoomOpts
func WithDataZoomOpts(opt ...opts.DataZoom) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.DataZoomList = append(bc.DataZoomList, opt...)
}
}
// WithVisualMapOpts
func WithVisualMapOpts(opt ...opts.VisualMap) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.VisualMapList = append(bc.VisualMapList, opt...)
}
}
// WithRadarComponentOpts
func WithRadarComponentOpts(opt opts.RadarComponent) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.RadarComponent = opt
}
}
// WithGeoComponentOpts
func WithGeoComponentOpts(opt opts.GeoComponent) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.GeoComponent = opt
bc.JSAssets.Add("maps/" + datasets.MapFileNames[opt.Map] + ".js")
}
}
// WithParallelComponentOpts
func WithParallelComponentOpts(opt opts.ParallelComponent) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.ParallelComponent = opt
}
}
// WithParallelAxisList
func WithParallelAxisList(opt []opts.ParallelAxis) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.ParallelAxisList = opt
}
}
// WithColorsOpts
func WithColorsOpts(opt opts.Colors) GlobalOpts {
return func(bc *BaseConfiguration) {
bc.insertSeriesColors(opt)
}
}
// reverseSlice reverse string slice
func reverseSlice(s []string) []string {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}