-
Notifications
You must be signed in to change notification settings - Fork 4
/
barchart.go
64 lines (54 loc) · 1.36 KB
/
barchart.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
package c3
import (
"fmt"
"github.com/grokify/gocharts/v2/data/slot"
"github.com/grokify/gocharts/v2/data/timeseries"
)
func TimeSeriesSetSimpleToC3ChartBar(data []timeseries.RowInt64, c3BarInfo C3Bar) C3Chart {
c3Chart := C3Chart{
Data: C3ChartData{
Columns: [][]any{},
Type: "bar"},
Bar: c3BarInfo}
for _, r := range data {
row := []any{}
row = append(row, r.Name)
for _, v := range r.Values {
row = append(row, v)
}
c3Chart.Data.Columns = append(c3Chart.Data.Columns, row)
}
return c3Chart
}
func SlotDataSeriesSetSimpleToC3ChartBar(input slot.SlotDataSeriesSetSimple, c3BarInfo C3Bar, hardMax int64) (C3Chart, error) {
output := C3Chart{
Data: C3ChartData{
Columns: [][]any{},
Type: "bar"},
Bar: c3BarInfo}
columns := [][]any{}
min, max := input.MinMaxX()
if hardMax > 0 {
max = hardMax
}
seriesNames := input.KeysSorted()
for _, seriesName := range seriesNames {
slotDataSeries, ok := input.SeriesSet[seriesName]
if !ok {
return output, fmt.Errorf("series name not found [%v]", seriesName)
}
column := []any{seriesName}
for i := min; i <= max; i++ {
//fmt.Printf("%v ", i)
x := i
if y, ok := slotDataSeries.SeriesData[x]; ok {
column = append(column, y)
} else {
column = append(column, 0)
}
}
columns = append(columns, column)
}
output.Data.Columns = columns
return output, nil
}