1
0
mirror of https://github.com/gizak/termui.git synced 2025-04-24 13:48:50 +08:00

Center percentage based on character length

This commit is contained in:
Marc S. Brooks 2020-12-06 19:10:33 -08:00
parent 4cca61d83f
commit 433fc253d7

View File

@ -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)
}