forked from VladimirMarkelov/clui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
barchart.go
157 lines (145 loc) · 3.85 KB
/
barchart.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
package main
import (
ui "github.com/VladimirMarkelov/clui"
)
func customColored(d *ui.BarDataCell) {
part := d.TotalMax / 3
if d.ID%2 == 0 {
if d.Value <= part {
d.Fg = ui.ColorGreen
} else if d.Value > 2*part {
d.Fg = ui.ColorRed
} else {
d.Fg = ui.ColorBlue
}
} else {
d.Ch = '#'
if d.Value <= part {
d.Fg = ui.ColorGreenBold
} else if d.Value > 2*part {
d.Fg = ui.ColorRedBold
} else {
d.Fg = ui.ColorBlueBold
}
}
}
func createView() *ui.BarChart {
view := ui.AddWindow(0, 0, 10, 7, "BarChart Demo")
bch := ui.CreateBarChart(view, 40, 12, 1)
frmChk := ui.CreateFrame(view, 8, 5, ui.BorderNone, ui.Fixed)
frmChk.SetPack(ui.Vertical)
chkTitles := ui.CreateCheckBox(frmChk, ui.AutoSize, "Show Titles", ui.Fixed)
chkMarks := ui.CreateCheckBox(frmChk, ui.AutoSize, "Show Marks", ui.Fixed)
chkTitles.SetState(1)
chkLegend := ui.CreateCheckBox(frmChk, ui.AutoSize, "Show Legend", ui.Fixed)
chkValues := ui.CreateCheckBox(frmChk, ui.AutoSize, "Show Values", ui.Fixed)
chkValues.SetState(1)
chkFixed := ui.CreateCheckBox(frmChk, ui.AutoSize, "Fixed Width", ui.Fixed)
chkGap := ui.CreateCheckBox(frmChk, ui.AutoSize, "No Gap", ui.Fixed)
chkMulti := ui.CreateCheckBox(frmChk, ui.AutoSize, "MultiColored", ui.Fixed)
chkCustom := ui.CreateCheckBox(frmChk, ui.AutoSize, "Custom Colors", ui.Fixed)
ui.ActivateControl(view, chkTitles)
chkTitles.OnChange(func(state int) {
if state == 0 {
chkMarks.SetEnabled(false)
bch.SetShowTitles(false)
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
} else if state == 1 {
chkMarks.SetEnabled(true)
bch.SetShowTitles(true)
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
}
})
chkMarks.OnChange(func(state int) {
if state == 0 {
bch.SetShowMarks(false)
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
} else if state == 1 {
bch.SetShowMarks(true)
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
}
})
chkLegend.OnChange(func(state int) {
if state == 0 {
bch.SetLegendWidth(0)
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
} else if state == 1 {
bch.SetLegendWidth(10)
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
}
})
chkValues.OnChange(func(state int) {
if state == 0 {
bch.SetValueWidth(0)
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
} else if state == 1 {
bch.SetValueWidth(5)
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
}
})
chkMulti.OnChange(func(state int) {
if state == 0 {
d := []ui.BarData{
{Value: 80, Title: "80%"},
{Value: 50, Title: "50%"},
{Value: 150, Title: ">100%"},
}
bch.SetData(d)
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
} else if state == 1 {
d := []ui.BarData{
{Value: 80, Title: "80%", Fg: ui.ColorBlue},
{Value: 50, Title: "50%", Fg: ui.ColorGreen, Ch: 'X'},
{Value: 150, Title: ">100%", Fg: ui.ColorYellow},
}
bch.SetData(d)
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
}
})
chkFixed.OnChange(func(state int) {
if state == 0 {
bch.SetAutoSize(true)
} else if state == 1 {
bch.SetAutoSize(false)
}
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
})
chkGap.OnChange(func(state int) {
if state == 1 {
bch.SetBarGap(0)
} else if state == 0 {
bch.SetBarGap(1)
}
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
})
chkCustom.OnChange(func(state int) {
if state == 0 {
bch.OnDrawCell(nil)
} else if state == 1 {
bch.OnDrawCell(customColored)
}
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.SetBarGap(1)
d := []ui.BarData{
{Value: 80, Title: "80%"},
{Value: 50, Title: "50%"},
{Value: 150, Title: ">100%"},
}
b.SetData(d)
b.SetValueWidth(5)
b.SetAutoSize(true)
// start event processing loop - the main core of the library
ui.MainLoop()
}
func main() {
mainLoop()
}