From 99973c9c71b3b1b2b103ba5fa883239f3ea12b72 Mon Sep 17 00:00:00 2001 From: dhilipkumars Date: Wed, 15 Apr 2015 16:50:49 -0400 Subject: [PATCH] Fix an OutofIndex Panic on empty bargraphs and introduce SetMax() --- bar.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/bar.go b/bar.go index 807b02d..57bae0a 100644 --- a/bar.go +++ b/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()