-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
184 lines (162 loc) · 3.37 KB
/
common.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package gochart
import (
"math"
"time"
"github.com/fogleman/gg"
"github.com/warmans/gochart/pkg/style"
)
const defaultMargin float64 = 8
const defaultTickSize float64 = 4
type Renderable interface {
Render(canvas *gg.Context, container BoundingBox) error
}
func NewStyles(defaults ...style.Opt) Styles {
return Styles{styleOpts: defaults}
}
type Styles struct {
styleOpts style.Opts
}
func (a *Styles) SetStyle(opt ...style.Opt) {
a.styleOpts = append(a.styleOpts, opt...)
}
// simply find the min and max numbers in the given
// slices.
func floatsRange(vv [][]float64) (float64, float64) {
overallMax := 0.0
overallMin := math.MaxFloat64
for _, v := range vv {
min, max := floatRange(v)
if min < overallMin {
overallMin = min
}
if max > overallMax {
overallMax = max
}
}
return overallMin, overallMax
}
func additiveFloatMerge(slices [][]float64) []float64 {
res := []float64{}
for _, sl := range slices {
for k, v := range sl {
if len(res)-1 < k {
res = append(res, v)
} else {
res[k] += v
}
}
}
return res
}
func floatRange(v []float64) (float64, float64) {
max := 0.0
min := math.MaxFloat64
for _, v := range v {
if v > max {
max = v
}
if v < min {
min = v
}
}
return min, max
}
func timeRange(v []time.Time) (time.Time, time.Time) {
max := time.Time{}
min := time.Unix(math.MaxInt32, 0) //2038 :(
for _, v := range v {
if v.After(max) {
max = v
}
if v.Before(min) {
min = v
}
}
return min, max
}
func BoundingBoxFromCanvas(ctx *gg.Context) BoundingBox {
return BoundingBox{
X: 10,
Y: 10,
W: float64(ctx.Width()) - 20,
H: float64(ctx.Height()) - 20,
}
}
func normalizeToRange(val, valMin, valMax, scaleMin, scaleMax float64) float64 {
return (((val - valMin) / valMax) * scaleMax) + scaleMin
}
func truncateStringToMaxSize(canvas *gg.Context, s string, size float64) string {
for {
if len([]rune(s)) < 1 {
return ""
}
w, _ := canvas.MeasureString(s)
if w > size {
s = s[:len([]rune(s))-1]
} else {
return s
}
}
}
func reduceNumLabelsToFitSpace(canvas *gg.Context, ss []Label, size float64) []Label {
for {
// actually none fit
if len(ss) == 0 {
return ss
}
if totalLabelsWidth(canvas, ss, defaultMargin*2) <= size {
return ss
}
// todo: this skews the labels to the left. It needs to center the ticks rather than arrange them
// from left to right. The problem is the labels are centered on the ticks so the first and
// last do not take up the expected space.
reduced := []Label{}
for k, s := range ss {
if k%2 == 0 {
reduced = append(reduced, s)
}
}
ss = reduced
}
}
func totalLabelsWidth(canvas *gg.Context, ss []Label, margins float64) float64 {
total := 0.0
for _, v := range ss {
w, _ := canvas.MeasureString(v.Value)
total += w + margins
}
return total
}
func widestLabelSize(canvas *gg.Context, ss []Label) (w float64, h float64) {
for _, s := range ss {
ww, hh := canvas.MeasureString(s.Value)
if ww > w {
w = ww
h = hh
}
}
return
}
func minInt64(a, b int64) int64 {
if a > b {
return b
}
return a
}
func maxInt(a, b int) int {
if a > b {
return a
}
return b
}
func allYData(series []Series) [][]float64 {
all := make([][]float64, 0)
for _, s := range series {
all = append(all, s.Ys())
}
return all
}
func TimeSeriesDuration(s []time.Time) time.Duration {
min, max := timeRange(s)
return max.Sub(min)
}