-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget_scatterplot.go
66 lines (57 loc) · 1.56 KB
/
widget_scatterplot.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
package tchart
import (
"errors"
"github.com/s-westphal/termui/v3"
"github.com/s-westphal/termui/v3/widgets"
)
// ScatterPlotWidget line chart widget
type ScatterPlotWidget struct {
ScatterPlot *widgets.Plot
storages []*Storage
width int
height int
}
// NewScatterPlotWidget create line chart widget
func NewScatterPlotWidget(title string, storages []*Storage) *ScatterPlotWidget {
scatterPlot := widgets.NewPlot()
scatterPlot.Title = title
scatterPlot.PlotType = widgets.ScatterPlot
scatterPlot.Marker = widgets.MarkerDot
// scatterPlot.DotMarkerRune = 'x'
sp := &ScatterPlotWidget{
ScatterPlot: scatterPlot,
storages: storages,
width: 0,
height: 0,
}
return sp
}
func (sp *ScatterPlotWidget) setDimension(x, y, w, h int) {
sp.width = w
sp.height = h
sp.ScatterPlot.SetRect(x, y, x+w, y+h)
}
// Buffer buffer for rendering
func (sp *ScatterPlotWidget) Buffer() (*termui.Buffer, error) {
if sp.storages[0].Data.length > 1 {
buffer := termui.NewBuffer(sp.ScatterPlot.GetRect())
sp.ScatterPlot.Lock()
sp.ScatterPlot.Draw(buffer)
sp.ScatterPlot.Unlock()
return buffer, nil
}
return nil, errors.New("not enough data in storage")
}
func (sp *ScatterPlotWidget) update() {
sp.ScatterPlot.Data = make([][]float64, len(sp.storages))
for i, storage := range sp.storages {
var ringBufferData = storage.Data.Tail(storage.Data.length)
var data = make([]float64, 0, len(ringBufferData))
for _, s := range ringBufferData {
data = append(data, s.(float64))
}
if len(data) > 0 {
sp.ScatterPlot.Data[i] = data
}
}
}