Merge pull request #28 from dhilipkumars/BarGraphFix

Fix an OutofIndex Panic on empty bargraphs and introduce SetMax()
This commit is contained in:
Zack Guo 2015-04-15 17:38:40 -04:00
commit 7ca71e4197
1 changed files with 12 additions and 1 deletions

13
bar.go
View File

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