termui/widget/sparkline.go

159 lines
3.4 KiB
Go
Raw Normal View History

// +build ignore
2015-03-20 14:21:50 -06:00
// Copyright 2015 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.
2015-02-05 00:09:20 -07:00
package termui
import "math"
2015-03-24 15:16:43 -06:00
// Sparkline is like: ▅▆▂▂▅▇▂▂▃▆▆▆▅▃
/*
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()
spl.Data = data
spl.Title = "Sparkline 0"
spl.LineColor = termui.ColorGreen
*/
2015-02-05 00:09:20 -07:00
type Sparkline struct {
Data []int
Height int
Title string
TitleColor Attribute
LineColor Attribute
displayHeight int
scale float32
max int
}
2015-03-24 15:16:43 -06:00
// Sparklines is a renderable widget which groups together the given sparklines.
/*
spls := termui.NewSparklines(spl0,spl1,spl2) //...
spls.Height = 2
spls.Width = 20
*/
2015-02-05 00:09:20 -07:00
type Sparklines struct {
Block
Lines []Sparkline
displayLines int
displayWidth int
}
var sparks = []rune{'▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'}
2015-03-24 15:16:43 -06:00
// Add appends a given Sparkline to s *Sparklines.
2015-02-05 00:09:20 -07:00
func (s *Sparklines) Add(sl Sparkline) {
s.Lines = append(s.Lines, sl)
}
2015-03-24 15:16:43 -06:00
// NewSparkline returns a unrenderable single sparkline that intended to be added into Sparklines.
func NewSparkline() Sparkline {
return Sparkline{
Height: 1,
TitleColor: theme.SparklineTitle,
LineColor: theme.SparklineLine}
}
2015-03-24 15:16:43 -06:00
// NewSparklines return a new *Spaklines with given Sparkline(s), you can always add a new Sparkline later.
2015-02-05 00:09:20 -07:00
func NewSparklines(ss ...Sparkline) *Sparklines {
s := &Sparklines{Block: *NewBlock(), Lines: ss}
return s
}
func (sl *Sparklines) update() {
for i, v := range sl.Lines {
if v.Title == "" {
sl.Lines[i].displayHeight = v.Height
} else {
sl.Lines[i].displayHeight = v.Height + 1
}
}
sl.displayWidth = sl.innerWidth
// get how many lines gotta display
h := 0
2015-02-05 09:03:32 -07:00
sl.displayLines = 0
2015-02-05 00:09:20 -07:00
for _, v := range sl.Lines {
if h+v.displayHeight <= sl.innerHeight {
sl.displayLines++
} else {
break
}
h += v.displayHeight
}
for i := 0; i < sl.displayLines; i++ {
data := sl.Lines[i].Data
max := math.MinInt32
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)
}
}
2015-03-24 15:16:43 -06:00
// Buffer implements Bufferer interface.
2015-02-05 00:09:20 -07:00
func (sl *Sparklines) Buffer() []Point {
ps := sl.Block.Buffer()
sl.update()
oftY := 0
for i := 0; i < sl.displayLines; i++ {
l := sl.Lines[i]
data := l.Data
if len(data) > sl.innerWidth {
data = data[len(data)-sl.innerWidth:]
2015-02-05 00:09:20 -07:00
}
if l.Title != "" {
rs := trimStr2Runes(l.Title, sl.innerWidth)
oftX := 0
for _, v := range rs {
w := charWidth(v)
2015-02-05 00:09:20 -07:00
p := Point{}
2015-03-03 11:28:09 -07:00
p.Ch = v
p.Fg = l.TitleColor
p.Bg = sl.BgColor
2015-02-05 09:03:32 -07:00
p.X = sl.innerX + oftX
p.Y = sl.innerY + oftY
2015-02-05 00:09:20 -07:00
ps = append(ps, p)
oftX += w
2015-02-05 00:09:20 -07:00
}
}
for j, v := range data {
h := int(float32(v)*l.scale + 0.5)
2015-02-05 00:09:20 -07:00
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
//p.Bg = sl.BgColor
2015-02-05 00:09:20 -07:00
ps = append(ps, p)
}
if barMod != 0 {
p := Point{}
p.X = sl.innerX + j
p.Y = sl.innerY + oftY + l.Height - barCnt
2015-03-03 11:28:09 -07:00
p.Ch = sparks[barMod-1]
p.Fg = l.LineColor
p.Bg = sl.BgColor
2015-02-05 00:09:20 -07:00
ps = append(ps, p)
}
}
oftY += l.displayHeight
}
return sl.Block.chopOverflow(ps)
2015-02-05 00:09:20 -07:00
}