forked from VladimirMarkelov/clui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spark.go
80 lines (68 loc) · 1.74 KB
/
spark.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
package main
import (
ui "github.com/VladimirMarkelov/clui"
"math/rand"
"time"
)
func createView() *ui.SparkChart {
view := ui.AddWindow(0, 0, 10, 7, "BarChart Demo")
bch := ui.CreateSparkChart(view, 25, 12, 1)
bch.SetTop(20)
frmChk := ui.CreateFrame(view, 8, 5, ui.BorderNone, ui.Fixed)
frmChk.SetPack(ui.Vertical)
chkValues := ui.CreateCheckBox(frmChk, ui.AutoSize, "Show Values", ui.Fixed)
chkValues.SetState(0)
chkHilite := ui.CreateCheckBox(frmChk, ui.AutoSize, "Hilite peaks", ui.Fixed)
chkHilite.SetState(1)
chkAuto := ui.CreateCheckBox(frmChk, ui.AutoSize, "Auto scale", ui.Fixed)
chkAuto.SetState(1)
ui.ActivateControl(view, chkValues)
chkValues.OnChange(func(state int) {
if state == 0 {
bch.SetValueWidth(0)
} else if state == 1 {
bch.SetValueWidth(5)
}
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
})
chkHilite.OnChange(func(state int) {
if state == 0 {
bch.SetHilitePeaks(false)
} else if state == 1 {
bch.SetHilitePeaks(true)
}
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
})
chkAuto.OnChange(func(state int) {
if state == 0 {
bch.SetAutoScale(false)
} else if state == 1 {
bch.SetAutoScale(true)
}
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
})
return bch
}
func mainLoop() {
// Every application must create a single Composer and
// call its intialize method
ui.InitLibrary()
defer ui.DeinitLibrary()
b := createView()
b.SetData([]float64{1, 2, 3, 4, 5, 6, 6, 7, 5, 8, 9})
ticker := time.NewTicker(time.Millisecond * 200).C
go func() {
for {
select {
case <-ticker:
b.AddData(float64(rand.Int31n(20)))
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
}
}
}()
// start event processing loop - the main core of the library
ui.MainLoop()
}
func main() {
mainLoop()
}