diff options
Diffstat (limited to 'Godeps/_workspace/src/github.com/gizak/termui/sparkline.go')
-rw-r--r-- | Godeps/_workspace/src/github.com/gizak/termui/sparkline.go | 83 |
1 files changed, 47 insertions, 36 deletions
diff --git a/Godeps/_workspace/src/github.com/gizak/termui/sparkline.go b/Godeps/_workspace/src/github.com/gizak/termui/sparkline.go index c63a5857f..312ad9563 100644 --- a/Godeps/_workspace/src/github.com/gizak/termui/sparkline.go +++ b/Godeps/_workspace/src/github.com/gizak/termui/sparkline.go @@ -1,12 +1,10 @@ -// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved. +// Copyright 2016 Zack Guo <gizak@icloud.com>. All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. 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() @@ -49,8 +47,8 @@ func (s *Sparklines) Add(sl Sparkline) { func NewSparkline() Sparkline { return Sparkline{ Height: 1, - TitleColor: theme.SparklineTitle, - LineColor: theme.SparklineLine} + TitleColor: ThemeAttr("sparkline.title.fg"), + LineColor: ThemeAttr("sparkline.line.fg")} } // NewSparklines return a new *Spaklines with given Sparkline(s), you can always add a new Sparkline later. @@ -67,13 +65,13 @@ func (sl *Sparklines) update() { sl.Lines[i].displayHeight = v.Height + 1 } } - sl.displayWidth = sl.innerWidth + sl.displayWidth = sl.innerArea.Dx() // get how many lines gotta display h := 0 sl.displayLines = 0 for _, v := range sl.Lines { - if h+v.displayHeight <= sl.innerHeight { + if h+v.displayHeight <= sl.innerArea.Dy() { sl.displayLines++ } else { break @@ -84,20 +82,24 @@ 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 + } } } // Buffer implements Bufferer interface. -func (sl *Sparklines) Buffer() []Point { - ps := sl.Block.Buffer() +func (sl *Sparklines) Buffer() Buffer { + buf := sl.Block.Buffer() sl.update() oftY := 0 @@ -105,52 +107,61 @@ func (sl *Sparklines) Buffer() []Point { l := sl.Lines[i] data := l.Data - if len(data) > sl.innerWidth { - data = data[len(data)-sl.innerWidth:] + if len(data) > sl.innerArea.Dx() { + data = data[len(data)-sl.innerArea.Dx():] } if l.Title != "" { - rs := trimStr2Runes(l.Title, sl.innerWidth) + rs := trimStr2Runes(l.Title, sl.innerArea.Dx()) oftX := 0 for _, v := range rs { w := charWidth(v) - p := Point{} - p.Ch = v - p.Fg = l.TitleColor - p.Bg = sl.BgColor - p.X = sl.innerX + oftX - p.Y = sl.innerY + oftY - ps = append(ps, p) + c := Cell{ + Ch: v, + Fg: l.TitleColor, + Bg: sl.Bg, + } + x := sl.innerArea.Min.X + oftX + y := sl.innerArea.Min.Y + oftY + buf.Set(x, y, c) oftX += w } } 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++ { - p := Point{} - p.X = sl.innerX + j - p.Y = sl.innerY + oftY + l.Height - jj - p.Ch = ' ' // => sparks[7] - p.Bg = l.LineColor + c := Cell{ + Ch: ' ', // => sparks[7] + Bg: l.LineColor, + } + x := sl.innerArea.Min.X + j + y := sl.innerArea.Min.Y + oftY + l.Height - jj + //p.Bg = sl.BgColor - ps = append(ps, p) + buf.Set(x, y, c) } if barMod != 0 { - p := Point{} - p.X = sl.innerX + j - p.Y = sl.innerY + oftY + l.Height - barCnt - p.Ch = sparks[barMod-1] - p.Fg = l.LineColor - p.Bg = sl.BgColor - ps = append(ps, p) + c := Cell{ + Ch: sparks[barMod-1], + Fg: l.LineColor, + Bg: sl.Bg, + } + x := sl.innerArea.Min.X + j + y := sl.innerArea.Min.Y + oftY + l.Height - barCnt + buf.Set(x, y, c) } } oftY += l.displayHeight } - return sl.Block.chopOverflow(ps) + return buf } |