made chart labels/colors order reproducable, based on config

This commit is contained in:
sqshq 2019-02-16 11:59:35 -05:00
parent 83ab5d6e64
commit 94c7d5011e
6 changed files with 31 additions and 40 deletions

View File

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

View File

@ -1,7 +1,7 @@
package data
import (
. "github.com/sqshq/termui"
ui "github.com/sqshq/termui"
"os/exec"
"strings"
)
@ -9,7 +9,7 @@ import (
type Item struct {
Script string `yaml:"script"`
Label string `yaml:"label"`
Color Color `yaml:"color"`
Color ui.Color `yaml:"color"`
}
func (self *Item) nextValue() (value string, err error) {

View File

@ -29,7 +29,6 @@ func (self *Sampler) sample() {
sample := Sample{
Value: value,
Error: err,
Color: self.item.Color,
Label: self.item.Label,
}

View File

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

View File

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

View File

@ -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.mutex.Unlock()
}