-
Notifications
You must be signed in to change notification settings - Fork 0
/
049.go-echarts-example.go
184 lines (156 loc) · 3.69 KB
/
049.go-echarts-example.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
package main
import (
"io"
"math/rand"
"os"
"log"
"net/http"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/components"
"github.com/go-echarts/go-echarts/v2/opts"
)
func logRequest(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}
func main() {
page := components.NewPage()
page.AddCharts(
mapBase(),
mapShowLabel(),
mapVisualMap(),
mapRegion(),
mapTheme(),
)
f, err := os.Create("map.html")
if err != nil {
panic(err)
}
page.Render(io.MultiWriter(f))
fs := http.FileServer(http.Dir("./"))
log.Println("running server at http://localhost:8089")
log.Fatal(http.ListenAndServe("localhost:8089", logRequest(fs)))
}
var (
baseMapData = []opts.MapData{
{"北京", float64(rand.Intn(150))},
{"上海", float64(rand.Intn(150))},
{"广东", float64(rand.Intn(150))},
{"辽宁", float64(rand.Intn(150))},
{"山东", float64(rand.Intn(150))},
{"山西", float64(rand.Intn(150))},
{"陕西", float64(rand.Intn(150))},
{"新疆", float64(rand.Intn(150))},
{"内蒙古", float64(rand.Intn(150))},
}
guangdongMapData = map[string]float64{
"深圳市": float64(rand.Intn(150)),
"广州市": float64(rand.Intn(150)),
"湛江市": float64(rand.Intn(150)),
"汕头市": float64(rand.Intn(150)),
"东莞市": float64(rand.Intn(150)),
"佛山市": float64(rand.Intn(150)),
"云浮市": float64(rand.Intn(150)),
"肇庆市": float64(rand.Intn(150)),
"梅州市": float64(rand.Intn(150)),
}
)
func generateMapData(data map[string]float64) (items []opts.MapData) {
items = make([]opts.MapData, 0)
for k, v := range data {
items = append(items, opts.MapData{Name: k, Value: v})
}
return
}
func mapBase() *charts.Map {
mc := charts.NewMap()
mc.RegisterMapType("china")
mc.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{
Title: "Map-example",
}),
)
mc.AddSeries("map", baseMapData)
return mc
}
func mapShowLabel() *charts.Map {
mc := charts.NewMap()
mc.RegisterMapType("china")
mc.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{
Title: "Map-show-label",
}),
)
mc.AddSeries("map", baseMapData).
SetSeriesOptions(
charts.WithLabelOpts(opts.Label{
Show: true,
}),
)
return mc
}
func mapVisualMap() *charts.Map {
mc := charts.NewMap()
mc.RegisterMapType("china")
mc.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{
Title: "Map-VisualMap",
}),
charts.WithVisualMapOpts(opts.VisualMap{
Calculable: true,
}),
)
mc.AddSeries("map", baseMapData)
return mc
}
func mapRegion() *charts.Map {
mc := charts.NewMap()
mc.RegisterMapType("广东")
mc.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{
Title: "Map-religion-Guangdong",
}),
charts.WithVisualMapOpts(opts.VisualMap{
Calculable: true,
InRange: &opts.VisualMapInRange{Color: []string{"#50a3ba", "#eac736", "#d94e5d"}},
}),
)
mc.AddSeries("map", generateMapData(guangdongMapData))
return mc
}
func mapTheme() *charts.Map {
mc := charts.NewMap()
mc.RegisterMapType("china")
mc.SetGlobalOptions(
charts.WithInitializationOpts(opts.Initialization{
Theme: "macarons",
}),
charts.WithTitleOpts(opts.Title{
Title: "Map-theme",
}),
charts.WithVisualMapOpts(opts.VisualMap{
Calculable: true,
Max: 150,
}),
)
mc.AddSeries("map", baseMapData)
return mc
}
type MapExamples struct{}
func (MapExamples) Examples() {
page := components.NewPage()
page.AddCharts(
mapBase(),
mapShowLabel(),
mapVisualMap(),
mapRegion(),
mapTheme(),
)
f, err := os.Create("examples/html/map.html")
if err != nil {
panic(err)
}
page.Render(io.MultiWriter(f))
}