-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtimeseries.go
100 lines (87 loc) · 2.22 KB
/
timeseries.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
package c3
import (
"encoding/json"
"github.com/grokify/mogo/time/timeutil"
"github.com/grokify/gocharts/v2/data/timeseries/interval"
)
type TimeseriesData struct {
IncludeTitle bool
Title string
TitleLevel string
DivID string
JSDataVar string
JSChartVar string
TimeSeriesSet *interval.TimeSeriesSet
JSONData TimeseriesDataJSON
}
type TimeseriesDataJSON struct {
Columns [][]any `json:"columns"`
Totals []int64 `json:"totals"`
TotalsMap map[string]int64 `json:"totalsMap"`
}
func (tdj *TimeseriesDataJSON) JSON() []byte {
bytes, err := json.Marshal(tdj)
if err != nil {
panic(err)
}
return bytes
}
type TimeseriesPageData struct {
Title string
URL string
Charts []TimeseriesData
Xox interval.YoYQoQGrowth
XoxPoints []interval.XoxPoint
}
func (data *TimeseriesData) AddTimeSeriesSet(set *interval.TimeSeriesSet, interval timeutil.Interval, seriesType interval.SeriesType) error {
data.TimeSeriesSet = set
columns := [][]any{}
xValues := []any{"x"}
totals := []int64{}
totalsMap := map[string]int64{}
seriesNames := set.SeriesNamesSorted()
for i, seriesName := range seriesNames {
timeSeries, err := set.GetTimeSeries(seriesName, seriesType)
if err != nil {
return err
}
yValues := []any{seriesName}
items := timeSeries.ItemsSorted()
for j, item := range items {
if i == 0 && interval == timeutil.IntervalQuarter {
xValues = append(xValues, item.Time.Format(timeutil.RFC3339FullDate))
}
yValues = append(yValues, item.Value)
if j == len(totals) {
totals = append(totals, int64(0))
}
totals[j] += item.Value
rfc3339ym := item.Time.Format(timeutil.ISO8601YM)
totalsMap[rfc3339ym] += item.Value
}
if i == 0 {
columns = append(columns, xValues)
}
columns = append(columns, yValues)
}
tsj := TimeseriesDataJSON{
Columns: columns,
Totals: totals,
TotalsMap: totalsMap}
data.JSONData = tsj
return nil
}
func (data *TimeseriesData) FormattedDataJSON() []byte {
bytes, err := json.Marshal(data.JSONData.Columns)
if err != nil {
panic(err)
}
return bytes
}
func (data *TimeseriesData) DataJSON() []byte {
bytes, err := json.Marshal(data)
if err != nil {
panic(err)
}
return bytes
}