clui/demos/spark/spark.go

81 lines
1.7 KiB
Go
Raw Normal View History

package main
2015-12-11 15:33:29 -08:00
import (
2017-04-06 10:40:49 -07:00
ui "github.com/VladimirMarkelov/clui"
2015-12-11 15:33:29 -08:00
"math/rand"
"time"
)
func createView() *ui.SparkChart {
2015-12-11 15:33:29 -08:00
view := ui.AddWindow(0, 0, 10, 7, "BarChart Demo")
bch := ui.CreateSparkChart(view, 25, 12, 1)
2015-12-11 15:33:29 -08:00
bch.SetTop(20)
frmChk := ui.CreateFrame(view, 8, 5, ui.BorderNone, ui.Fixed)
2015-12-11 15:33:29 -08:00
frmChk.SetPack(ui.Vertical)
chkValues := ui.CreateCheckBox(frmChk, ui.AutoSize, "Show Values", ui.Fixed)
2015-12-11 15:33:29 -08:00
chkValues.SetState(0)
chkHilite := ui.CreateCheckBox(frmChk, ui.AutoSize, "Hilite peaks", ui.Fixed)
2015-12-11 15:33:29 -08:00
chkHilite.SetState(1)
chkAuto := ui.CreateCheckBox(frmChk, ui.AutoSize, "Auto scale", ui.Fixed)
2015-12-11 15:33:29 -08:00
chkAuto.SetState(1)
ui.ActivateControl(view, chkValues)
2015-12-11 15:33:29 -08:00
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})
2015-12-11 15:33:29 -08:00
})
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})
2015-12-11 15:33:29 -08:00
})
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})
2015-12-11 15:33:29 -08:00
})
return bch
}
func mainLoop() {
// Every application must create a single Composer and
// call its intialize method
ui.InitLibrary()
defer ui.DeinitLibrary()
2015-12-11 15:33:29 -08:00
b := createView()
2015-12-11 15:33:29 -08:00
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})
2015-12-11 15:33:29 -08:00
}
}
}()
// start event processing loop - the main core of the library
ui.MainLoop()
2015-12-11 15:33:29 -08:00
}
func main() {
mainLoop()
}