Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes to resolve barchart style inconsistencies #284

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions v3/widgets/barchart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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),
Expand All @@ -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)
nuxy marked this conversation as resolved.
Show resolved Hide resolved

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