From 94c7d5011e2efce35dbab3dbbc1ce4df6c820ad3 Mon Sep 17 00:00:00 2001 From: sqshq Date: Sat, 16 Feb 2019 11:59:35 -0500 Subject: [PATCH] made chart labels/colors order reproducable, based on config --- data/consumer.go | 3 --- data/item.go | 8 ++++---- data/sampler.go | 1 - main.go | 1 + widgets/runchart/grid.go | 18 ++++++---------- widgets/runchart/runchart.go | 40 ++++++++++++++++++------------------ 6 files changed, 31 insertions(+), 40 deletions(-) diff --git a/data/consumer.go b/data/consumer.go index b665f5f..92d42fe 100644 --- a/data/consumer.go +++ b/data/consumer.go @@ -1,14 +1,11 @@ package data -import . "github.com/sqshq/termui" - type Consumer interface { ConsumeSample(sample Sample) } type Sample struct { Label string - Color Color Value string Error error } diff --git a/data/item.go b/data/item.go index c81dbbd..7145274 100644 --- a/data/item.go +++ b/data/item.go @@ -1,15 +1,15 @@ package data import ( - . "github.com/sqshq/termui" + ui "github.com/sqshq/termui" "os/exec" "strings" ) type Item struct { - Script string `yaml:"script"` - Label string `yaml:"label"` - Color Color `yaml:"color"` + Script string `yaml:"script"` + Label string `yaml:"label"` + Color ui.Color `yaml:"color"` } func (self *Item) nextValue() (value string, err error) { diff --git a/data/sampler.go b/data/sampler.go index 41d1097..8e8980a 100644 --- a/data/sampler.go +++ b/data/sampler.go @@ -29,7 +29,6 @@ func (self *Sampler) sample() { sample := Sample{ Value: value, Error: err, - Color: self.item.Color, Label: self.item.Label, } diff --git a/main.go b/main.go index 1c45065..900df15 100644 --- a/main.go +++ b/main.go @@ -28,6 +28,7 @@ func main() { layout.AddComponent(chart, c.Title, c.Position, c.Size, widgets.TypeRunChart) for _, item := range c.Items { + chart.AddLine(item.Label, item.Color) data.NewSampler(chart, item, c.RefreshRateMs) } } diff --git a/widgets/runchart/grid.go b/widgets/runchart/grid.go index 5ab8cf7..6ad1b9b 100644 --- a/widgets/runchart/grid.go +++ b/widgets/runchart/grid.go @@ -36,15 +36,13 @@ func (c *RunChart) renderAxes(buffer *ui.Buffer) { // draw origin cell buffer.SetCell( ui.NewCell(ui.BOTTOM_LEFT, ui.NewStyle(ui.ColorWhite)), - image.Pt(c.Inner.Min.X+c.grid.minTimeWidth, c.Inner.Max.Y-xAxisLabelsHeight-1), - ) + image.Pt(c.Inner.Min.X+c.grid.minTimeWidth, c.Inner.Max.Y-xAxisLabelsHeight-1)) // draw x axis line for i := c.grid.minTimeWidth + 1; i < c.Inner.Dx(); i++ { buffer.SetCell( ui.NewCell(ui.HORIZONTAL_DASH, ui.NewStyle(ui.ColorWhite)), - image.Pt(i+c.Inner.Min.X, c.Inner.Max.Y-xAxisLabelsHeight-1), - ) + image.Pt(i+c.Inner.Min.X, c.Inner.Max.Y-xAxisLabelsHeight-1)) } // draw grid lines @@ -52,8 +50,7 @@ func (c *RunChart) renderAxes(buffer *ui.Buffer) { for x := 1; x <= c.grid.linesCount; x++ { buffer.SetCell( ui.NewCell(ui.VERTICAL_DASH, ui.NewStyle(console.ColorDarkGrey)), - image.Pt(c.grid.maxTimeWidth-x*xAxisGridWidth, y+c.Inner.Min.Y+1), - ) + image.Pt(c.grid.maxTimeWidth-x*xAxisGridWidth, y+c.Inner.Min.Y+1)) } } @@ -61,8 +58,7 @@ func (c *RunChart) renderAxes(buffer *ui.Buffer) { for i := 0; i < c.Inner.Dy()-xAxisLabelsHeight-1; i++ { buffer.SetCell( ui.NewCell(ui.VERTICAL_DASH, ui.NewStyle(ui.ColorWhite)), - image.Pt(c.Inner.Min.X+c.grid.minTimeWidth, i+c.Inner.Min.Y), - ) + image.Pt(c.Inner.Min.X+c.grid.minTimeWidth, i+c.Inner.Min.Y)) } // draw x axis time labels @@ -71,8 +67,7 @@ func (c *RunChart) renderAxes(buffer *ui.Buffer) { buffer.SetString( labelTime.Format("15:04:05"), ui.NewStyle(ui.ColorWhite), - image.Pt(c.grid.maxTimeWidth-xAxisLabelsWidth/2-i*(xAxisGridWidth), c.Inner.Max.Y-1), - ) + image.Pt(c.grid.maxTimeWidth-xAxisLabelsWidth/2-i*(xAxisGridWidth), c.Inner.Max.Y-1)) } // draw y axis labels @@ -84,8 +79,7 @@ func (c *RunChart) renderAxes(buffer *ui.Buffer) { buffer.SetString( formatValue(value, c.precision), ui.NewStyle(ui.ColorWhite), - image.Pt(c.Inner.Min.X, 1+c.Inner.Min.Y+i*(yAxisLabelsIndent+yAxisLabelsHeight)), - ) + image.Pt(c.Inner.Min.X, 1+c.Inner.Min.Y+i*(yAxisLabelsIndent+yAxisLabelsHeight))) } } else { buffer.SetString( diff --git a/widgets/runchart/runchart.go b/widgets/runchart/runchart.go index 3dcf00f..c7477f1 100644 --- a/widgets/runchart/runchart.go +++ b/widgets/runchart/runchart.go @@ -111,6 +111,16 @@ func (c *RunChart) Draw(buffer *ui.Buffer) { c.mutex.Unlock() } +func (c *RunChart) AddLine(Label string, color ui.Color) { + line := TimeLine{ + points: []TimePoint{}, + color: color, + label: Label, + extrema: ValueExtrema{max: -math.MaxFloat64, min: math.MaxFloat64}, + } + c.lines = append(c.lines, line) +} + func (c *RunChart) ConsumeSample(sample data.Sample) { float, err := strconv.ParseFloat(sample.Value, 64) @@ -121,40 +131,30 @@ func (c *RunChart) ConsumeSample(sample data.Sample) { c.mutex.Lock() - lineIndex := -1 - + index := -1 for i, line := range c.lines { if line.label == sample.Label { - lineIndex = i + index = i } } - if lineIndex == -1 { - line := &TimeLine{ - points: []TimePoint{}, - color: sample.Color, - label: sample.Label, - extrema: ValueExtrema{max: float, min: float}, - } - c.lines = append(c.lines, *line) - lineIndex = len(c.lines) - 1 - } - - line := c.lines[lineIndex] + line := c.lines[index] if float < line.extrema.min { line.extrema.min = float } - if float > line.extrema.max { line.extrema.max = float } - timePoint := c.newTimePoint(float) - line.points = append(line.points, timePoint) - c.lines[lineIndex] = line + line.points = append(line.points, c.newTimePoint(float)) + c.lines[index] = line + + // perform cleanup once in a while + if len(line.points)%100 == 0 { + c.trimOutOfRangeValues() + } - c.trimOutOfRangeValues() c.mutex.Unlock() }