Skip to content
Open
Changes from all 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
20 changes: 18 additions & 2 deletions 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 + calcNumberXPos(self.NumFormatter(data), self.BarWidth)
if numberXCoordinate <= self.Inner.Max.X {
buf.SetString(
self.NumFormatter(data),
Expand All @@ -87,3 +87,19 @@ func (self *BarChart) Draw(buf *Buffer) {
barXCoordinate += (self.BarWidth + self.BarGap)
}
}

//
// Compute bar text position based on character length.
//
func calcNumberXPos(numFormatterData string, barWidth int) int {
numberCharsLen := len(numFormatterData)
barWidthCenter := int(float64(barWidth / 2))

var numberXCharPos int = barWidthCenter - numberCharsLen + (numberCharsLen / 2)

if numberCharsLen > barWidthCenter {
numberXCharPos = numberCharsLen - barWidthCenter
}

return int(numberXCharPos)
}