fix: fix zero value of funnel chart, #43

This commit is contained in:
vicanso 2023-01-12 20:20:36 +08:00
parent 8ba9e2e1b2
commit d3f7a773af
2 changed files with 15 additions and 2 deletions

View file

@ -30,6 +30,8 @@ func main() {
60,
40,
20,
10,
0,
}
p, err := charts.FunnelRender(
values,
@ -40,6 +42,8 @@ func main() {
"Visit",
"Inquiry",
"Order",
"Pay",
"Cancel",
}),
)
if err != nil {

View file

@ -93,12 +93,21 @@ func (f *funnelChart) render(result *defaultRenderResult, seriesList SeriesList)
widthList := make([]int, len(seriesList))
textList := make([]string, len(seriesList))
seriesNames := seriesList.Names()
offset := max - min
for index, item := range seriesList {
value := item.Data[0].Value
widthPercent := (value - min) / (max - min)
// 最大最小值一致则为100%
widthPercent := 100.0
if offset != 0 {
widthPercent = (value - min) / offset
}
w := int(widthPercent * float64(width))
widthList[index] = w
percent := value / max
// 如果最大值为0则占比100%
percent := 1.0
if max != 0 {
percent = value / max
}
textList[index] = NewFunnelLabelFormatter(seriesNames, item.Label.Formatter)(index, value, percent)
}