From 08a5d3f67b7d9ec87830ea39c48e570a1f18531f Mon Sep 17 00:00:00 2001 From: gizak Date: Wed, 27 Jan 2016 14:12:43 -0500 Subject: [PATCH] Fix https://github.com/gizak/termui/issues/75 Negative numbers are suppressed as zeros. The height of each data point is based off zero, so it will be confusing when it comes to negative numbers. Simply treat negative values data points as zeroes. --- sparkline.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/sparkline.go b/sparkline.go index b438941..312ad95 100644 --- a/sparkline.go +++ b/sparkline.go @@ -4,9 +4,7 @@ package termui -import "math" - -// Sparkline is like: ▅▆▂▂▅▇▂▂▃▆▆▆▅▃ +// Sparkline is like: ▅▆▂▂▅▇▂▂▃▆▆▆▅▃. The data points should be non-negative integers. /* data := []int{4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1} spl := termui.NewSparkline() @@ -84,14 +82,18 @@ func (sl *Sparklines) update() { for i := 0; i < sl.displayLines; i++ { data := sl.Lines[i].Data - max := math.MinInt32 + max := 0 for _, v := range data { if max < v { max = v } } sl.Lines[i].max = max - sl.Lines[i].scale = float32(8*sl.Lines[i].Height) / float32(max) + if max != 0 { + sl.Lines[i].scale = float32(8*sl.Lines[i].Height) / float32(max) + } else { // when all negative + sl.Lines[i].scale = 0 + } } } @@ -127,7 +129,12 @@ func (sl *Sparklines) Buffer() Buffer { } for j, v := range data { + // display height of the data point, zero when data is negative h := int(float32(v)*l.scale + 0.5) + if v < 0 { + h = 0 + } + barCnt := h / 8 barMod := h % 8 for jj := 0; jj < barCnt; jj++ {