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
jentfoo committed Jan 10, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 0d67ace commit 3a88761
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"
@@ -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
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"
@@ -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

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"
@@ -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

0 comments on commit 3a88761

Please sign in to comment.