forked from go-echarts/go-echarts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
geo.go
86 lines (74 loc) · 2.22 KB
/
geo.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
package charts
import (
"fmt"
"github.com/chenjiandongx/go-echarts/datatypes"
"io"
"log"
"github.com/chenjiandongx/go-echarts/datasets"
)
// GeoComponentOpts is the option set for geo component.
type GeoComponentOpts struct {
Map string `json:"map,omitempty"`
}
// Geo represents a geo chart.
type Geo struct {
BaseOpts
Series
GeoComponentOpts
}
func (Geo) chartType() string { return ChartType.Geo }
var geoFormatter = `function (params) {
return params.name + ' : ' + params.value[2];
}`
// NewGeo creates a new geo chart.
func NewGeo(mapType string, routers ...RouterOpts) *Geo {
chart := new(Geo)
chart.initBaseOpts(routers...)
chart.HasGeo = true
chart.JSAssets.Add("maps/" + datasets.MapFileNames[mapType] + ".js")
chart.GeoComponentOpts.Map = mapType
return chart
}
// Add adds new data sets.
// geoType 是 Geo 图形的种类,有以下三种类型可选
// common.ChartType.Scatter
// common.ChartType.EffectScatter
// common.ChartType.HeatMap
func (c *Geo) Add(name, geoType string, data map[string]float32, options ...seriesOptser) *Geo {
nvs := make([]datatypes.NameValueItem, 0)
for k, v := range data {
nvs = append(nvs, datatypes.NameValueItem{Name: k, Value: c.extendValue(k, v)})
}
series := singleSeries{Name: name, Type: geoType, Data: nvs, CoordSystem: ChartType.Geo}
series.setSingleSeriesOpts(options...)
c.Series = append(c.Series, series)
c.setColor(options...)
return c
}
func (c *Geo) extendValue(region string, v float32) []float32 {
res := make([]float32, 0)
tv := datasets.Coordinates[region]
if tv == [2]float32{0, 0} {
log.Println(fmt.Sprintf("No coordinate is specified for %s", region))
} else {
res = append(tv[:], v)
}
return res
}
// SetGlobalOptions sets options for the Geo instance.
func (c *Geo) SetGlobalOptions(options ...globalOptser) *Geo {
c.BaseOpts.setBaseGlobalOptions(options...)
return c
}
func (c *Geo) validateOpts() {
if c.TooltipOpts.Formatter == "" {
c.TooltipOpts.Formatter = FuncOpts(geoFormatter)
}
c.validateAssets(c.AssetsHost)
}
// Render renders the chart and writes the output to given writers.
func (c *Geo) Render(w ...io.Writer) error {
c.insertSeriesColors(c.appendColor)
c.validateOpts()
return renderToWriter(c, "chart", []string{}, w...)
}