-
Notifications
You must be signed in to change notification settings - Fork 0
/
series.go
160 lines (140 loc) · 2.88 KB
/
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
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
package gochart
import (
"fmt"
"time"
)
func NewYSeries(y []float64) Series {
return &XYSeries{y: y}
}
func NewXYSeries(x []string, y []float64) Series {
return &XYSeries{x: x, y: y}
}
type Series interface {
X(i int) string
Y(i int) float64
Ys() []float64
Xs() []string
AdditiveMerge(add Series) Series
}
type XYSeries struct {
x []string
y []float64
}
func (s *XYSeries) X(i int) string {
x := s.x
if x == nil {
x = s.Xs()
}
if i < len(x) {
return x[i]
}
return ""
}
func (s *XYSeries) Y(i int) float64 {
if i < len(s.y) {
return s.y[i]
}
return 0.0
}
func (s *XYSeries) Ys() []float64 {
return s.y
}
func (s *XYSeries) Xs() []string {
if s.x == nil {
//if no x-axis is set then just generate numeric values from the Y indexes.
xs := make([]string, len(s.y))
for i := 0; i < len(s.y); i++ {
xs[i] = fmt.Sprintf("%d", i)
}
return xs
}
return s.x
}
func (s *XYSeries) AdditiveMerge(add Series) Series {
merged := &XYSeries{
x: make([]string, len(s.Ys())),
y: make([]float64, len(s.Ys())),
}
for k := range s.Ys() {
merged.x[k] = s.X(k)
merged.y[k] = s.Y(k)
}
if add != nil {
for k := range merged.Ys() {
merged.y[k] += add.Y(k)
}
}
return merged
}
type TimeSeriesOpt func(t *TimeSeries)
func TimeFormat(formater func(t time.Time) string) TimeSeriesOpt {
return func(t *TimeSeries) {
t.timeFormatter = formater
}
}
func NewTimeSeries(x []time.Time, y []float64, opts ...TimeSeriesOpt) Series {
ts := &TimeSeries{x: x, y: y, seriesDuration: TimeSeriesDuration(x)}
for _, opt := range opts {
opt(ts)
}
return ts
}
type TimeSeries struct {
x []time.Time
y []float64
timeFormatter func(t time.Time) string
seriesDuration time.Duration
}
func (t *TimeSeries) X(i int) string {
if i < len(t.x) {
return t.formatTime(t.x[i])
}
return ""
}
func (t *TimeSeries) Y(i int) float64 {
if i < len(t.y) {
return t.y[i]
}
return 0.0
}
func (t *TimeSeries) Ys() []float64 {
return t.y
}
func (t *TimeSeries) Xs() []string {
//if no x-axis is set then just generate numeric values from the Y indexes.
xs := make([]string, len(t.x))
for i := 0; i < len(t.x); i++ {
xs[i] = t.formatTime(t.x[i])
}
return xs
}
func (t *TimeSeries) AdditiveMerge(add Series) Series {
merged := &TimeSeries{
x: make([]time.Time, len(t.Ys())),
y: make([]float64, len(t.Ys())),
}
for k := range t.Ys() {
merged.x[k] = t.x[k]
merged.y[k] = t.Y(k)
}
//todo : should this use equality checks on the times?
if add != nil {
for k := range merged.Ys() {
merged.y[k] += add.Y(k)
}
}
return merged
}
func (t *TimeSeries) formatTime(ts time.Time) string {
if t.timeFormatter == nil {
switch true {
case t.seriesDuration < time.Minute:
return ts.Format("15:04:05")
case t.seriesDuration < time.Hour*24:
return ts.Format("15:04")
default:
return ts.Format("2006-01-02 15:04:05")
}
}
return t.timeFormatter(ts)
}