-
Notifications
You must be signed in to change notification settings - Fork 1
/
ticks.go
61 lines (47 loc) · 1.16 KB
/
ticks.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
package tplot
import (
"github.com/gdamore/tcell/v2"
)
type Ticks struct {
*base
}
var DefaultTicksRunes = []rune{'⎽', '⎼', '—', '⎻', '⎺'}
func NewTicks(factory DecimalFactory) *Ticks {
return &Ticks{
base: newBase(factory, DefaultTicksRunes),
}
}
func (b *Ticks) Draw(screen tcell.Screen) {
b.DrawForSubclass(screen, b)
data := b.DataSlice()
scale := b.scale
spacing := b.spacing
runes := b.runes
style := b.style
x, y, w, h := b.GetInnerRect()
if h == 0 || w == 0 {
return
}
// We are using special block characters to display quarters so we need
// to resize our scale after we've drawn the axis.
numFractions := len(b.runes)
if numFractions == 0 {
runes = []rune{'-'}
numFractions = 1
}
rng := b.calcRange(data)
scale.SetRange(rng)
// If we're sharing the scale with other components that can't use the
// fractions.
scale = scale.Copy()
scale.SetSize(h * numFractions)
for i, dec := range data {
v := scale.Value(dec)
xx := x + i*spacing + (w - len(data)*spacing)
fullSteps := v / numFractions
rem := v % numFractions
ch := runes[rem]
yy := y + h - fullSteps - 1
screen.SetContent(xx, yy, ch, nil, style)
}
}