-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchartJS-generator.go
202 lines (177 loc) · 5.14 KB
/
chartJS-generator.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
package main
import (
"fmt"
"math"
"strconv"
"golang.org/x/exp/rand"
)
const MA5_SKYBLUE string = "rgba(97, 141, 212, 0.6)"
const MA10_YELLOW string = "rgba(214, 194, 92, 0.6)"
const MA20_PURPLE string = "rgba(245, 158, 255, 0.6)"
type DataPoint struct {
X string `json:"x,omitempty"` // Time
O float64 `json:"o,omitempty"` // Open price
H float64 `json:"h,omitempty"` // High price
L float64 `json:"l,omitempty"` // Low price
C float64 `json:"c,omitempty"` // Close price
Y float64 `json:"y,omitempty"` // Volume,MA
}
// ChartJS Dataset
type Dataset struct {
Type string `json:"type"`
Label string `json:"label"`
Data []DataPoint `json:"data"`
BackgroundColor interface{} `json:"backgroundColor,omitempty"` // Dynamic or static color
BorderColor interface{} `json:"borderColor,omitempty"`
BorderWidth int `json:"borderWidth,omitempty"`
YAxisID string `json:"yAxisID"`
BarPercentage float64 `json:"barPercentage,omitempty"`
CategoryPercentage float64 `json:"categoryPercentage,omitempty"`
Fill bool `json:"fill,omitempty"`
}
// ChartJS Config
type ChartConfig struct {
Type string `json:"type"`
Data struct {
Labels []string `json:"labels"`
Datasets []Dataset `json:"datasets"`
} `json:"data"`
Options map[string]interface{} `json:"options"`
}
type GenericDataset struct {
Type string `json:"type"`
Label string `json:"label"`
Data []float64 `json:"data"`
BackgroundColor interface{} `json:"backgroundColor,omitempty"`
BorderColor interface{} `json:"borderColor,omitempty"`
BorderWidth int `json:"borderWidth,omitempty"`
HoverOffset int `json:"hoverOffset,omitempty"`
}
type GenericChartConfig struct {
Type string `json:"type"`
Data struct {
Labels []string `json:"labels"`
Datasets []GenericDataset `json:"datasets"`
} `json:"data"`
Options map[string]interface{} `json:"options"`
}
func GenCandleDataPoint(x string, o float64, h float64, l float64, c float64) DataPoint {
return DataPoint{X: x, O: o, H: h, L: l, C: c}
}
func GenXYDataPoint(x string, y float64) DataPoint {
return DataPoint{X: x, Y: y}
}
func GenCandleDataset(ticket string, candle []DataPoint) Dataset {
return Dataset{
Type: "candlestick",
Label: ticket,
Data: candle,
YAxisID: "price",
}
}
func GenLineDataset(name string, data []DataPoint, color string) Dataset {
return Dataset{
Type: "line",
Label: name,
Data: data,
BorderColor: color,
Fill: false,
YAxisID: "price",
BorderWidth: 2,
}
}
func GenVolumeDataset(volume []DataPoint) Dataset {
return Dataset{
Type: "bar",
Label: "Volume",
Data: volume,
YAxisID: "volume",
BarPercentage: 0.8,
CategoryPercentage: 0.8,
BackgroundColor: "rgba(75, 192, 192, 0.6)", // Static color
}
}
func GenGenericDataset(graphType string, graphName string, val []float64, bgColor []string) GenericDataset {
return GenericDataset{
Type: graphType,
Label: graphName,
Data: val,
BackgroundColor: bgColor,
HoverOffset: 2,
}
}
func GenBGColor() string {
r := rand.Int63n(256)
g := rand.Int63n(256)
b := rand.Int63n(256)
return fmt.Sprintf("rgba(%s, %s, %s, 0.6)", strconv.FormatInt(r, 10), strconv.FormatInt(g, 10), strconv.FormatInt(b, 10))
}
func GenCandleStickChartConfig(labels []string, datasets []Dataset) ChartConfig {
config := ChartConfig{Type: "candlestick"}
config.Data.Labels = labels
config.Data.Datasets = datasets
nr := len(datasets[0].Data)
hh := 0.0
ll := math.MaxFloat64
vhh := 0.0
for i := 0; i < nr; i += 1 {
hh = math.Max(hh, datasets[0].Data[i].H)
ll = math.Min(ll, datasets[0].Data[i].L)
vhh = math.Max(vhh, datasets[1].Data[i].Y)
}
hh = hh * 1.05
ll = ll * 0.95
height := hh - ll
ll -= (height * 0.2)
vhh = vhh * 5
config.Options = map[string]interface{}{
"responsive": true,
"scales": map[string]interface{}{
"x": map[string]interface{}{
"type": "category",
"ticks": map[string]interface{}{
"autoSkip": false,
},
},
"price": map[string]interface{}{
"type": "linear",
"max": hh,
"min": ll,
"beginAtZero": false,
"position": "left",
"title": map[string]interface{}{
"display": false,
"text": "Price",
},
},
"volume": map[string]interface{}{
"type": "linear",
"max": vhh,
"min": 0,
"position": "right",
"title": map[string]interface{}{
"display": false,
"text": "Volume",
},
"grid": map[string]interface{}{
"drawOnChartArea": false,
},
},
},
}
return config
}
func GenGenericChartConfig(graphType string, labels []string, datasets []GenericDataset) GenericChartConfig {
config := GenericChartConfig{Type: graphType}
config.Data.Labels = labels
config.Data.Datasets = datasets
config.Options = map[string]interface{}{
"responsive": true,
"plugins": map[string]interface{}{
"legend": map[string]interface{}{
"display": false,
},
},
}
return config
}