-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
sparkline.go
176 lines (144 loc) · 3.33 KB
/
sparkline.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
package tchart
import (
"fmt"
"image"
"math"
"github.com/derailed/tcell/v2"
"github.com/derailed/tview"
)
var sparks = []rune{'▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'}
type block struct {
full int
partial rune
}
type blocks struct {
s1, s2 block
}
// Metric tracks two series.
type Metric struct {
S1, S2 int64
}
// MaxDigits returns the max series number of digits.
func (m Metric) MaxDigits() int {
s := fmt.Sprintf("%d", m.Max())
return len(s)
}
// Max returns the max of the series.
func (m Metric) Max() int64 {
return int64(math.Max(float64(m.S1), float64(m.S2)))
}
// Sum returns the sum of series.
func (m Metric) Sum() int64 {
return m.S1 + m.S2
}
// SparkLine represents a sparkline component.
type SparkLine struct {
*Component
data []Metric
multiSeries bool
}
// NewSparkLine returns a new graph.
func NewSparkLine(id string) *SparkLine {
return &SparkLine{
Component: NewComponent(id),
multiSeries: true,
}
}
// SetMultiSeries indicates if multi series are in effect or not.
func (s *SparkLine) SetMultiSeries(b bool) {
s.multiSeries = b
}
// Add adds a metric.
func (s *SparkLine) Add(m Metric) {
s.mx.Lock()
defer s.mx.Unlock()
s.data = append(s.data, m)
}
// Draw draws the graph.
func (s *SparkLine) Draw(screen tcell.Screen) {
s.Component.Draw(screen)
s.mx.RLock()
defer s.mx.RUnlock()
if len(s.data) == 0 {
return
}
pad := 0
if s.legend != "" {
pad++
}
rect := s.asRect()
s.cutSet(rect.Dx())
max := s.computeMax()
cX, idx := rect.Min.X+1, 0
if len(s.data)*2 < rect.Dx() {
cX = rect.Max.X - len(s.data)*2
} else {
idx = len(s.data) - rect.Dx()/2
}
scale := float64(len(sparks)*(rect.Dy()-pad)) / float64(max)
c1, c2 := s.colorForSeries()
for _, d := range s.data[idx:] {
b := toBlocks(d, scale)
cY := rect.Max.Y - pad
s.drawBlock(rect, screen, cX, cY, b.s1, c1)
cX++
s.drawBlock(rect, screen, cX, cY, b.s2, c2)
cX++
}
if rect.Dx() > 0 && rect.Dy() > 0 && s.legend != "" {
legend := s.legend
if s.HasFocus() {
legend = fmt.Sprintf("[%s:%s:]", s.focusFgColor, s.focusBgColor) + s.legend + "[::]"
}
tview.Print(screen, legend, rect.Min.X, rect.Max.Y, rect.Dx(), tview.AlignCenter, tcell.ColorWhite)
}
}
func (s *SparkLine) drawBlock(r image.Rectangle, screen tcell.Screen, x, y int, b block, c tcell.Color) {
style := tcell.StyleDefault.Foreground(c).Background(s.bgColor)
zeroY := r.Max.Y - r.Dy()
for i := 0; i < b.full; i++ {
screen.SetContent(x, y, sparks[len(sparks)-1], nil, style)
y--
if y <= zeroY {
break
}
}
if b.partial != 0 {
screen.SetContent(x, y, b.partial, nil, style)
}
}
func (s *SparkLine) cutSet(width int) {
if width <= 0 || len(s.data) == 0 {
return
}
if len(s.data) >= width*2 {
s.data = s.data[len(s.data)-width:]
}
}
func (s *SparkLine) computeMax() int64 {
var max int64
for _, d := range s.data {
m := d.Max()
if max < m {
max = m
}
}
return max
}
func toBlocks(m Metric, scale float64) blocks {
if m.Sum() <= 0 {
return blocks{}
}
return blocks{s1: makeBlocks(m.S1, scale), s2: makeBlocks(m.S2, scale)}
}
func makeBlocks(v int64, scale float64) block {
scaled := int(math.Round(float64(v) * scale))
p, b := scaled%len(sparks), block{full: scaled / len(sparks)}
if b.full == 0 && v > 0 && p == 0 {
p = 4
}
if v > 0 && p >= 0 && p < len(sparks) {
b.partial = sparks[p]
}
return b
}