Merge pull request #118 from bcicen/barchart-numfmt

Allow for custom number formatting function in barchart
This commit is contained in:
Caleb Bassi 2018-08-16 20:37:24 -07:00 committed by GitHub
commit 8d4faad061
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 3 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)
} }

View File

@ -30,6 +30,7 @@ type MBarChart struct {
BarColor [NumberofColors]Attribute BarColor [NumberofColors]Attribute
TextColor Attribute TextColor Attribute
NumColor [NumberofColors]Attribute NumColor [NumberofColors]Attribute
NumFmt func(int) string
Data [NumberofColors][]int Data [NumberofColors][]int
DataLabels []string DataLabels []string
BarWidth int BarWidth int
@ -51,6 +52,7 @@ func NewMBarChart() *MBarChart {
bc.BarColor[0] = ThemeAttr("mbarchart.bar.bg") bc.BarColor[0] = ThemeAttr("mbarchart.bar.bg")
bc.NumColor[0] = ThemeAttr("mbarchart.num.fg") bc.NumColor[0] = ThemeAttr("mbarchart.num.fg")
bc.TextColor = ThemeAttr("mbarchart.text.fg") bc.TextColor = ThemeAttr("mbarchart.text.fg")
bc.NumFmt = func(n int) string { return fmt.Sprint(n) }
bc.BarGap = 1 bc.BarGap = 1
bc.BarWidth = 3 bc.BarWidth = 3
return bc return bc
@ -93,7 +95,7 @@ func (bc *MBarChart) layout() {
//For each stack of bar calculate the rune //For each stack of bar calculate the rune
for j := 0; j < LabelLen && i < bc.numBar; j++ { for j := 0; j < LabelLen && i < bc.numBar; j++ {
n := bc.Data[i][j] n := bc.Data[i][j]
s := fmt.Sprint(n) s := bc.NumFmt(n)
bc.dataNum[i][j] = trimStr2Runes(s, bc.BarWidth) bc.dataNum[i][j] = trimStr2Runes(s, bc.BarWidth)
} }
//If color is not defined by default then populate a color that is different from the previous bar //If color is not defined by default then populate a color that is different from the previous bar
@ -127,7 +129,7 @@ func (bc *MBarChart) layout() {
//Finally Calculate max sale //Finally Calculate max sale
if bc.ShowScale { if bc.ShowScale {
s := fmt.Sprintf("%d", bc.max) s := bc.NumFmt(bc.max)
bc.maxScale = trimStr2Runes(s, len(s)) bc.maxScale = trimStr2Runes(s, len(s))
bc.scale = float64(bc.max) / float64(bc.innerArea.Dy()-2) bc.scale = float64(bc.max) / float64(bc.innerArea.Dy()-2)
} else { } else {