From 748477aa49aea6c33adef13dfa3efb45042a2c0f Mon Sep 17 00:00:00 2001 From: Leigh McCulloch Date: Sun, 3 Jul 2016 12:33:42 -0700 Subject: [PATCH 1/2] Make BarChart cell character configurable. What === Make BarChart cell character configurable. Why === So that it can be set to a non-space character, which can be useful in some situations. For example, it allows the barcharts to be used as ASCII art and copy-pastable. Example === https://github.com/leighmcculloch/keywords --- barchart.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/barchart.go b/barchart.go index 980e958..ded47ed 100644 --- a/barchart.go +++ b/barchart.go @@ -29,6 +29,7 @@ type BarChart struct { DataLabels []string BarWidth int BarGap int + CellChar rune labels [][]rune dataNum [][]rune numBar int @@ -44,6 +45,7 @@ func NewBarChart() *BarChart { bc.TextColor = ThemeAttr("barchart.text.fg") bc.BarGap = 1 bc.BarWidth = 3 + bc.CellChar = ' ' return bc } @@ -91,7 +93,7 @@ func (bc *BarChart) Buffer() Buffer { for j := 0; j < bc.BarWidth; j++ { for k := 0; k < h; k++ { c := Cell{ - Ch: ' ', + Ch: bc.CellChar, Bg: bc.BarColor, } if bc.BarColor == ColorDefault { // when color is default, space char treated as transparent! From 62281b0e2304b8cc835492622fdb5201891e8a4f Mon Sep 17 00:00:00 2001 From: Zack Guo Date: Sat, 13 Aug 2016 22:34:50 -0400 Subject: [PATCH 2/2] Reconcile CellChar color --- barchart.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/barchart.go b/barchart.go index ded47ed..55ce9d1 100644 --- a/barchart.go +++ b/barchart.go @@ -89,16 +89,27 @@ func (bc *BarChart) Buffer() Buffer { for i := 0; i < bc.numBar && i < len(bc.Data) && i < len(bc.DataLabels); i++ { h := int(float64(bc.Data[i]) / bc.scale) 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 for j := 0; j < bc.BarWidth; j++ { for k := 0; k < h; k++ { c := Cell{ Ch: bc.CellChar, - Bg: bc.BarColor, - } - if bc.BarColor == ColorDefault { // when color is default, space char treated as transparent! - c.Bg |= AttrReverse + Bg: barBg, + Fg: barFg, } + x := bc.innerArea.Min.X + i*(bc.BarWidth+bc.BarGap) + j y := bc.innerArea.Min.Y + bc.innerArea.Dy() - 2 - k buf.Set(x, y, c) @@ -122,11 +133,9 @@ func (bc *BarChart) Buffer() Buffer { c := Cell{ Ch: bc.dataNum[i][j], Fg: bc.NumColor, - Bg: bc.BarColor, - } - if bc.BarColor == ColorDefault { // the same as above - c.Bg |= AttrReverse + Bg: barBg, } + if h == 0 { c.Bg = bc.Bg }