-
Notifications
You must be signed in to change notification settings - Fork 785
/
piechart.go
74 lines (64 loc) · 1.25 KB
/
piechart.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
// +build ignore
package main
import (
"fmt"
"log"
"math"
"math/rand"
"time"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)
var run = true
func main() {
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()
rand.Seed(time.Now().UTC().UnixNano())
randomDataAndOffset := func() (data []float64, offset float64) {
noSlices := 1 + rand.Intn(5)
data = make([]float64, noSlices)
for i := range data {
data[i] = rand.Float64()
}
offset = 2.0 * math.Pi * rand.Float64()
return
}
pc := widgets.NewPieChart()
pc.Title = "Pie Chart"
pc.SetRect(5, 5, 70, 36)
pc.Data = []float64{.25, .25, .25, .25}
pc.AngleOffset = -.5 * math.Pi
pc.LabelFormatter = func(i int, v float64) string {
return fmt.Sprintf("%.02f", v)
}
pause := func() {
run = !run
if run {
pc.Title = "Pie Chart"
} else {
pc.Title = "Pie Chart (Stopped)"
}
ui.Render(pc)
}
ui.Render(pc)
uiEvents := ui.PollEvents()
ticker := time.NewTicker(time.Second).C
for {
select {
case e := <-uiEvents:
switch e.ID {
case "q", "<C-c>":
return
case "s":
pause()
}
case <-ticker:
if run {
pc.Data, pc.AngleOffset = randomDataAndOffset()
ui.Render(pc)
}
}
}
}