From 099886834fae186ca3e732a7ff8acfcdf8387823 Mon Sep 17 00:00:00 2001 From: Bradley Cicenas Date: Sun, 8 Jan 2017 17:29:50 +0000 Subject: [PATCH 1/2] allow for custom number format function in barchart --- barchart.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/barchart.go b/barchart.go index 6560c8b..feb4bbd 100644 --- a/barchart.go +++ b/barchart.go @@ -25,6 +25,7 @@ type BarChart struct { BarColor Attribute TextColor Attribute NumColor Attribute + NumFmt func(int) string Data []int DataLabels []string BarWidth int @@ -43,6 +44,7 @@ func NewBarChart() *BarChart { bc.BarColor = ThemeAttr("barchart.bar.bg") bc.NumColor = ThemeAttr("barchart.num.fg") bc.TextColor = ThemeAttr("barchart.text.fg") + bc.NumFmt = func(n int) string { return fmt.Sprint(n) } bc.BarGap = 1 bc.BarWidth = 3 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++ { bc.labels[i] = trimStr2Runes(bc.DataLabels[i], bc.BarWidth) n := bc.Data[i] - s := fmt.Sprint(n) + s := bc.NumFmt(n) bc.dataNum[i] = trimStr2Runes(s, bc.BarWidth) } From ea10e6ccee219e572ffad0ac1909f1a17f6db7d6 Mon Sep 17 00:00:00 2001 From: Bradley Cicenas Date: Sun, 8 Jan 2017 17:42:26 +0000 Subject: [PATCH 2/2] add custom number format func to mbarchart --- mbarchart.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mbarchart.go b/mbarchart.go index 0ce6c45..b3f774b 100644 --- a/mbarchart.go +++ b/mbarchart.go @@ -30,6 +30,7 @@ type MBarChart struct { BarColor [NumberofColors]Attribute TextColor Attribute NumColor [NumberofColors]Attribute + NumFmt func(int) string Data [NumberofColors][]int DataLabels []string BarWidth int @@ -51,6 +52,7 @@ func NewMBarChart() *MBarChart { bc.BarColor[0] = ThemeAttr("mbarchart.bar.bg") bc.NumColor[0] = ThemeAttr("mbarchart.num.fg") bc.TextColor = ThemeAttr("mbarchart.text.fg") + bc.NumFmt = func(n int) string { return fmt.Sprint(n) } bc.BarGap = 1 bc.BarWidth = 3 return bc @@ -93,7 +95,7 @@ func (bc *MBarChart) layout() { //For each stack of bar calculate the rune for j := 0; j < LabelLen && i < bc.numBar; j++ { n := bc.Data[i][j] - s := fmt.Sprint(n) + s := bc.NumFmt(n) 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 @@ -127,7 +129,7 @@ func (bc *MBarChart) layout() { //Finally Calculate max sale if bc.ShowScale { - s := fmt.Sprintf("%d", bc.max) + s := bc.NumFmt(bc.max) bc.maxScale = trimStr2Runes(s, len(s)) bc.scale = float64(bc.max) / float64(bc.innerArea.Dy()-2) } else {