-
Notifications
You must be signed in to change notification settings - Fork 0
/
scale.go
131 lines (105 loc) · 2.71 KB
/
scale.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
package gochart
import "fmt"
type Label struct {
Value string
Tick int
}
type XScale interface {
NumTicks() int
Labels() []Label
Position(i int, b BoundingBox) float64
Offset() float64
}
type YScale interface {
NumTicks() int
Labels() []Label
MinMax() (float64, float64)
Position(v float64, b BoundingBox) float64
}
func NewXScale(series Series, offset float64) *StdXScale {
return &StdXScale{series: series, offset: offset}
}
type StdXScale struct {
series Series
offset float64
}
func (s *StdXScale) NumTicks() int {
if s.series.Ys() == nil {
return len(s.series.Ys())
}
return len(s.series.Xs())
}
func (s *StdXScale) Labels() []Label {
labels := make([]Label, s.NumTicks())
for i := 0; i < s.NumTicks(); i++ {
labels[i] = Label{s.series.X(i), i}
}
return labels
}
func (s *StdXScale) Position(i int, b BoundingBox) float64 {
if i > s.NumTicks() {
return b.RelX(b.W - s.offset)
}
// the actual size available is the total width with the margins removed.
finalScaleWidth := b.W - (s.offset * 2)
normalizedPosition := normalizeToRange(float64(i), 0, float64(s.NumTicks()-1), 0, finalScaleWidth)
return b.RelX(normalizedPosition) + s.offset
}
func (s *StdXScale) Offset() float64 {
return s.offset
}
func NewYScale(numTicks int, series ...Series) *StdYScale {
return &StdYScale{
d: series,
numTicks: numTicks,
}
}
type StdYScale struct {
d []Series
numTicks int
}
func (r *StdYScale) MinMax() (float64, float64) {
return floatsRange(allYData(r.d))
}
func (r *StdYScale) NumTicks() int {
return r.numTicks //todo: should scale based on the canvas size
}
func (r *StdYScale) Labels() []Label {
labels := make([]Label, r.NumTicks()+1)
_, max := r.MinMax()
for i := 0; i <= r.NumTicks(); i++ {
labels[i] = Label{fmt.Sprintf("%0.2f", (max/float64(r.NumTicks()))*float64(i)), i}
}
return labels
}
func (r *StdYScale) Position(v float64, b BoundingBox) float64 {
min, max := r.MinMax()
return b.MapY(min, max, v)
}
func NewStackedYScale(numTicks int, series ...Series) YScale {
return &StackedYScale{d: series, numTicks: 10}
}
type StackedYScale struct {
d []Series
numTicks int
}
func (s *StackedYScale) NumTicks() int {
return s.numTicks
}
func (s *StackedYScale) Labels() []Label {
labels := make([]Label, s.NumTicks()+1)
_, max := s.MinMax()
for i := 0; i <= s.NumTicks(); i++ {
labels[i] = Label{fmt.Sprintf("%0.2f", (max/float64(s.NumTicks()))*float64(i)), i}
}
return labels
}
func (s *StackedYScale) MinMax() (float64, float64) {
min, _ := floatsRange(allYData(s.d))
_, max := floatRange(additiveFloatMerge(allYData(s.d)))
return min, max
}
func (s *StackedYScale) Position(v float64, b BoundingBox) float64 {
min, max := s.MinMax()
return b.MapY(min, max, v)
}