-
Notifications
You must be signed in to change notification settings - Fork 2
/
axis.go
194 lines (165 loc) · 4.75 KB
/
axis.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
185
186
187
188
189
190
191
192
193
194
package plot
import (
"math"
)
// Axis defines an axis that defines how values are transformed to canvas space.
type Axis struct {
// Min value of the axis (in value space)
Min float64
// Max value of the axis (in value space)
Max float64
Flip bool
Ticks Ticks
MajorTicks int
MinorTicks int
Transform AxisTransform
}
// AxisTransform transforms values between canvas and value-space.
type AxisTransform interface {
ToCanvas(axis *Axis, v float64, screenMin, screenMax Length) Length
FromCanvas(axis *Axis, s Length, screenMin, screenMax Length) float64
}
// NewAxis creates a new axis.
func NewAxis() *Axis {
return &Axis{
Min: math.NaN(),
Max: math.NaN(),
Ticks: AutomaticTicks{},
MajorTicks: 5,
MinorTicks: 5,
}
}
// project projects points to canvas space using the given axes.
func project(data []Point, x, y *Axis, bounds Rect) []Point {
points := make([]Point, 0, len(data))
size := bounds.Size()
for _, p := range data {
p.X = x.ToCanvas(p.X, 0, size.X)
p.Y = y.ToCanvas(p.Y, 0, size.Y)
points = append(points, p)
}
return points
}
// projectcb projects points to canvas space with callbacks.
func projectcb(data []Point, x, y *Axis, bounds Rect, fn func(p Point)) {
size := bounds.Size()
for _, p := range data {
p.X = x.ToCanvas(p.X, 0, size.X)
p.Y = y.ToCanvas(p.Y, 0, size.Y)
fn(p)
}
}
// IsValid returns whether axis has been defined.
func (axis *Axis) IsValid() bool {
return !math.IsNaN(axis.Min) && !math.IsNaN(axis.Max)
}
func (axis *Axis) fixNaN() {
if math.IsNaN(axis.Min) {
axis.Min = 0
}
if math.IsNaN(axis.Max) {
axis.Max = 1
}
}
// lowhigh returns axis low and high values.
func (axis *Axis) lowhigh() (low, high float64) {
if axis.Flip {
return axis.Max, axis.Min
}
return axis.Min, axis.Max
}
// ToCanvas converts value to canvas space.
func (axis *Axis) ToCanvas(v float64, screenMin, screenMax Length) Length {
if axis.Transform != nil {
return axis.Transform.ToCanvas(axis, v, screenMin, screenMax)
}
low, high := axis.lowhigh()
n := (v - low) / (high - low)
return screenMin + n*(screenMax-screenMin)
}
// FromCanvas converts canvas point to value point.
func (axis *Axis) FromCanvas(s Length, screenMin, screenMax Length) float64 {
if axis.Transform != nil {
return axis.Transform.FromCanvas(axis, s, screenMin, screenMax)
}
low, high := axis.lowhigh()
n := (s - screenMin) / (screenMax - screenMin)
return low + n*(high-low)
}
// Include ensures that min and max can be displayed on the axis.
func (axis *Axis) Include(min, max float64) {
if math.IsNaN(axis.Min) {
axis.Min = min
} else {
axis.Min = math.Min(axis.Min, min)
}
if math.IsNaN(axis.Max) {
axis.Max = max
} else {
axis.Max = math.Max(axis.Max, max)
}
}
// MakeNice tries to adjust min, max such they look nice given the MajorTicks and MinorTicks.
func (axis *Axis) MakeNice() {
axis.Min, axis.Max = niceAxis(axis.Min, axis.Max, axis.MajorTicks, axis.MinorTicks)
axis.fixNaN()
}
// detectAxis automatically figures out axes using element stats.
func detectAxis(x, y *Axis, elements []Element) (X, Y *Axis) {
tx, ty := NewAxis(), NewAxis()
*tx, *ty = *x, *y
for _, element := range elements {
if stats, ok := tryGetStats(element); ok {
tx.Include(stats.Min.X, stats.Max.X)
ty.Include(stats.Min.Y, stats.Max.Y)
}
}
tx.Min, tx.Max = niceAxis(tx.Min, tx.Max, tx.MajorTicks, tx.MinorTicks)
ty.Min, ty.Max = niceAxis(ty.Min, ty.Max, ty.MajorTicks, ty.MinorTicks)
if !math.IsNaN(x.Min) {
tx.Min = x.Min
}
if !math.IsNaN(x.Max) {
tx.Max = x.Max
}
if !math.IsNaN(y.Min) {
ty.Min = y.Min
}
if !math.IsNaN(y.Max) {
ty.Max = y.Max
}
tx.fixNaN()
ty.fixNaN()
return tx, ty
}
// niceAxis calculates nice range for a given min, max or values.
func niceAxis(min, max float64, major, minor int) (nicemin, nicemax float64) {
span := niceNumber(max-min, false)
tickSpacing := niceNumber(span/(float64(major*minor)-1), true)
nicemin = math.Floor(min/tickSpacing) * tickSpacing
nicemax = math.Ceil(max/tickSpacing) * tickSpacing
return nicemin, nicemax
}
// ScreenSpaceTransform transforms using a custom func.
type ScreenSpaceTransform struct {
Transform func(v float64) float64
Inverse func(v float64) float64
}
// ToCanvas converts value to canvas space.
func (tx *ScreenSpaceTransform) ToCanvas(axis *Axis, v float64, screenMin, screenMax Length) Length {
low, high := axis.lowhigh()
n := (v - low) / (high - low)
if tx.Transform != nil {
n = tx.Transform(n)
}
return screenMin + n*(screenMax-screenMin)
}
// FromCanvas converts canvas point to value point.
func (tx *ScreenSpaceTransform) FromCanvas(axis *Axis, s Length, screenMin, screenMax Length) float64 {
low, high := axis.lowhigh()
n := (s - screenMin) / (screenMax - screenMin)
if tx.Inverse != nil {
n = tx.Inverse(n)
}
return low + n*(high-low)
}