-
Notifications
You must be signed in to change notification settings - Fork 4
/
continuous_series.go
75 lines (67 loc) · 2.08 KB
/
continuous_series.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
package wchart
import (
"github.com/grokify/mogo/time/month"
"github.com/grokify/mogo/time/quarter"
"github.com/grokify/mogo/time/timeutil"
chart "github.com/wcharczuk/go-chart/v2"
"github.com/grokify/gocharts/v2/data/timeseries"
)
func TimeSeriesMapToContinuousSeriesMonths(dsm map[string]timeseries.TimeSeries, order []string) []chart.ContinuousSeries {
csSet := []chart.ContinuousSeries{}
for _, seriesName := range order {
if ds, ok := dsm[seriesName]; ok {
csSet = append(csSet, TimeSeriesToContinuousSeries(ds))
}
}
return csSet
}
func TimeSeriesToContinuousSeries(ds timeseries.TimeSeries) chart.ContinuousSeries {
series := chart.ContinuousSeries{
Name: ds.SeriesName,
XValues: []float64{},
YValues: []float64{}}
items := ds.ItemsSorted()
for _, item := range items {
switch ds.Interval {
case timeutil.IntervalMonth:
series.XValues = append(series.XValues,
float64(month.TimeToMonthContinuous(item.Time)))
case timeutil.IntervalQuarter:
series.XValues = append(series.XValues,
float64(quarter.TimeToQuarterContinuous(item.Time)))
default:
series.XValues = append(series.XValues, float64(item.Time.Unix()))
}
if ds.IsFloat {
series.YValues = append(series.YValues, item.ValueFloat)
} else {
series.YValues = append(series.YValues, float64(item.Value))
}
}
return series
}
func TimeSeriesMapToContinuousSeriesQuarters(dsm map[string]timeseries.TimeSeries, order []string) []chart.ContinuousSeries {
csSet := []chart.ContinuousSeries{}
for _, seriesName := range order {
if ds, ok := dsm[seriesName]; ok {
csSet = append(csSet, TimeSeriesToContinuousSeriesQuarter(ds))
}
}
return csSet
}
func TimeSeriesToContinuousSeriesQuarter(ds timeseries.TimeSeries) chart.ContinuousSeries {
series := chart.ContinuousSeries{
Name: ds.SeriesName,
XValues: []float64{},
YValues: []float64{}}
items := ds.ItemsSorted()
for _, item := range items {
series.XValues = append(
series.XValues,
float64(quarter.TimeToQuarterContinuous(item.Time)))
series.YValues = append(
series.YValues,
float64(item.Value))
}
return series
}