-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart.go
153 lines (130 loc) · 2.94 KB
/
chart.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
package gouse
import (
"os"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/opts"
"github.com/go-echarts/go-echarts/v2/types"
)
/* Bar chart */
type IBarChartItem struct {
Name string
Values []float64
}
type IBarChartOpts struct {
Output string
Title string
Subtitle string
XAxis []string
Items []IBarChartItem
}
func generateBarItems(values []float64) []opts.BarData {
items := make([]opts.BarData, 0)
for i := 0; i < len(values); i++ {
items = append(items, opts.BarData{Value: values[i]})
}
return items
}
func CreateBarChart(options *IBarChartOpts) {
bar := charts.NewBar()
bar.SetGlobalOptions(charts.WithTitleOpts(opts.Title{
Title: options.Title,
Subtitle: options.Subtitle,
}))
for _, item := range options.Items {
bar.SetXAxis(options.XAxis).
AddSeries(item.Name, generateBarItems(item.Values))
}
f, _ := os.Create(options.Output)
_ = bar.Render(f)
}
/* Line chart */
type ILineChartItem struct {
Name string
Values []float64
}
type ILineChartOpts struct {
Output string
Title string
Subtitle string
XAxis []string
Items []ILineChartItem
}
func generateLineItems(values []float64) []opts.LineData {
items := make([]opts.LineData, 0)
for i := 0; i < len(values); i++ {
items = append(items, opts.LineData{Value: values[i]})
}
return items
}
func CreateLineChart(options *ILineChartOpts) {
line := charts.NewLine()
line.SetGlobalOptions(
charts.WithInitializationOpts(opts.Initialization{
Theme: types.ThemeInfographic,
}),
charts.WithTitleOpts(opts.Title{
Title: options.Title,
Subtitle: options.Subtitle,
}),
)
for _, item := range options.Items {
line.SetXAxis(options.XAxis).
AddSeries(item.Name, generateLineItems(item.Values)).
SetSeriesOptions(charts.WithLineChartOpts(opts.LineChart{Smooth: true}))
}
f, _ := os.Create(options.Output)
_ = line.Render(f)
}
/* Pie chart */
type IPieChartItem struct {
Name string
Values float64
}
type IPieChartOpts struct {
Output string
Title string
Subtitle string
Radius float64
Format string
ShowLabel bool
Items []IPieChartItem
}
func generatePieItems(ele []IPieChartItem) []opts.PieData {
items := make([]opts.PieData, 0)
for i := 0; i < len(ele); i++ {
items = append(items, opts.PieData{
Name: ele[i].Name,
Value: ele[i].Values,
})
}
return items
}
func CreatePieChart(options *IPieChartOpts) {
pie := charts.NewPie()
pie.SetGlobalOptions(
charts.WithTitleOpts(
opts.Title{
Title: options.Title,
Subtitle: options.Subtitle,
},
),
)
pie.SetSeriesOptions()
pie.AddSeries("Monthly revenue",
generatePieItems(options.Items)).
SetSeriesOptions(
charts.WithPieChartOpts(
opts.PieChart{
Radius: options.Radius,
},
),
charts.WithLabelOpts(
opts.Label{
Show: options.ShowLabel,
Formatter: options.Format,
},
),
)
f, _ := os.Create(options.Output)
_ = pie.Render(f)
}