legend improvement: show +/- delta sign

This commit is contained in:
sqshq 2019-02-17 23:49:40 -05:00
parent 8bdaccf58f
commit e0986b7e31
4 changed files with 103 additions and 7 deletions

View File

@ -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:

View File

@ -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)
// }
//}
//

View File

@ -71,7 +71,7 @@ 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("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)),
}
@ -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
}
}

View File

@ -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 {