forked from atsaki/termeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinechart.go
82 lines (70 loc) · 1.78 KB
/
linechart.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
package termeter
import "gopkg.in/gizak/termui.v1"
type LineChartWidget struct {
Data *float64RingBuffer
DataLabels *stringRingBuffer
Mode string
LineChart *termui.LineChart
}
func NewLineChartWidget(bufsize int) *LineChartWidget {
lc := &LineChartWidget{
Data: newFloat64RingBuffer(bufsize),
DataLabels: newStringRingBuffer(bufsize),
LineChart: termui.NewLineChart(),
}
return lc
}
func (lc *LineChartWidget) SetX(x int) {
lc.LineChart.X = x
}
func (lc *LineChartWidget) SetY(y int) {
lc.LineChart.Y = y
}
func (lc *LineChartWidget) SetWidth(w int) {
lc.LineChart.Width = w
lc.updateLineChartData()
}
func (lc *LineChartWidget) SetHeight(h int) {
lc.LineChart.Height = h
}
func (lc *LineChartWidget) Buffer() []termui.Point {
defer func() {
recover()
}()
return lc.LineChart.Buffer()
}
func (lc *LineChartWidget) Add(x float64, dataLabel string) {
lc.Data.Add(x)
if dataLabel != "" {
lc.DataLabels.Add(dataLabel)
}
lc.updateLineChartData()
}
func (lc *LineChartWidget) Update(xs []float64, dataLabels []string) {
lc.Data = newFloat64RingBuffer(lc.Data.Capacity())
if xs != nil {
for _, x := range xs {
lc.Data.Add(x)
}
}
lc.DataLabels = newStringRingBuffer(lc.DataLabels.Capacity())
if dataLabels != nil {
for _, dataLabel := range dataLabels {
lc.DataLabels.Add(dataLabel)
}
}
lc.updateLineChartData()
}
func (lc *LineChartWidget) Clear() {
lc.Update(nil, nil)
}
func (lc *LineChartWidget) updateLineChartData() {
lc.LineChart.Mode = lc.Mode
if lc.Mode == "dot" {
lc.LineChart.Data = lc.Data.Last(lc.LineChart.Width)
lc.LineChart.DataLabels = lc.DataLabels.Last(lc.LineChart.Width)
} else {
lc.LineChart.Data = lc.Data.Last(2 * lc.LineChart.Width)
lc.LineChart.DataLabels = lc.DataLabels.Last(2 * lc.LineChart.Width)
}
}