-
Notifications
You must be signed in to change notification settings - Fork 0
/
chart.go
240 lines (209 loc) · 6.55 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
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
package wchart
import (
"image/color"
"syscall/js"
"github.com/soypat/gwasm"
)
type Chart struct {
js.Value
}
func NewChart(ctx js.Value, config *Config) Chart {
if !ctx.Truthy() {
panic("ctx argument to NewChart should be a DOM element node.")
}
if chart.IsUndefined() {
panic("Chart not defined. Did you add the script to DOM?")
}
if config.Type == "" {
panic("config must have defined type field")
}
return Chart{
Value: chart.Get("Chart").New(ctx, objectify(config)),
}
}
func (c Chart) Update() {
c.Call("update")
}
// Decimate keeps every mod'th point of all datasets. This
// can be used to improve rendering performance on graphs with many points.
// Higher mod values yield more aggressive decimation, with mod=2 being the least aggressive possible
// discarding 1 of every two sample points.
// mod must be greater than 1. Example: mod=10 discards 9 of every 10 points.
func (c Chart) Decimate(mod int) {
if mod < 2 {
panic("mod argument to Chart.Decimate must be greater than 1")
}
labels := c.GetConfig().Get("data").Get("labels")
n := labels.Length()
dlen := n / mod
xdata := make([]string, dlen)
for i := 0; i < dlen; i++ {
xdata[i] = labels.Index(i * mod).String()
}
c.SetLabels(xdata)
for _, dset := range c.GetConfig().Datasets() {
dset.decimate(mod)
}
}
func (c Chart) SetLabels(newLabels []string) {
data := js.Global().Get("Array").New()
for i := range newLabels {
data.Call("push", newLabels[i])
}
c.GetConfig().Get("data").Set("labels", data)
}
func (c Chart) GetConfig() ConfigHandle {
return ConfigHandle{
Value: c.Get("config"),
}
}
// IsHiddenMeta is null until user clicks on legend, then it is
// set to true if user has hidden dataset, or false if dataset is visible.
func (c Chart) IsHiddenMeta(datasetIdx int) js.Value {
return c.Call("getDatasetMeta", datasetIdx).Get("hidden")
}
// IsHidden returns true if dataset hidden property is set to true.
func (c Chart) IsHidden(datasetIdx int) bool {
hidden := c.GetConfig().Datasets()[datasetIdx].Get("hidden")
if hidden.IsNull() || hidden.IsUndefined() {
return false
}
return hidden.Bool()
}
type ConfigHandle struct {
js.Value
}
func (ch ConfigHandle) AppendFloats(label string, data []float64) {
datasets := ch.Datasets()
if len(data) != len(datasets) {
panic("length of incoming data must match length of existing datasets")
}
ch.Get("data").Get("labels").Call("push", label)
for i, d := range datasets {
d.AppendFloat(data[i])
}
}
func (ch ConfigHandle) Datasets() []DatasetHandle {
D := ch.Get("data").Get("datasets")
N := D.Get("length").Int()
datasets := make([]DatasetHandle, N)
for i := 0; i < N; i++ {
datasets[i] = DatasetHandle{
Value: D.Index(i),
}
}
return datasets
}
// ClearChartData destroys dataset `data` fields and config data.labels and sets them to empty array
func (ch ConfigHandle) ClearChartData() {
ch.Get("data").Set("labels", js.Global().Get("Array").New())
dsets := ch.Datasets()
for _, dset := range dsets {
dset.Set("data", js.Global().Get("Array").New())
}
}
// SetAnimation enables or disables animations for the chart.
// Usually used for performance reasons or when animations are distracting.
func (ch ConfigHandle) SetAnimation(enableAnimation bool) {
setOption(ch.Value, "animation", enableAnimation)
}
// SetSpanGaps If you have a lot of data points, it can be more performant to enable spanGaps.
// This disables segmentation of the line, which can be an unneeded step.
func (ch ConfigHandle) SetSpanGaps(enableSpanGaps bool) {
setOption(ch.Value, "spanGaps", enableSpanGaps)
}
// SetShowLine enables/disables line drawing for the chart.
func (ch ConfigHandle) SetShowLine(enableShowLine bool) {
setOption(ch.Value, "showLine", enableShowLine)
}
// SetPointRadius sets the point radius of the chart. Set to zero for performance gains.
func (ch ConfigHandle) SetPointRadius(pointRadius float64) {
opts := ch.Get("options")
if opts.IsUndefined() {
opts = js.Global().Get("Object").New()
ch.Set("options", opts)
}
elems := opts.Get("elements")
if elems.IsUndefined() {
elems = js.Global().Get("Object").New()
setOption(ch.Value, "elements", elems)
}
point := elems.Get("point")
if point.IsUndefined() {
point = js.Global().Get("Object").New()
elems.Set("point", point)
}
point.Set("radius", pointRadius)
}
type DatasetHandle struct {
js.Value
}
func (dh DatasetHandle) AppendFloat(f float64) {
data := dh.Get("data")
if !data.Truthy() {
dh.Set("data", js.Global().Get("Array").New())
data = dh.Get("data")
}
data.Call("push", f)
}
func (dh DatasetHandle) SetBackgroundColor(c color.Color) {
dh.Set("backgroundColor", gwasm.JSColor(c))
}
// SetColor sets font color
func (dh DatasetHandle) SetColor(c color.Color) {
dh.Set("color", gwasm.JSColor(c))
}
// SetBorderColor sets outline color of the chart data.
func (dh DatasetHandle) SetBorderColor(c color.Color) {
dh.Set("borderColor", gwasm.JSColor(c))
}
// SetAnimation enables or disables animations for the dataset.
// Usually used for performance reasons or when animations are distracting.
func (dh DatasetHandle) SetAnimation(enableAnimation bool) {
setOption(dh.Value, "animation", enableAnimation)
}
// SetSpanGaps if the dataset has a lot of data points, it can be more performant to enable spanGaps.
// This disables segmentation of the line, which can be an unneeded step.
func (dh DatasetHandle) SetSpanGaps(enableSpanGaps bool) {
setOption(dh.Value, "spanGaps", enableSpanGaps)
}
// SetShowLine disables/enables dataset line drawing.
// func (dh DatasetHandle) SetShowLine(enableShowLine bool) {
// setOption(dh.Value, "showLine", enableShowLine) // Not working.
// }
// SetPointRadius sets the point radius of dataset. Set to zero for performance gains.
func (dh DatasetHandle) SetPointRadius(pointRadius float64) {
dh.Set("pointRadius", pointRadius)
}
// SetHidden hides/shows dataset data in chart.
func (dh DatasetHandle) SetHidden(hidden bool) {
dh.Set("hidden", hidden)
}
func setOption(jsv js.Value, prop string, x interface{}) {
opts := jsv.Get("options")
if opts.IsUndefined() {
opts = js.Global().Get("Object").New()
jsv.Set("options", opts)
}
opts.Set(prop, x)
}
func (dh DatasetHandle) SetData(ydata []float64) {
data := js.Global().Get("Array").New()
for i := range ydata {
data.Call("push", ydata[i])
}
dh.Set("data", data)
}
func (dh DatasetHandle) decimate(mod int) {
if mod < 2 {
panic("mod argument to DatasetHandle.Decimate must be greater than 1")
}
data := dh.Get("data")
n := data.Length()
dlen := n / mod
ydata := make([]float64, dlen)
for i := 0; i < dlen; i++ {
ydata[i] = data.Index(i * mod).Float()
}
dh.SetData(ydata)
}