-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtickers.go
More file actions
188 lines (158 loc) · 4.31 KB
/
Copy pathtickers.go
File metadata and controls
188 lines (158 loc) · 4.31 KB
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
185
186
187
188
package margaid
import (
"math"
"strconv"
"time"
"github.com/erkkah/margaid/svg"
)
// Ticker provides tick marks and labels for axes
type Ticker interface {
label(value float64) string
start(axis Axis, series *Series, steps int) float64
next(previous float64) (next float64, hasMore bool)
}
// TimeTicker returns time valued tick labels in the specified time format.
// TimeTicker assumes that time is linear.
func (m *Margaid) TimeTicker(format string) Ticker {
return &timeTicker{m, format, 1}
}
type timeTicker struct {
m *Margaid
format string
step float64
}
func (t *timeTicker) label(value float64) string {
formatted := TimeFromSeconds(value).Format(t.format)
return svg.EncodeText(formatted, svg.HAlignMiddle)
}
func (t *timeTicker) start(axis Axis, _ *Series, steps int) float64 {
minmax := t.m.ranges[axis]
scaleRange := minmax.max - minmax.min
scaleDuration := TimeFromSeconds(scaleRange).Sub(time.Unix(0, 0))
t.step = math.Pow(10.0, math.Trunc(math.Log10(scaleDuration.Seconds()/float64(steps))))
base := t.step
for int(scaleRange/t.step) > steps {
t.step += base
}
durationStep := time.Duration(t.step)
durationStart := time.Duration(minmax.min)
start := (durationStart * time.Second).Truncate(durationStep).Seconds()
if start < minmax.min {
start += t.step
}
return start
}
func (t *timeTicker) next(previous float64) (float64, bool) {
return previous + t.step, true
}
// ValueTicker returns tick labels by converting floats using strconv.FormatFloat
func (m *Margaid) ValueTicker(style byte, precision int, base int) Ticker {
return &valueTicker{
m: m,
step: 1,
style: style,
precision: precision,
base: base,
}
}
type valueTicker struct {
m *Margaid
projection Projection
step float64
style byte
precision int
base int
}
func (t *valueTicker) label(value float64) string {
return strconv.FormatFloat(value, t.style, t.precision, 64)
}
func (t *valueTicker) start(axis Axis, _ *Series, steps int) float64 {
t.projection = t.m.projections[axis]
minmax := t.m.ranges[axis]
scaleRange := minmax.max - minmax.min
startValue := 0.0
floatBase := float64(t.base)
if t.projection == Lin {
roundedLog := math.Round(math.Log(scaleRange/float64(steps)) / math.Log(floatBase))
t.step = math.Pow(floatBase, roundedLog)
base := t.step
for int(scaleRange/t.step) > steps {
t.step += base
}
wholeSteps := math.Ceil(minmax.min / t.step)
startValue = wholeSteps * t.step
return startValue
}
t.step = 0
startValue = math.Pow(floatBase, math.Round(math.Log(minmax.min)/math.Log(floatBase)))
for startValue < minmax.min {
startValue, _ = t.next(startValue)
}
return startValue
}
func (t *valueTicker) next(previous float64) (float64, bool) {
if t.projection == Lin {
return previous + t.step, true
}
floatBase := float64(t.base)
log := math.Log(previous) / math.Log(floatBase)
if log < 0 {
log = -math.Ceil(-log)
} else {
log = math.Floor(log)
}
increment := math.Pow(floatBase, log)
next := previous + increment
next /= increment
next = math.Round(next) * increment
return next, true
}
// LabeledTicker places tick marks and labels for all values
// of a series. The labels are provided by the labeler function.
func (m *Margaid) LabeledTicker(labeler func(float64) string) Ticker {
return &labeledTicker{
labeler: labeler,
}
}
type labeledTicker struct {
labeler func(float64) string
values []float64
index int
}
func (t *labeledTicker) label(value float64) string {
return svg.EncodeText(t.labeler(value), svg.HAlignMiddle)
}
func (t *labeledTicker) start(axis Axis, series *Series, _ int) float64 {
var values []float64
var get func(v Value) float64
if axis == X1Axis || axis == X2Axis {
get = func(v Value) float64 {
return v.X
}
}
if axis == Y1Axis || axis == Y2Axis {
get = func(v Value) float64 {
return v.Y
}
}
v := series.Values()
for v.Next() {
val := v.Get()
values = append(values, get(val))
}
t.values = values
return values[0]
}
func (t *labeledTicker) next(previous float64) (float64, bool) {
// Tickers are not supposed to have state.
// LabeledTicker breaks this, assuming a strict linear calling order.
if previous != t.values[t.index] {
return previous, false
}
t.index++
if t.index < len(t.values) {
return t.values[t.index], true
}
t.index = 0
return 0, false
}