-
Notifications
You must be signed in to change notification settings - Fork 547
/
wordcloud.go
88 lines (74 loc) 路 2.32 KB
/
wordcloud.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
package charts
import (
"github.com/go-echarts/go-echarts/datatypes"
"io"
)
// WordCloud represents a word cloud chart.
type WordCloud struct {
BaseOpts
Series
}
func (WordCloud) chartType() string { return ChartType.WordCloud }
// WordCloudOpts is the option set for a word cloud chart.
type WordCloudOpts struct {
// shape of WordCloud
// Optional: "circle", "rect", "roundRect", "triangle", "diamond", "pin", "arrow"
Shape string
// range of font size
SizeRange []float32
// range of font rotation angle
RotationRange []float32
}
func (WordCloudOpts) MarkSeries() {}
func (opt *WordCloudOpts) setChartOpt(s *singleSeries) {
s.Shape = opt.Shape
s.SizeRange = opt.SizeRange
s.RotationRange = opt.RotationRange
}
var wcTextColor = `function () {
return 'rgb(' + [
Math.round(Math.random() * 160),
Math.round(Math.random() * 160),
Math.round(Math.random() * 160)].join(',') + ')';
}`
// NewWordCloud creates a new word cloud chart.
func NewWordCloud(routers ...RouterOpts) *WordCloud {
chart := new(WordCloud)
chart.initBaseOpts(routers...)
chart.JSAssets.Add("echarts-wordcloud.min.js")
return chart
}
// Add adds new data sets.
func (c *WordCloud) Add(name string, data map[string]interface{}, options ...SeriesOptser) *WordCloud {
nvs := make([]datatypes.NameValueItem, 0)
for k, v := range data {
nvs = append(nvs, datatypes.NameValueItem{Name: k, Value: v})
}
series := singleSeries{Name: name, Type: ChartType.WordCloud, Data: nvs}
series.setSingleSeriesOpts(options...)
// set default random color for WordCloud chart
if series.TextStyleOpts.Normal == nil {
series.TextStyleOpts.Normal = &TextStyleOpts{Color: FuncOpts(wcTextColor)}
} else {
if series.TextStyleOpts.Normal.Color == "" {
series.TextStyleOpts.Normal.Color = FuncOpts(wcTextColor)
}
}
c.Series = append(c.Series, series)
c.setColor(options...)
return c
}
// SetGlobalOptions sets options for the WordCloud instance.
func (c *WordCloud) SetGlobalOptions(options ...GlobalOptser) *WordCloud {
c.BaseOpts.setBaseGlobalOptions(options...)
return c
}
func (c *WordCloud) validateOpts() {
c.validateAssets(c.AssetsHost)
}
// Render renders the chart and writes the output to given writers.
func (c *WordCloud) Render(w ...io.Writer) error {
c.insertSeriesColors(c.appendColor)
c.validateOpts()
return renderToWriter(c, "chart", []string{}, w...)
}