-
Notifications
You must be signed in to change notification settings - Fork 1
/
axis_box.go
66 lines (51 loc) · 1.11 KB
/
axis_box.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
package tplot
import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type AxisBox struct {
*tview.Box
axis *Axis
content Primitive
scale Scale
position Position
}
func NewAxisBox(axis *Axis, content Primitive) *AxisBox {
return &AxisBox{
Box: tview.NewBox(),
axis: axis,
content: content,
position: Left,
}
}
func (a *AxisBox) SetPosition(position Position) {
a.position = position
}
func (a *AxisBox) Position() Position {
return a.position
}
func (a *AxisBox) Draw(screen tcell.Screen) {
a.Box.DrawForSubclass(screen, a)
scale := a.content.Scale()
a.axis.SetScale(scale)
x, y, w, h := a.Box.GetInnerRect()
scale.SetSize(h)
axisW := a.axis.CalcWidth()
barsW := w - axisW
// Hide axis when there's no space
if barsW < 0 {
axisW = 0
barsW = w
}
// We need to draw the content first because the scale.Range
// might change.
if a.position == Right {
a.content.SetRect(x, y, barsW, h)
a.axis.SetRect(x+barsW, y, axisW, h)
} else {
a.content.SetRect(x+axisW, y, barsW, h)
a.axis.SetRect(x, y, axisW, h)
}
a.content.Draw(screen)
a.axis.Draw(screen)
}