diff options
author | Sean E. Russell <ser@ser1.net> | 2020-05-31 15:48:14 -0500 |
---|---|---|
committer | Sean E. Russell <ser@ser1.net> | 2020-05-31 16:09:51 -0500 |
commit | 44b5d2d6eca94aa2329f07e2e5525577344ece7d (patch) | |
tree | 68c9b0b4f4c1a55f5d910b4d6b2e50d10a99c1b4 | |
parent | 5123568b923991f9fba318954ea3e8cf6785e252 (diff) |
Fixes #128 for LineGraph; there's probably another leak in Sparkline
Fixes #128 for Sparkline. Also, the amount of data held is tiny so bumped the buffer to 4x to reduce collections.
Minor comment change
-rw-r--r-- | termui/linegraph.go | 17 | ||||
-rw-r--r-- | termui/sparkline.go | 4 |
2 files changed, 18 insertions, 3 deletions
diff --git a/termui/linegraph.go b/termui/linegraph.go index 55aacbd..db15822 100644 --- a/termui/linegraph.go +++ b/termui/linegraph.go @@ -8,11 +8,18 @@ import ( drawille "github.com/xxxserxxx/gotop/v4/termui/drawille-go" ) -// LineGraph implements a line graph of data points. +// LineGraph draws a graph like this ⣀⡠⠤⠔⣁ of data points. type LineGraph struct { *Block - Data map[string][]float64 + // Data is a size-managed data set for the graph. Each entry is a line; + // each sub-array are points in the line. The maximum size of the + // sub-arrays is controlled by the size of the canvas. This + // array is **not** thread-safe. Do not modify this array, or it's + // sub-arrays in threads different than the thread that calls `Draw()` + Data map[string][]float64 + // The labels drawn on the graph for each of the lines; the key is shared + // by Data; the value is the text that will be rendered. Labels map[string]string HorizontalScale int @@ -67,8 +74,9 @@ func (self *LineGraph) Draw(buf *Buffer) { // coordinates of last point lastY, lastX := -1, -1 // assign colors to `colors` and lines/points to the canvas + dx := self.Inner.Dx() for i := len(seriesData) - 1; i >= 0; i-- { - x := ((self.Inner.Dx() + 1) * 2) - 1 - (((len(seriesData) - 1) - i) * self.HorizontalScale) + x := ((dx + 1) * 2) - 1 - (((len(seriesData) - 1) - i) * self.HorizontalScale) y := ((self.Inner.Dy() + 1) * 4) - 1 - int((float64((self.Inner.Dy())*4)-1)*(seriesData[i]/100)) if x < 0 { // render the line to the last point up to the wall @@ -80,6 +88,9 @@ func (self *LineGraph) Draw(buf *Buffer) { } } } + if len(seriesData) > 4*dx { + self.Data[seriesName] = seriesData[dx-1:] + } break } if lastY == -1 { // if this is the first point diff --git a/termui/sparkline.go b/termui/sparkline.go index 2f27d9e..3b3ad75 100644 --- a/termui/sparkline.go +++ b/termui/sparkline.go @@ -98,5 +98,9 @@ func (self *SparklineGroup) Draw(buf *Buffer) { image.Pt(self.Inner.Min.X+x-1, self.Inner.Min.Y+sparkY-1), ) } + dx := self.Inner.Dx() + if len(line.Data) > 4*dx { + line.Data = line.Data[dx-1:] + } } } |