allow for custom number format function in barchart

This commit is contained in:
Bradley Cicenas 2017-01-08 17:29:50 +00:00 committed by Bradley Cicenas
parent 798ffb9cbb
commit 099886834f
1 changed files with 3 additions and 1 deletions

View File

@ -25,6 +25,7 @@ type BarChart struct {
BarColor Attribute BarColor Attribute
TextColor Attribute TextColor Attribute
NumColor Attribute NumColor Attribute
NumFmt func(int) string
Data []int Data []int
DataLabels []string DataLabels []string
BarWidth int BarWidth int
@ -43,6 +44,7 @@ func NewBarChart() *BarChart {
bc.BarColor = ThemeAttr("barchart.bar.bg") bc.BarColor = ThemeAttr("barchart.bar.bg")
bc.NumColor = ThemeAttr("barchart.num.fg") bc.NumColor = ThemeAttr("barchart.num.fg")
bc.TextColor = ThemeAttr("barchart.text.fg") bc.TextColor = ThemeAttr("barchart.text.fg")
bc.NumFmt = func(n int) string { return fmt.Sprint(n) }
bc.BarGap = 1 bc.BarGap = 1
bc.BarWidth = 3 bc.BarWidth = 3
bc.CellChar = ' ' bc.CellChar = ' '
@ -57,7 +59,7 @@ func (bc *BarChart) layout() {
for i := 0; i < bc.numBar && i < len(bc.DataLabels) && i < len(bc.Data); i++ { for i := 0; i < bc.numBar && i < len(bc.DataLabels) && i < len(bc.Data); i++ {
bc.labels[i] = trimStr2Runes(bc.DataLabels[i], bc.BarWidth) bc.labels[i] = trimStr2Runes(bc.DataLabels[i], bc.BarWidth)
n := bc.Data[i] n := bc.Data[i]
s := fmt.Sprint(n) s := bc.NumFmt(n)
bc.dataNum[i] = trimStr2Runes(s, bc.BarWidth) bc.dataNum[i] = trimStr2Runes(s, bc.BarWidth)
} }