Merge branch 'leighmcculloch-barchart-cell-char'

This commit is contained in:
Zack Guo 2016-08-13 22:39:28 -04:00
commit 23b25db083
1 changed files with 20 additions and 9 deletions

View File

@ -29,6 +29,7 @@ type BarChart struct {
DataLabels []string DataLabels []string
BarWidth int BarWidth int
BarGap int BarGap int
CellChar rune
labels [][]rune labels [][]rune
dataNum [][]rune dataNum [][]rune
numBar int numBar int
@ -44,6 +45,7 @@ func NewBarChart() *BarChart {
bc.TextColor = ThemeAttr("barchart.text.fg") bc.TextColor = ThemeAttr("barchart.text.fg")
bc.BarGap = 1 bc.BarGap = 1
bc.BarWidth = 3 bc.BarWidth = 3
bc.CellChar = ' '
return bc return bc
} }
@ -87,16 +89,27 @@ func (bc *BarChart) Buffer() Buffer {
for i := 0; i < bc.numBar && i < len(bc.Data) && i < len(bc.DataLabels); i++ { for i := 0; i < bc.numBar && i < len(bc.Data) && i < len(bc.DataLabels); i++ {
h := int(float64(bc.Data[i]) / bc.scale) h := int(float64(bc.Data[i]) / bc.scale)
oftX := i * (bc.BarWidth + bc.BarGap) oftX := i * (bc.BarWidth + bc.BarGap)
barBg := bc.Bg
barFg := bc.BarColor
if bc.CellChar == ' ' {
barBg = bc.BarColor
barFg = ColorDefault
if bc.BarColor == ColorDefault { // the same as above
barBg |= AttrReverse
}
}
// plot bar // plot bar
for j := 0; j < bc.BarWidth; j++ { for j := 0; j < bc.BarWidth; j++ {
for k := 0; k < h; k++ { for k := 0; k < h; k++ {
c := Cell{ c := Cell{
Ch: ' ', Ch: bc.CellChar,
Bg: bc.BarColor, Bg: barBg,
} Fg: barFg,
if bc.BarColor == ColorDefault { // when color is default, space char treated as transparent!
c.Bg |= AttrReverse
} }
x := bc.innerArea.Min.X + i*(bc.BarWidth+bc.BarGap) + j x := bc.innerArea.Min.X + i*(bc.BarWidth+bc.BarGap) + j
y := bc.innerArea.Min.Y + bc.innerArea.Dy() - 2 - k y := bc.innerArea.Min.Y + bc.innerArea.Dy() - 2 - k
buf.Set(x, y, c) buf.Set(x, y, c)
@ -120,11 +133,9 @@ func (bc *BarChart) Buffer() Buffer {
c := Cell{ c := Cell{
Ch: bc.dataNum[i][j], Ch: bc.dataNum[i][j],
Fg: bc.NumColor, Fg: bc.NumColor,
Bg: bc.BarColor, Bg: barBg,
}
if bc.BarColor == ColorDefault { // the same as above
c.Bg |= AttrReverse
} }
if h == 0 { if h == 0 {
c.Bg = bc.Bg c.Bg = bc.Bg
} }