forked from gonum/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
axis.go
500 lines (441 loc) · 11.6 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// Copyright ©2015 The gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package plot
import (
"fmt"
"image/color"
"math"
"github.com/gonum/plot/vg"
"github.com/gonum/plot/vg/draw"
)
// Ticker creates Ticks in a specified range
type Ticker interface {
// Ticks returns Ticks in a specified range
Ticks(min, max float64) []Tick
}
// Normalizer rescales values from the data coordinate system to the
// normalized coordinate system.
type Normalizer interface {
// Normalize transforms a value x in the data coordinate system to
// the normalized coordinate system.
Normalize(min, max, x float64) float64
}
// An Axis represents either a horizontal or vertical
// axis of a plot.
type Axis struct {
// Min and Max are the minimum and maximum data
// values represented by the axis.
Min, Max float64
Label struct {
// Text is the axis label string.
Text string
// TextStyle is the style of the axis label text.
draw.TextStyle
}
// LineStyle is the style of the axis line.
draw.LineStyle
// Padding between the axis line and the data. Having
// non-zero padding ensures that the data is never drawn
// on the axis, thus making it easier to see.
Padding vg.Length
Tick struct {
// Label is the TextStyle on the tick labels.
Label draw.TextStyle
// LineStyle is the LineStyle of the tick lines.
draw.LineStyle
// Length is the length of a major tick mark.
// Minor tick marks are half of the length of major
// tick marks.
Length vg.Length
// Marker returns the tick marks. Any tick marks
// returned by the Marker function that are not in
// range of the axis are not drawn.
Marker Ticker
}
// Scale transforms a value given in the data coordinate system
// to the normalized coordinate system of the axis—its distance
// along the axis as a fraction of the axis range.
Scale Normalizer
}
// makeAxis returns a default Axis.
//
// The default range is (∞, ∞), and thus any finite
// value is less than Min and greater than Max.
func makeAxis() (Axis, error) {
labelFont, err := vg.MakeFont(DefaultFont, vg.Points(12))
if err != nil {
return Axis{}, err
}
tickFont, err := vg.MakeFont(DefaultFont, vg.Points(10))
if err != nil {
return Axis{}, err
}
a := Axis{
Min: math.Inf(1),
Max: math.Inf(-1),
LineStyle: draw.LineStyle{
Color: color.Black,
Width: vg.Points(0.5),
},
Padding: vg.Points(5),
Scale: LinearScale{},
}
a.Label.TextStyle = draw.TextStyle{
Color: color.Black,
Font: labelFont,
}
a.Tick.Label = draw.TextStyle{
Color: color.Black,
Font: tickFont,
}
a.Tick.LineStyle = draw.LineStyle{
Color: color.Black,
Width: vg.Points(0.5),
}
a.Tick.Length = vg.Points(8)
a.Tick.Marker = DefaultTicks{}
return a, nil
}
// sanitizeRange ensures that the range of the
// axis makes sense.
func (a *Axis) sanitizeRange() {
if math.IsInf(a.Min, 0) {
a.Min = 0
}
if math.IsInf(a.Max, 0) {
a.Max = 0
}
if a.Min > a.Max {
a.Min, a.Max = a.Max, a.Min
}
if a.Min == a.Max {
a.Min -= 1
a.Max += 1
}
}
// LinearScale an be used as the value of an Axis.Scale function to
// set the axis to a standard linear scale.
type LinearScale struct{}
var _ Normalizer = LinearScale{}
func (LinearScale) Normalize(min, max, x float64) float64 {
return (x - min) / (max - min)
}
// LocScale can be used as the value of an Axis.Scale function to
// set the axis to a log scale.
type LogScale struct{}
var _ Normalizer = LogScale{}
func (LogScale) Normalize(min, max, x float64) float64 {
logMin := log(min)
return (log(x) - logMin) / (log(max) - logMin)
}
// Norm returns the value of x, given in the data coordinate
// system, normalized to its distance as a fraction of the
// range of this axis. For example, if x is a.Min then the return
// value is 0, and if x is a.Max then the return value is 1.
func (a *Axis) Norm(x float64) float64 {
return a.Scale.Normalize(a.Min, a.Max, x)
}
// drawTicks returns true if the tick marks should be drawn.
func (a *Axis) drawTicks() bool {
return a.Tick.Width > 0 && a.Tick.Length > 0
}
// A horizontalAxis draws horizontally across the bottom
// of a plot.
type horizontalAxis struct {
Axis
}
// size returns the height of the axis.
func (a *horizontalAxis) size() (h vg.Length) {
if a.Label.Text != "" {
h -= a.Label.Font.Extents().Descent
h += a.Label.Height(a.Label.Text)
}
if marks := a.Tick.Marker.Ticks(a.Min, a.Max); len(marks) > 0 {
if a.drawTicks() {
h += a.Tick.Length
}
h += tickLabelHeight(a.Tick.Label, marks)
}
h += a.Width / 2
h += a.Padding
return
}
// draw draws the axis along the lower edge of a draw.Canvas.
func (a *horizontalAxis) draw(c draw.Canvas) {
y := c.Min.Y
if a.Label.Text != "" {
y -= a.Label.Font.Extents().Descent
c.FillText(a.Label.TextStyle, c.Center().X, y, -0.5, 0, a.Label.Text)
y += a.Label.Height(a.Label.Text)
}
marks := a.Tick.Marker.Ticks(a.Min, a.Max)
for _, t := range marks {
x := c.X(a.Norm(t.Value))
if !c.ContainsX(x) || t.IsMinor() {
continue
}
c.FillText(a.Tick.Label, x, y, -0.5, 0, t.Label)
}
if len(marks) > 0 {
y += tickLabelHeight(a.Tick.Label, marks)
} else {
y += a.Width / 2
}
if len(marks) > 0 && a.drawTicks() {
len := a.Tick.Length
for _, t := range marks {
x := c.X(a.Norm(t.Value))
if !c.ContainsX(x) {
continue
}
start := t.lengthOffset(len)
c.StrokeLine2(a.Tick.LineStyle, x, y+start, x, y+len)
}
y += len
}
c.StrokeLine2(a.LineStyle, c.Min.X, y, c.Max.X, y)
}
// GlyphBoxes returns the GlyphBoxes for the tick labels.
func (a *horizontalAxis) GlyphBoxes(*Plot) (boxes []GlyphBox) {
for _, t := range a.Tick.Marker.Ticks(a.Min, a.Max) {
if t.IsMinor() {
continue
}
w := a.Tick.Label.Width(t.Label)
box := GlyphBox{
X: a.Norm(t.Value),
Rectangle: draw.Rectangle{draw.Point{X: -w / 2}, draw.Point{X: w / 2}},
}
boxes = append(boxes, box)
}
return
}
// A verticalAxis is drawn vertically up the left side of a plot.
type verticalAxis struct {
Axis
}
// size returns the width of the axis.
func (a *verticalAxis) size() (w vg.Length) {
if a.Label.Text != "" {
w -= a.Label.Font.Extents().Descent
w += a.Label.Height(a.Label.Text)
}
if marks := a.Tick.Marker.Ticks(a.Min, a.Max); len(marks) > 0 {
if lwidth := tickLabelWidth(a.Tick.Label, marks); lwidth > 0 {
w += lwidth
w += a.Label.Width(" ")
}
if a.drawTicks() {
w += a.Tick.Length
}
}
w += a.Width / 2
w += a.Padding
return
}
// draw draws the axis along the left side of a draw.Canvas.
func (a *verticalAxis) draw(c draw.Canvas) {
x := c.Min.X
if a.Label.Text != "" {
x += a.Label.Height(a.Label.Text)
c.Push()
c.Rotate(math.Pi / 2)
c.FillText(a.Label.TextStyle, c.Center().Y, -x, -0.5, 0, a.Label.Text)
c.Pop()
x += -a.Label.Font.Extents().Descent
}
marks := a.Tick.Marker.Ticks(a.Min, a.Max)
if w := tickLabelWidth(a.Tick.Label, marks); len(marks) > 0 && w > 0 {
x += w
}
major := false
for _, t := range marks {
y := c.Y(a.Norm(t.Value))
if !c.ContainsY(y) || t.IsMinor() {
continue
}
c.FillText(a.Tick.Label, x, y, -1, -0.5, t.Label)
major = true
}
if major {
x += a.Tick.Label.Width(" ")
}
if a.drawTicks() && len(marks) > 0 {
len := a.Tick.Length
for _, t := range marks {
y := c.Y(a.Norm(t.Value))
if !c.ContainsY(y) {
continue
}
start := t.lengthOffset(len)
c.StrokeLine2(a.Tick.LineStyle, x+start, y, x+len, y)
}
x += len
}
c.StrokeLine2(a.LineStyle, x, c.Min.Y, x, c.Max.Y)
}
// GlyphBoxes returns the GlyphBoxes for the tick labels
func (a *verticalAxis) GlyphBoxes(*Plot) (boxes []GlyphBox) {
for _, t := range a.Tick.Marker.Ticks(a.Min, a.Max) {
if t.IsMinor() {
continue
}
h := a.Tick.Label.Height(t.Label)
box := GlyphBox{
Y: a.Norm(t.Value),
Rectangle: draw.Rectangle{draw.Point{Y: -h / 2}, draw.Point{Y: h / 2}},
}
boxes = append(boxes, box)
}
return
}
// DefaultTicks is suitable for the Tick.Marker field of an Axis,
// it returns a resonable default set of tick marks.
type DefaultTicks struct{}
var _ Ticker = DefaultTicks{}
// Ticks returns Ticks in a specified range
func (DefaultTicks) Ticks(min, max float64) (ticks []Tick) {
const SuggestedTicks = 3
if max < min {
panic("illegal range")
}
tens := math.Pow10(int(math.Floor(math.Log10(max - min))))
n := (max - min) / tens
for n < SuggestedTicks {
tens /= 10
n = (max - min) / tens
}
majorMult := int(n / SuggestedTicks)
switch majorMult {
case 7:
majorMult = 6
case 9:
majorMult = 8
}
majorDelta := float64(majorMult) * tens
val := math.Floor(min/majorDelta) * majorDelta
for val <= max {
if val >= min && val <= max {
ticks = append(ticks, Tick{Value: val, Label: fmt.Sprintf("%g", float32(val))})
}
if math.Nextafter(val, val+majorDelta) == val {
break
}
val += majorDelta
}
minorDelta := majorDelta / 2
switch majorMult {
case 3, 6:
minorDelta = majorDelta / 3
case 5:
minorDelta = majorDelta / 5
}
val = math.Floor(min/minorDelta) * minorDelta
for val <= max {
found := false
for _, t := range ticks {
if t.Value == val {
found = true
}
}
if val >= min && val <= max && !found {
ticks = append(ticks, Tick{Value: val})
}
if math.Nextafter(val, val+minorDelta) == val {
break
}
val += minorDelta
}
return
}
// LogTicks is suitable for the Tick.Marker field of an Axis,
// it returns tick marks suitable for a log-scale axis.
type LogTicks struct{}
var _ Ticker = LogTicks{}
// Ticks returns Ticks in a specified range
func (LogTicks) Ticks(min, max float64) []Tick {
var ticks []Tick
val := math.Pow10(int(math.Floor(math.Log10(min))))
if min <= 0 {
panic("Values must be greater than 0 for a log scale.")
}
for val < max*10 {
for i := 1; i < 10; i++ {
tick := Tick{Value: val * float64(i)}
if i == 1 {
tick.Label = fmt.Sprintf("%g", float32(val)*float32(i))
}
ticks = append(ticks, tick)
}
val *= 10
}
tick := Tick{Value: val, Label: fmt.Sprintf("%g", float32(val))}
ticks = append(ticks, tick)
return ticks
}
// ConstantTicks is suitable for the Tick.Marker field of an Axis.
// This function returns the given set of ticks.
type ConstantTicks []Tick
var _ Ticker = ConstantTicks{}
// Ticks returns Ticks in a specified range
func (ts ConstantTicks) Ticks(float64, float64) []Tick {
return ts
}
// A Tick is a single tick mark on an axis.
type Tick struct {
// Value is the data value marked by this Tick.
Value float64
// Label is the text to display at the tick mark.
// If Label is an empty string then this is a minor
// tick mark.
Label string
}
// IsMinor returns true if this is a minor tick mark.
func (t Tick) IsMinor() bool {
return t.Label == ""
}
// lengthOffset returns an offset that should be added to the
// tick mark's line to accout for its length. I.e., the start of
// the line for a minor tick mark must be shifted by half of
// the length.
func (t Tick) lengthOffset(len vg.Length) vg.Length {
if t.IsMinor() {
return len / 2
}
return 0
}
// tickLabelHeight returns height of the tick mark labels.
func tickLabelHeight(sty draw.TextStyle, ticks []Tick) vg.Length {
maxHeight := vg.Length(0)
for _, t := range ticks {
if t.IsMinor() {
continue
}
h := sty.Height(t.Label)
if h > maxHeight {
maxHeight = h
}
}
return maxHeight
}
// tickLabelWidth returns the width of the widest tick mark label.
func tickLabelWidth(sty draw.TextStyle, ticks []Tick) vg.Length {
maxWidth := vg.Length(0)
for _, t := range ticks {
if t.IsMinor() {
continue
}
w := sty.Width(t.Label)
if w > maxWidth {
maxWidth = w
}
}
return maxWidth
}
func log(x float64) float64 {
if x <= 0 {
panic("Values must be greater than 0 for a log scale.")
}
return math.Log(x)
}