forked from jeremija/tplot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bars.go
70 lines (53 loc) · 1.29 KB
/
bars.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
package tplot
import (
"github.com/gdamore/tcell/v2"
)
type Bars struct {
*base
}
var DefaultBarsRunes = []rune{'▃', '▄', '▆', '█'}
func NewBars(factory DecimalFactory) *Bars {
return &Bars{
base: newBase(factory, DefaultBarsRunes),
}
}
func (b *Bars) 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
}
if len(runes) == 0 {
runes = []rune{'█'}
}
// We are using special block characters to display quarters so we need
// to resize our scale after we've drawn the axis.
numFractions := len(runes)
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
fullBlock := runes[len(runes)-1]
for j := 1; j <= fullSteps; j++ {
yy := y + h - j
screen.SetContent(xx, yy, fullBlock, nil, style)
}
if rem > 0 {
ch := runes[rem-1]
yy := y + h - fullSteps - 1
screen.SetContent(xx, yy, ch, nil, style)
}
}
}