-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget_pie.go
64 lines (55 loc) · 1.4 KB
/
widget_pie.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
package tchart
import (
"errors"
"fmt"
"math"
"github.com/s-westphal/termui/v3"
ui "github.com/s-westphal/termui/v3"
"github.com/s-westphal/termui/v3/widgets"
)
// PieChartWidget line chart widget
type PieChartWidget struct {
PieChart *widgets.PieChart
storages []*Storage
width int
height int
}
// NewPieChartWidget create line chart widget
func NewPieChartWidget(title string, storages []*Storage, colors []ui.Color) *PieChartWidget {
pieChart := widgets.NewPieChart()
pieChart.Title = title
pieChart.Colors = colors
pieChart.AngleOffset = -.5 * math.Pi
pieChart.LabelFormatter = func(i int, v float64) string {
return fmt.Sprintf("%.02f", v)
}
pc := &PieChartWidget{
PieChart: pieChart,
storages: storages,
width: 0,
height: 0,
}
return pc
}
func (pc *PieChartWidget) setDimension(x, y, w, h int) {
pc.width = w
pc.height = h
pc.PieChart.SetRect(x, y, x+w, y+h)
}
// Buffer buffer for rendering
func (pc *PieChartWidget) Buffer() (*termui.Buffer, error) {
if pc.storages[0].Data.length > 0 {
buffer := termui.NewBuffer(pc.PieChart.GetRect())
pc.PieChart.Lock()
pc.PieChart.Draw(buffer)
pc.PieChart.Unlock()
return buffer, nil
}
return nil, errors.New("not enough data in storage")
}
func (pc *PieChartWidget) update() {
pc.PieChart.Data = make([]float64, len(pc.storages))
for i, storage := range pc.storages {
pc.PieChart.Data[i] = MaxFloat64(0, storage.Sum)
}
}