-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathecharts.go
498 lines (462 loc) · 12 KB
/
echarts.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package charts
import (
"bytes"
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/go-analyze/charts/chartdraw"
)
func convertToArray(data []byte) []byte {
data = bytes.TrimSpace(data)
if len(data) == 0 {
return nil
}
if data[0] != '[' {
data = []byte("[" + string(data) + "]")
}
return data
}
type EChartsPosition string
func (p *EChartsPosition) UnmarshalJSON(data []byte) error {
if len(data) == 0 {
return nil
}
if regexp.MustCompile(`^\d+`).Match(data) {
data = []byte(fmt.Sprintf(`"%s"`, string(data)))
}
s := (*string)(p)
return json.Unmarshal(data, s)
}
type EChartStyle struct {
Color string `json:"color"`
}
type EChartsSeriesDataValue struct {
values []float64
}
func (value *EChartsSeriesDataValue) UnmarshalJSON(data []byte) error {
data = convertToArray(data)
return json.Unmarshal(data, &value.values)
}
func (value *EChartsSeriesDataValue) First() float64 {
if len(value.values) == 0 {
return 0
}
return value.values[0]
}
func NewEChartsSeriesDataValue(values ...float64) EChartsSeriesDataValue {
return EChartsSeriesDataValue{
values: values,
}
}
type EChartsSeriesData struct {
Value EChartsSeriesDataValue `json:"value"`
Name string `json:"name"`
ItemStyle EChartStyle `json:"itemStyle"`
}
type _EChartsSeriesData EChartsSeriesData
var numericRep = regexp.MustCompile(`^[-+]?[0-9]+(?:\.[0-9]+)?$`)
func (es *EChartsSeriesData) UnmarshalJSON(data []byte) error {
data = bytes.TrimSpace(data)
if len(data) == 0 {
return nil
}
if numericRep.Match(data) {
v, err := strconv.ParseFloat(string(data), 64)
if err != nil {
return err
}
es.Value = EChartsSeriesDataValue{
values: []float64{
v,
},
}
return nil
}
v := _EChartsSeriesData{}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
es.Name = v.Name
es.Value = v.Value
es.ItemStyle = v.ItemStyle
return nil
}
type EChartsXAxisData struct {
BoundaryGap *bool `json:"boundaryGap"`
SplitNumber int `json:"splitNumber"`
Data []string `json:"data"`
Type string `json:"type"`
}
type EChartsXAxis struct {
Data []EChartsXAxisData
}
func (ex *EChartsXAxis) UnmarshalJSON(data []byte) error {
data = convertToArray(data)
if len(data) == 0 {
return nil
}
return json.Unmarshal(data, &ex.Data)
}
type EChartsAxisLabel struct {
Formatter string `json:"formatter"`
}
type EChartsYAxisData struct {
Min *float64 `json:"min"`
Max *float64 `json:"max"`
AxisLabel EChartsAxisLabel `json:"axisLabel"`
AxisLine struct {
LineStyle struct {
Color string `json:"color"`
} `json:"lineStyle"`
} `json:"axisLine"`
Data []string `json:"data"`
}
type EChartsYAxis struct {
Data []EChartsYAxisData `json:"data"`
}
func (ey *EChartsYAxis) UnmarshalJSON(data []byte) error {
data = convertToArray(data)
if len(data) == 0 {
return nil
}
return json.Unmarshal(data, &ey.Data)
}
type EChartsPadding struct {
Box chartdraw.Box
}
func (eb *EChartsPadding) UnmarshalJSON(data []byte) error {
data = convertToArray(data)
if len(data) == 0 {
return nil
}
arr := make([]int, 0)
if err := json.Unmarshal(data, &arr); err != nil {
return err
}
if len(arr) == 0 {
return nil
}
switch len(arr) {
case 1:
eb.Box = chartdraw.NewBox(arr[0], arr[0], arr[0], arr[0])
case 2:
eb.Box = chartdraw.NewBox(arr[0], arr[1], arr[1], arr[0])
default:
result := make([]int, 4)
copy(result, arr)
if len(arr) == 3 {
result[3] = result[1]
}
// top right, bottom left
eb.Box = chartdraw.NewBox(result[0], result[3], result[1], result[2])
}
return nil
}
type EChartsLabelOption struct {
Show bool `json:"show"`
Distance int `json:"distance"`
Color string `json:"color"`
}
type EChartsLegend struct {
Show *bool `json:"show"`
Data []string `json:"data"`
Align string `json:"align"`
Orient string `json:"orient"`
Padding EChartsPadding `json:"padding"`
Left EChartsPosition `json:"left"`
Top EChartsPosition `json:"top"`
TextStyle EChartsTextStyle `json:"textStyle"`
}
type EChartsMarkData struct {
Type string `json:"type"`
}
type _EChartsMarkData EChartsMarkData
func (emd *EChartsMarkData) UnmarshalJSON(data []byte) error {
data = bytes.TrimSpace(data)
if len(data) == 0 {
return nil
}
data = convertToArray(data)
ds := make([]*_EChartsMarkData, 0)
if err := json.Unmarshal(data, &ds); err != nil {
return err
}
for _, d := range ds {
if d.Type != "" {
emd.Type = d.Type
}
}
return nil
}
type EChartsMarkPoint struct {
SymbolSize int `json:"symbolSize"`
Data []EChartsMarkData `json:"data"`
}
func (emp *EChartsMarkPoint) ToSeriesMarkPoint() SeriesMarkPoint {
sp := SeriesMarkPoint{
SymbolSize: emp.SymbolSize,
}
if len(emp.Data) == 0 {
return sp
}
data := make([]SeriesMarkData, len(emp.Data))
for index, item := range emp.Data {
data[index].Type = item.Type
}
sp.Data = data
return sp
}
type EChartsMarkLine struct {
Data []EChartsMarkData `json:"data"`
}
func (eml *EChartsMarkLine) ToSeriesMarkLine() SeriesMarkLine {
sl := SeriesMarkLine{}
if len(eml.Data) == 0 {
return sl
}
data := make([]SeriesMarkData, len(eml.Data))
for index, item := range eml.Data {
data[index].Type = item.Type
}
sl.Data = data
return sl
}
type EChartsSeries struct {
Data []EChartsSeriesData `json:"data"`
Name string `json:"name"`
Type string `json:"type"`
Radius string `json:"radius"`
YAxisIndex int `json:"yAxisIndex"`
ItemStyle EChartStyle `json:"itemStyle"`
// label configuration
Label EChartsLabelOption `json:"label"`
MarkPoint EChartsMarkPoint `json:"markPoint"`
MarkLine EChartsMarkLine `json:"markLine"`
Max *float64 `json:"max"`
Min *float64 `json:"min"`
}
type EChartsSeriesList []EChartsSeries
func (esList EChartsSeriesList) ToSeriesList() SeriesList {
seriesList := make(SeriesList, 0, len(esList))
for _, item := range esList {
// if pie, each sub-recommendation generates a series
if item.Type == ChartTypePie {
for _, dataItem := range item.Data {
seriesList = append(seriesList, Series{
Type: item.Type,
Name: dataItem.Name,
Label: SeriesLabel{
Show: true,
},
Radius: item.Radius,
Data: []SeriesData{
{
Value: dataItem.Value.First(),
},
},
})
}
continue
}
if item.Type == ChartTypeRadar ||
item.Type == ChartTypeFunnel {
for _, dataItem := range item.Data {
seriesList = append(seriesList, Series{
Name: dataItem.Name,
Type: item.Type,
Data: NewSeriesDataFromValues(dataItem.Value.values),
Max: item.Max,
Min: item.Min,
Label: SeriesLabel{
FontStyle: FontStyle{
FontColor: ParseColor(item.Label.Color),
},
Show: item.Label.Show,
Distance: item.Label.Distance,
},
})
}
continue
}
data := make([]SeriesData, len(item.Data))
for j, dataItem := range item.Data {
data[j] = SeriesData{
Value: dataItem.Value.First(),
}
}
seriesList = append(seriesList, Series{
Type: item.Type,
Data: data,
YAxisIndex: item.YAxisIndex,
Label: SeriesLabel{
FontStyle: FontStyle{
FontColor: ParseColor(item.Label.Color),
},
Show: item.Label.Show,
Distance: item.Label.Distance,
},
Name: item.Name,
MarkPoint: item.MarkPoint.ToSeriesMarkPoint(),
MarkLine: item.MarkLine.ToSeriesMarkLine(),
})
}
return seriesList
}
type EChartsTextStyle struct {
Color string `json:"color"`
FontFamily string `json:"fontFamily"`
FontSize float64 `json:"fontSize"`
}
func (et *EChartsTextStyle) ToStyle() chartdraw.Style {
s := chartdraw.Style{
FontStyle: chartdraw.FontStyle{
FontSize: et.FontSize,
FontColor: ParseColor(et.Color),
},
}
if et.FontFamily != "" {
s.Font = GetFont(et.FontFamily)
}
return s
}
type EChartsOption struct {
Type string `json:"type"`
Theme string `json:"theme"`
FontFamily string `json:"fontFamily"`
Padding EChartsPadding `json:"padding"`
Box chartdraw.Box `json:"box"`
Width int `json:"width"`
Height int `json:"height"`
Title struct {
Text string `json:"text"`
Subtext string `json:"subtext"`
Left EChartsPosition `json:"left"`
Top EChartsPosition `json:"top"`
TextStyle EChartsTextStyle `json:"textStyle"`
SubtextStyle EChartsTextStyle `json:"subtextStyle"`
} `json:"title"`
XAxis EChartsXAxis `json:"xAxis"`
YAxis EChartsYAxis `json:"yAxis"`
Legend EChartsLegend `json:"legend"`
Radar struct {
Indicator []RadarIndicator `json:"indicator"`
} `json:"radar"`
Series EChartsSeriesList `json:"series"`
Children []EChartsOption `json:"children"`
}
func (eo *EChartsOption) ToOption() ChartOption {
fontFamily := eo.FontFamily
if len(fontFamily) == 0 {
fontFamily = eo.Title.TextStyle.FontFamily
}
titleTextStyle := eo.Title.TextStyle.ToStyle()
titleSubtextStyle := eo.Title.SubtextStyle.ToStyle()
legendTextStyle := eo.Legend.TextStyle.ToStyle()
o := ChartOption{
OutputFormat: eo.Type,
Font: GetFont(fontFamily),
Theme: GetTheme(eo.Theme),
Title: TitleOption{
Text: eo.Title.Text,
Subtext: eo.Title.Subtext,
FontStyle: FontStyle{
FontColor: titleTextStyle.FontColor,
FontSize: titleTextStyle.FontSize,
},
SubtextFontStyle: FontStyle{
FontSize: titleSubtextStyle.FontSize,
FontColor: titleSubtextStyle.FontColor,
},
Offset: OffsetStr{
Left: string(eo.Title.Left),
Top: string(eo.Title.Top),
},
},
Legend: LegendOption{
Show: eo.Legend.Show,
FontStyle: FontStyle{
FontSize: legendTextStyle.FontSize,
FontColor: legendTextStyle.FontColor,
},
Data: eo.Legend.Data,
Offset: OffsetStr{
Left: string(eo.Legend.Left),
Top: string(eo.Legend.Top),
},
Align: eo.Legend.Align,
Vertical: strings.EqualFold(eo.Legend.Orient, "vertical"),
},
RadarIndicators: eo.Radar.Indicator,
Width: eo.Width,
Height: eo.Height,
Padding: eo.Padding.Box,
Box: eo.Box,
SeriesList: eo.Series.ToSeriesList(),
}
isHorizontalChart := false
for _, item := range eo.XAxis.Data {
if item.Type == "value" {
isHorizontalChart = true
}
}
if isHorizontalChart {
for index := range o.SeriesList {
series := o.SeriesList[index]
if series.Type == ChartTypeBar {
o.SeriesList[index].Type = ChartTypeHorizontalBar
}
}
}
if len(eo.XAxis.Data) != 0 {
xAxisData := eo.XAxis.Data[0]
o.XAxis = XAxisOption{
BoundaryGap: xAxisData.BoundaryGap,
Data: xAxisData.Data,
LabelCount: xAxisData.SplitNumber,
}
if o.XAxis.BoundaryGap == nil {
// Ensure default ECharts behavior of centering labels and sets a "BoundaryGap"
// https://echarts.apache.org/en/option.html#xAxis.boundaryGap
o.XAxis.BoundaryGap = True()
}
}
yAxisOptions := make([]YAxisOption, len(eo.YAxis.Data))
for index, item := range eo.YAxis.Data {
yAxisOptions[index] = YAxisOption{
Min: item.Min,
Max: item.Max,
Formatter: item.AxisLabel.Formatter,
AxisColor: ParseColor(item.AxisLine.LineStyle.Color),
Data: item.Data,
}
}
o.YAxis = yAxisOptions
if len(eo.Children) != 0 {
o.Children = make([]ChartOption, len(eo.Children))
for index, item := range eo.Children {
o.Children[index] = item.ToOption()
}
}
return o
}
func renderEcharts(options, outputType string) ([]byte, error) {
o := EChartsOption{}
if err := json.Unmarshal([]byte(options), &o); err != nil {
return nil, err
}
opt := o.ToOption()
opt.OutputFormat = outputType
if p, err := Render(opt); err != nil {
return nil, err
} else {
return p.Bytes()
}
}
func RenderEChartsToPNG(options string) ([]byte, error) {
return renderEcharts(options, ChartOutputPNG)
}
func RenderEChartsToSVG(options string) ([]byte, error) {
return renderEcharts(options, ChartOutputSVG)
}