-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget_barchart.go
62 lines (55 loc) · 1.6 KB
/
widget_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
package tchart
import (
"fmt"
"math"
"github.com/s-westphal/termui/v3"
ui "github.com/s-westphal/termui/v3"
"github.com/s-westphal/termui/v3/widgets"
)
type barChartWidget struct {
barChart *widgets.BarChart
storage *Storage
numBins int
}
func NewBarChartWidget(title string, storage *Storage, numBins int) *barChartWidget {
bc := widgets.NewBarChart()
bc.Title = title
bc.BarColors = []ui.Color{ui.ColorWhite}
bc.LabelStyles = []ui.Style{ui.NewStyle(ui.ColorGreen), ui.NewStyle(ui.ColorYellow)}
bc.NumStyles = []ui.Style{ui.NewStyle(ui.ColorBlack)}
bc.BarWidth = 3
bc.Border = false
return &barChartWidget{
barChart: bc,
storage: storage,
numBins: numBins,
}
}
func (bc *barChartWidget) Buffer() (*termui.Buffer, error) {
buffer := termui.NewBuffer(bc.barChart.GetRect())
bc.barChart.Lock()
bc.barChart.Draw(buffer)
bc.barChart.Unlock()
return buffer, nil
}
func (bc *barChartWidget) setDimension(x, y, w, h int) {
bc.barChart.SetRect(x, y, x+w, y+h)
}
func (bc *barChartWidget) update() {
if bc.storage.Data.Len() == 0 {
// Histogram does not work for empty data
return
}
stepsize := (bc.storage.Max - bc.storage.Min) / float64(bc.numBins)
data := make([]float64, bc.numBins)
labels := make([]string, bc.numBins)
var prevValue float64 = 0
for i := 0; i < bc.numBins; i++ {
cummulativeValue := bc.storage.Histogram.CDF(bc.storage.Min + stepsize*float64(i+1))
data[i] = math.Round((cummulativeValue - prevValue) * 100)
prevValue = cummulativeValue
labels[i] = fmt.Sprintf("%.1f", bc.storage.Min+stepsize*(float64(i)+0.5))
}
bc.barChart.Labels = labels
bc.barChart.Data = data
}