-
Notifications
You must be signed in to change notification settings - Fork 4
/
c3.go
134 lines (115 loc) · 2.62 KB
/
c3.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
package c3
import (
"encoding/json"
)
const (
ChartTypeBar = "bar"
)
type C3Chart struct {
Bindto string `json:"bindto,omitempty"`
Data C3ChartData `json:"data,omitempty"`
Axis C3Axis `json:"axis,omitempty"`
Grid C3Grid `json:"grid,omitempty"`
Donut C3Donut `json:"donut,omitempty"`
Bar C3Bar `json:"bar,omitempty"`
}
func (data *C3Chart) MustJSON() []byte {
bytes, err := json.Marshal(data)
if err != nil {
panic(err)
}
return bytes
}
type C3ChartData struct {
X string `json:"x,omitempty"`
Columns [][]any `json:"columns,omitempty"`
Type string `json:"type,omitempty"`
}
func (data *C3ChartData) MustJSON() []byte {
bytes, err := json.Marshal(data)
if err != nil {
panic(err)
}
return bytes
}
type C3Axis struct {
X C3AxisX `json:"x,omitempty"`
}
type C3AxisX struct {
Type string `json:"type,omitempty"` // "Category"
Categories []string `json:"categories,omitempty"`
}
type C3Grid struct {
X C3GridLines `json:"x,omitempty"`
Y C3GridLines `json:"y,omitempty"`
}
type C3GridLines struct {
Show bool `json:"show,omitempty"`
Lines C3GridLine `json:"lines,omitempty"`
}
type C3GridLine struct {
Value any `json:"value,omitempty"`
Text string `json:"text,omitempty"`
Class string `json:"class,omitempty"`
Position string `json:"position,omitempty"`
}
type C3Donut struct {
Title string `json:"title,omitempty"`
}
/*
func C3ChartForEsAggregationSimple(agg v5.AggregationResRad) C3Chart {
c3Chart := C3Chart{
Data: C3ChartData{
Columns: [][]any{},
},
}
for _, bucket := range agg.AggregationData.Buckets {
c3Column := []any{bucket.Key, bucket.DocCount}
c3Chart.Data.Columns = append(c3Chart.Data.Columns, c3Column)
}
return c3Chart
}
*/
/*
var chart = c3.generate({
data: {
columns: [
['data1', 30, 200, 100, 400, 150, 250],
['data2', 130, 100, 140, 200, 150, 50]
],
type: 'bar'
},
bar: {
width: {
ratio: 0.5 // this makes bar width 50% of length between ticks
}
// or
//width: 100 // this makes bar width 100px
}
});
*/
type C3Bar struct {
WidthRatio float64
Width int
}
type C3ColumnsInt struct {
Columns []C3ColumnInt
}
func (cols *C3ColumnsInt) ToC3ChartData(chartType string) C3ChartData {
columns := [][]any{}
for _, col := range cols.Columns {
row := []any{}
row = append(row, col.Name)
for _, val := range col.Values {
row = append(row, val)
}
columns = append(columns, row)
}
return C3ChartData{
Columns: columns,
Type: chartType}
}
type C3ColumnInt struct {
Name string
Values []int
}