From e0986b7e31433d33f02e3b84c764ef857d223886 Mon Sep 17 00:00:00 2001 From: sqshq Date: Sun, 17 Feb 2019 23:49:40 -0500 Subject: [PATCH] legend improvement: show +/- delta sign --- config.yml | 7 ++- widgets/barchart/barchart.go | 83 ++++++++++++++++++++++++++++++++++++ widgets/runchart/legend.go | 10 ++--- widgets/runchart/runchart.go | 10 +++++ 4 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 widgets/barchart/barchart.go diff --git a/config.yml b/config.yml index 97b7884..f6fe1a9 100644 --- a/config.yml +++ b/config.yml @@ -33,6 +33,7 @@ runcharts: - label: BING script: curl -o /dev/null -s -w '%{time_total}' https://www.bing.com - title: MONGO COLLECTIONS COUNT + precision: 0 position: w: 17 h: 14 @@ -40,8 +41,10 @@ runcharts: w: 17 h: 10 items: - - label: POSTS - script: mongo --quiet --host=localhost blog --eval "db.getCollection('post').find({}).size()" + - label: ACTIVE + script: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'ACTIVE'}).size()" + - label: INACTIVE + script: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).size()" asciiboxes: - title: COUNT position: diff --git a/widgets/barchart/barchart.go b/widgets/barchart/barchart.go new file mode 100644 index 0000000..40ab3db --- /dev/null +++ b/widgets/barchart/barchart.go @@ -0,0 +1,83 @@ +package barchart + +// +//import ( +// "fmt" +// "image" +//) +// +//type BarChart struct { +// Block +// BarColors []Color +// LabelStyles []Style +// NumStyles []Style // only Fg and Modifier are used +// NumFmt func(float64) string +// Data []float64 +// Labels []string +// BarWidth int +// BarGap int +// MaxVal float64 +//} +// +//func NewBarChart() *BarChart { +// return &BarChart{ +// Block: *NewBlock(), +// BarColors: Theme.BarChart.Bars, +// NumStyles: Theme.BarChart.Nums, +// LabelStyles: Theme.BarChart.Labels, +// NumFmt: func(n float64) string { return fmt.Sprint(n) }, +// BarGap: 1, +// BarWidth: 3, +// } +//} +// +//func (self *BarChart) Draw(buf *Buffer) { +// self.Block.Draw(buf) +// +// maxVal := self.MaxVal +// if maxVal == 0 { +// maxVal, _ = GetMaxFloat64FromSlice(self.Data) +// } +// +// barXCoordinate := self.Inner.Min.X +// +// for i, data := range self.Data { +// // draw bar +// height := int((data / maxVal) * float64(self.Inner.Dy()-1)) +// for x := barXCoordinate; x < MinInt(barXCoordinate+self.BarWidth, self.Inner.Max.X); x++ { +// for y := self.Inner.Max.Y - 2; y > (self.Inner.Max.Y-2)-height; y-- { +// c := NewCell(' ', NewStyle(ColorClear, SelectColor(self.BarColors, i))) +// buf.SetCell(c, image.Pt(x, y)) +// } +// } +// +// // draw label +// if i < len(self.Labels) { +// labelXCoordinate := barXCoordinate + +// int((float64(self.BarWidth) / 2)) - +// int((float64(rw.StringWidth(self.Labels[i])) / 2)) +// buf.SetString( +// self.Labels[i], +// SelectStyle(self.LabelStyles, i), +// image.Pt(labelXCoordinate, self.Inner.Max.Y-1), +// ) +// } +// +// // draw number +// numberXCoordinate := barXCoordinate + int((float64(self.BarWidth) / 2)) +// if numberXCoordinate <= self.Inner.Max.X { +// buf.SetString( +// self.NumFmt(data), +// NewStyle( +// SelectStyle(self.NumStyles, i+1).Fg, +// SelectColor(self.BarColors, i), +// SelectStyle(self.NumStyles, i+1).Modifier, +// ), +// image.Pt(numberXCoordinate, self.Inner.Max.Y-2), +// ) +// } +// +// barXCoordinate += (self.BarWidth + self.BarGap) +// } +//} +// diff --git a/widgets/runchart/legend.go b/widgets/runchart/legend.go index 639e34d..d643fa5 100644 --- a/widgets/runchart/legend.go +++ b/widgets/runchart/legend.go @@ -70,10 +70,10 @@ func (c *RunChart) renderLegend(buffer *ui.Buffer, rectangle image.Rectangle) { } details := [4]string{ - fmt.Sprintf("cur %s", formatValue(getCurrentValue(line), c.precision)), - fmt.Sprintf("dlt %s", formatValue(getDiffWithPreviousValue(line), c.precision)), - fmt.Sprintf("max %s", formatValue(line.extrema.max, c.precision)), - fmt.Sprintf("min %s", formatValue(line.extrema.min, c.precision)), + fmt.Sprintf("cur %s", formatValue(getCurrentValue(line), c.precision)), + fmt.Sprintf("dlt %s", formatValueWithSign(getDiffWithPreviousValue(line), c.precision)), + fmt.Sprintf("max %s", formatValue(line.extrema.max, c.precision)), + fmt.Sprintf("min %s", formatValue(line.extrema.min, c.precision)), } for i, detail := range details { @@ -102,7 +102,7 @@ func getDiffWithPreviousValue(line TimeLine) float64 { if len(line.points) < 2 { return 0 } else { - return math.Abs(line.points[len(line.points)-1].value - line.points[len(line.points)-2].value) + return line.points[len(line.points)-1].value - line.points[len(line.points)-2].value } } diff --git a/widgets/runchart/runchart.go b/widgets/runchart/runchart.go index da16e5a..02ed076 100644 --- a/widgets/runchart/runchart.go +++ b/widgets/runchart/runchart.go @@ -336,6 +336,16 @@ func formatValue(value float64, precision int) string { } } +func formatValueWithSign(value float64, precision int) string { + if value == 0 { + return " 0" + } else if value > 0 { + return "+" + formatValue(value, precision) + } else { + return formatValue(value, precision) + } +} + // time duration between grid lines func calculateTimescale(refreshRateMs int) time.Duration {