-
Notifications
You must be signed in to change notification settings - Fork 51
/
sparkchart.go
313 lines (255 loc) · 6.39 KB
/
sparkchart.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
package clui
import (
"fmt"
// xs "github.com/huandu/xstrings"
term "github.com/nsf/termbox-go"
)
/*
SparkChart is a chart that represents a live data that
is continuously added to the chart. Or it can be static
element that displays predefined set of data - in this
case it looks like BarChart. At a moment SparkChart
keeps only the number of last data that is enough to
fill the control area. So, if you enlarge the control,
it will show partially filled area until it gets new data.
SparkChart displays vertical axis with values on the chart left
if ValueWidth greater than 0, horizontal axis with bar titles.
Maximum peaks(maximum of the data that control keeps)
can be hilited with different color.
By default the data is autoscaled to make the highest bar
fit the full height of the control. But it maybe useful
to disable autoscale and set the Top value to have more
handy diagram. E.g, for CPU load in % you can set
AutoScale to false and Top value to 100.
Note: negative and zero values are displayed as empty bar
*/
type SparkChart struct {
BaseControl
data []float64
valueWidth int
hiliteMax bool
maxFg, maxBg term.Attribute
topValue float64
autosize bool
}
/*
CreateSparkChart creates a new spark chart.
view - is a View that manages the control
parent - is container that keeps the control. The same View can be a view and a parent at the same time.
w and h - are minimal size of the control.
scale - the way of scaling the control when the parent is resized. Use DoNotScale constant if the
control should keep its original size.
*/
func CreateSparkChart(parent Control, w, h int, scale int) *SparkChart {
c := new(SparkChart)
c.BaseControl = NewBaseControl()
if w == AutoSize {
w = 10
}
if h == AutoSize {
h = 5
}
c.parent = parent
c.SetSize(w, h)
c.SetConstraints(w, h)
c.tabSkip = true
c.hiliteMax = true
c.autosize = true
c.data = make([]float64, 0)
c.SetScale(scale)
if parent != nil {
parent.AddChild(c)
}
return c
}
// Draw repaints the control on its View surface
func (b *SparkChart) Draw() {
if b.hidden {
return
}
b.mtx.RLock()
defer b.mtx.RUnlock()
PushAttributes()
defer PopAttributes()
fg, bg := RealColor(b.fg, b.Style(), ColorSparkChartText), RealColor(b.bg, b.Style(), ColorSparkChartBack)
SetTextColor(fg)
SetBackColor(bg)
FillRect(b.x, b.y, b.width, b.height, ' ')
if len(b.data) == 0 {
return
}
b.drawValues()
b.drawBars()
}
func (b *SparkChart) drawBars() {
if len(b.data) == 0 {
return
}
start, width := b.calculateBarArea()
if width < 2 {
return
}
coeff, max := b.calculateMultiplier()
if coeff == 0.0 {
return
}
PushAttributes()
defer PopAttributes()
h := b.height
pos := b.x + start
mxFg, mxBg := RealColor(b.maxFg, b.Style(), ColorSparkChartMaxText), RealColor(b.maxBg, b.Style(), ColorSparkChartMaxBack)
brFg, brBg := RealColor(b.fg, b.Style(), ColorSparkChartBarText), RealColor(b.bg, b.Style(), ColorSparkChartBarBack)
parts := []rune(SysObject(ObjSparkChart))
var dt []float64
if len(b.data) > width {
dt = b.data[len(b.data)-width:]
} else {
dt = b.data
}
for _, d := range dt {
barH := int(d * coeff)
if barH <= 0 {
pos++
continue
}
f, g := brFg, brBg
if b.hiliteMax && max == d {
f, g = mxFg, mxBg
}
SetTextColor(f)
SetBackColor(g)
FillRect(pos, b.y+h-barH, 1, barH, parts[0])
pos++
}
}
func (b *SparkChart) drawValues() {
if b.valueWidth <= 0 {
return
}
pos, _ := b.calculateBarArea()
if pos == 0 {
return
}
h := b.height
coeff, max := b.calculateMultiplier()
if max == coeff {
return
}
if !b.autosize || b.topValue == 0 {
max = b.topValue
}
dy := 0
format := fmt.Sprintf("%%%v.2f", b.valueWidth)
for dy < h-1 {
v := float64(h-dy) / float64(h) * max
s := fmt.Sprintf(format, v)
s = CutText(s, b.valueWidth)
DrawRawText(b.x, b.y+dy, s)
dy += 2
}
}
func (b *SparkChart) calculateBarArea() (int, int) {
w := b.width
pos := 0
if b.valueWidth < w/2 {
w = w - b.valueWidth
pos = b.valueWidth
}
return pos, w
}
func (b *SparkChart) calculateMultiplier() (float64, float64) {
if len(b.data) == 0 {
return 0, 0
}
h := b.height
if h <= 1 {
return 0, 0
}
max := b.data[0]
for _, val := range b.data {
if val > max {
max = val
}
}
if max == 0 {
return 0, 0
}
if b.autosize || b.topValue == 0 {
return float64(h) / max, max
}
return float64(h) / b.topValue, max
}
// AddData appends a new bar to a chart
func (b *SparkChart) AddData(val float64) {
b.mtx.Lock()
defer b.mtx.Unlock()
b.data = append(b.data, val)
_, width := b.calculateBarArea()
if len(b.data) > width {
b.data = b.data[len(b.data)-width:]
}
}
// ClearData removes all bar from chart
func (b *SparkChart) ClearData() {
b.mtx.Lock()
defer b.mtx.Unlock()
b.data = make([]float64, 0)
}
// SetData assigns a new bar list to a chart
func (b *SparkChart) SetData(data []float64) {
b.mtx.Lock()
defer b.mtx.Unlock()
b.data = make([]float64, len(data))
copy(b.data, data)
_, width := b.calculateBarArea()
if len(b.data) > width {
b.data = b.data[len(b.data)-width:]
}
}
// ValueWidth returns the width of the area at the left of
// chart used to draw values. Set it to 0 to turn off the
// value panel
func (b *SparkChart) ValueWidth() int {
return b.valueWidth
}
// SetValueWidth changes width of the value panel on the left
func (b *SparkChart) SetValueWidth(width int) {
b.mtx.Lock()
defer b.mtx.Unlock()
b.valueWidth = width
}
// Top returns the value of the top of a chart. The value is
// used only if autosize is off to scale all the data
func (b *SparkChart) Top() float64 {
return b.topValue
}
// SetTop sets the theoretical highest value of data flow
// to scale the chart
func (b *SparkChart) SetTop(top float64) {
b.mtx.Lock()
defer b.mtx.Unlock()
b.topValue = top
}
// AutoScale returns whether spark chart scales automatically
// depending on displayed data or it scales using Top value
func (b *SparkChart) AutoScale() bool {
return b.autosize
}
// SetAutoScale changes the way of scaling the data flow
func (b *SparkChart) SetAutoScale(auto bool) {
b.mtx.Lock()
defer b.mtx.Unlock()
b.autosize = auto
}
// HilitePeaks returns whether chart draws maximum peaks
// with different color
func (b *SparkChart) HilitePeaks() bool {
return b.hiliteMax
}
// SetHilitePeaks enables or disables hiliting maximum
// values with different colors
func (b *SparkChart) SetHilitePeaks(hilite bool) {
b.mtx.Lock()
defer b.mtx.Unlock()
b.hiliteMax = hilite
}