Skip to content

Commit

Permalink
bar and funnel: Prevent divide by zero if empty series is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
jentfoo committed Jan 10, 2025
1 parent 0d67ace commit 3a88761
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bar_chart.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package charts

import (
"errors"
"math"

"github.com/golang/freetype/truetype"
Expand Down Expand Up @@ -64,6 +65,9 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B
barMargin = 3
}
seriesCount := len(seriesList)
if seriesCount == 0 {
return BoxZero, errors.New("empty series list")
}
barWidth := (width - 2*margin - barMargin*(seriesCount-1)) / seriesCount
if opt.BarWidth > 0 && opt.BarWidth < barWidth {
barWidth = opt.BarWidth
Expand Down
5 changes: 5 additions & 0 deletions funnel_chart.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package charts

import (
"errors"

"github.com/golang/freetype/truetype"

"github.com/go-analyze/charts/chartdraw"
Expand Down Expand Up @@ -63,6 +65,9 @@ func (f *funnelChart) render(result *defaultRenderResult, seriesList SeriesList)
height := seriesPainter.Height()
width := seriesPainter.Width()
count := len(seriesList)
if count == 0 {
return BoxZero, errors.New("empty series list")
}

h := (height - gap*(count-1)) / count

Expand Down
5 changes: 5 additions & 0 deletions horizontal_bar_chart.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package charts

import (
"errors"

"github.com/golang/freetype/truetype"

"github.com/go-analyze/charts/chartdraw"
Expand Down Expand Up @@ -59,6 +61,9 @@ func (h *horizontalBarChart) render(result *defaultRenderResult, seriesList Seri
barMargin = 3
}
seriesCount := len(seriesList)
if seriesCount == 0 {
return BoxZero, errors.New("empty series list")
}
barHeight := (height - 2*margin - barMargin*(seriesCount-1)) / seriesCount
if opt.BarHeight > 0 && opt.BarHeight < barHeight {
barHeight = opt.BarHeight
Expand Down

0 comments on commit 3a88761

Please sign in to comment.