From 433fc253d7356c18d522e22af02293ac04cabb40 Mon Sep 17 00:00:00 2001 From: "Marc S. Brooks" Date: Sun, 6 Dec 2020 19:10:33 -0800 Subject: [PATCH 1/3] Center percentage based on character length --- v3/widgets/barchart.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/v3/widgets/barchart.go b/v3/widgets/barchart.go index d76de3bb..a5044e60 100644 --- a/v3/widgets/barchart.go +++ b/v3/widgets/barchart.go @@ -71,7 +71,7 @@ func (self *BarChart) Draw(buf *Buffer) { } // draw number - numberXCoordinate := barXCoordinate + int((float64(self.BarWidth) / 2)) + numberXCoordinate := barXCoordinate + self.calcNumberXPos(data) if numberXCoordinate <= self.Inner.Max.X { buf.SetString( self.NumFormatter(data), @@ -87,3 +87,21 @@ func (self *BarChart) Draw(buf *Buffer) { barXCoordinate += (self.BarWidth + self.BarGap) } } + +// +// Compute bar text position based on character length. +// +func (self *BarChart) calcNumberXPos(data float64) int { + numFormatterData := self.NumFormatter(data) + + numberCharsLen := len(numFormatterData) + barWidthCenter := int(float64(self.BarWidth / 2)) + + var numberXCharPos int = barWidthCenter - numberCharsLen + (numberCharsLen / 2) + + if numberCharsLen > barWidthCenter { + numberXCharPos = numberCharsLen - barWidthCenter + } + + return int(numberXCharPos) +} From 0acee42c53eaaa579c47af600546be9492cf4007 Mon Sep 17 00:00:00 2001 From: "Marc S. Brooks" Date: Sun, 6 Dec 2020 19:14:07 -0800 Subject: [PATCH 2/3] Always render a complete bar vs partial --- v3/widgets/barchart.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/v3/widgets/barchart.go b/v3/widgets/barchart.go index a5044e60..00e1f17d 100644 --- a/v3/widgets/barchart.go +++ b/v3/widgets/barchart.go @@ -52,7 +52,7 @@ func (self *BarChart) Draw(buf *Buffer) { // draw bar height := int((data / maxVal) * float64(self.Inner.Dy()-1)) for x := barXCoordinate; x < MinInt(barXCoordinate+self.BarWidth, self.Inner.Max.X); x++ { - for y := self.Inner.Max.Y - 2; y > (self.Inner.Max.Y-2)-height; y-- { + for y := self.Inner.Max.Y - 2; y >= (self.Inner.Max.Y-2)-height; y-- { c := NewCell(' ', NewStyle(ColorClear, SelectColor(self.BarColors, i))) buf.SetCell(c, image.Pt(x, y)) } From f26ee98a812738c3a7310a9ad2fcd16bfd2ea472 Mon Sep 17 00:00:00 2001 From: "Marc S. Brooks" Date: Fri, 16 Apr 2021 23:52:20 -0700 Subject: [PATCH 3/3] Converted calcNumberXPos to static method --- v3/widgets/barchart.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/v3/widgets/barchart.go b/v3/widgets/barchart.go index 00e1f17d..5d0d0680 100644 --- a/v3/widgets/barchart.go +++ b/v3/widgets/barchart.go @@ -71,7 +71,7 @@ func (self *BarChart) Draw(buf *Buffer) { } // draw number - numberXCoordinate := barXCoordinate + self.calcNumberXPos(data) + numberXCoordinate := barXCoordinate + calcNumberXPos(self.NumFormatter(data), self.BarWidth) if numberXCoordinate <= self.Inner.Max.X { buf.SetString( self.NumFormatter(data), @@ -91,11 +91,9 @@ func (self *BarChart) Draw(buf *Buffer) { // // Compute bar text position based on character length. // -func (self *BarChart) calcNumberXPos(data float64) int { - numFormatterData := self.NumFormatter(data) - +func calcNumberXPos(numFormatterData string, barWidth int) int { numberCharsLen := len(numFormatterData) - barWidthCenter := int(float64(self.BarWidth / 2)) + barWidthCenter := int(float64(barWidth / 2)) var numberXCharPos int = barWidthCenter - numberCharsLen + (numberCharsLen / 2)