-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscatter_chart.go
123 lines (102 loc) · 3.81 KB
/
scatter_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
package charts
import (
"github.com/cnguy/gopherjs-frappe-charts/utils"
"github.com/gopherjs/gopherjs/js"
)
// ScatterChartArgs represents the relevant arguments that are used
// to instantiate a scatter chart.
type ScatterChartArgs struct {
*js.Object
Parent string `js:"parent"`
Title string `js:"title"`
Data *ChartData `js:"data"`
Type string `js:"type"`
Height int `js:"height"`
Colors []string `js:"colors"`
XAxisMode string `js:"x_axis_mode"`
YAxisMode string `js:"y_axis_mode"`
IsSeries int `js:"is_series"`
FormatTooltipX func(string) string `js:"format_tooltip_x"`
FormatTooltipY func(string) string `js:"format_tooltip_y"`
}
func NewScatterChart(parent string, data *ChartData) *ScatterChartArgs {
new := &ScatterChartArgs{Object: js.Global.Get("Object").New()}
new.Parent = parent
new.Type = "scatter"
new.Data = data
return new
}
func (chartArgs *ScatterChartArgs) WithTitle(title string) *ScatterChartArgs {
chartArgs.Title = title
return chartArgs
}
func (chartArgs *ScatterChartArgs) WithHeight(height int) *ScatterChartArgs {
chartArgs.Height = height
return chartArgs
}
func (chartArgs *ScatterChartArgs) WithColors(colors []string) *ScatterChartArgs {
chartArgs.Colors = colors
return chartArgs
}
func (chartArgs *ScatterChartArgs) SetXAxisMode(mode string) *ScatterChartArgs {
chartArgs.XAxisMode = mode
return chartArgs
}
func (chartArgs *ScatterChartArgs) SetYAxisMode(mode string) *ScatterChartArgs {
chartArgs.YAxisMode = mode
return chartArgs
}
// SetIsSeries allows us to set is_series using a boolean instead of 1/0's.
func (chartArgs *ScatterChartArgs) SetIsSeries(val bool) *ScatterChartArgs {
chartArgs.SetBton("is_series", val)
return chartArgs
}
func (chartArgs *ScatterChartArgs) WithFormatTooltipX(callback func(string) string) *ScatterChartArgs {
chartArgs.FormatTooltipX = callback
return chartArgs
}
func (chartArgs *ScatterChartArgs) WithFormatTooltipY(callback func(string) string) *ScatterChartArgs {
chartArgs.FormatTooltipY = callback
return chartArgs
}
func (chartArgs *ScatterChartArgs) SetBton(key string, val bool) {
chartArgs.Set(key, utils.Btoi(val))
}
type ScatterChart struct {
*js.Object
}
func (chart *ScatterChart) UpdateValues(updateValues *UpdateValuesArgs) {
chart.Call("update_values", updateValues.Values, updateValues.Labels)
}
func (chart *ScatterChart) AppendDataPoint(values []float64, label string) {
chart.Call("add_data_point", utils.FloatSliceToInterface(values), label)
}
func (chart *ScatterChart) AddDataPoint(values []float64, label string, index int) {
chart.Call("add_data_point", utils.FloatSliceToInterface(values), label, index)
}
// PopDataPoint calls remove_data_point without arguments, which defaults to removing the element at index 0.
func (chart *ScatterChart) PopDataPoint() {
chart.Call("remove_data_point")
}
// RemoveDataPoint allows users to remove a data point anywhere in the array.
func (chart *ScatterChart) RemoveDataPoint(index int) {
chart.Call("remove_data_point", index)
}
// ShowAverages calls show_averages which makes a new horizontal line representing the average for every dataset.
func (chart *ScatterChart) ShowAverages() {
chart.Call("show_averages")
}
// HideAverages simply hides the averages displayed using ShowAverages.
func (chart *ScatterChart) HideAverages() {
chart.Call("hide_averages")
}
// Render returns a ScatterChart that allows users to call the above functions.
func (chartArgs *ScatterChartArgs) Render() *ScatterChart {
// Set defaults that aren't handled by frappe.
if chartArgs.Height == 0 {
chartArgs.Height = 150
}
// Create actual chart.
new := ScatterChart{js.Global.Get("Chart").New(&chartArgs)}
return &new
}