mirror of https://github.com/gizak/termui.git
Fix an OutofIndex Panic on empty bargraphs and introduce SetMax()
This commit is contained in:
parent
b3e1431d46
commit
99973c9c71
13
bar.go
13
bar.go
|
@ -59,7 +59,11 @@ func (bc *BarChart) layout() {
|
|||
bc.dataNum[i] = trimStr2Runes(s, bc.BarWidth)
|
||||
}
|
||||
|
||||
bc.max = bc.Data[0] // what if Data is nil?
|
||||
//bc.max = bc.Data[0] // what if Data is nil? Sometimes when bar graph is nill it produces panic with panic: runtime error: index out of range
|
||||
// Asign a negative value to get maxvalue auto-populates
|
||||
if bc.max == 0 {
|
||||
bc.max = -1
|
||||
}
|
||||
for i := 0; i < len(bc.Data); i++ {
|
||||
if bc.max < bc.Data[i] {
|
||||
bc.max = bc.Data[i]
|
||||
|
@ -68,6 +72,13 @@ func (bc *BarChart) layout() {
|
|||
bc.scale = float64(bc.max) / float64(bc.innerHeight-1)
|
||||
}
|
||||
|
||||
func (bc *BarChart) SetMax(max int) {
|
||||
|
||||
if max > 0 {
|
||||
bc.max = max
|
||||
}
|
||||
}
|
||||
|
||||
// Buffer implements Bufferer interface.
|
||||
func (bc *BarChart) Buffer() []Point {
|
||||
ps := bc.Block.Buffer()
|
||||
|
|
Loading…
Reference in New Issue