From d3f7a773afc152f3be5b6237baa23bd2f40177db Mon Sep 17 00:00:00 2001 From: vicanso Date: Thu, 12 Jan 2023 20:20:36 +0800 Subject: [PATCH] fix: fix zero value of funnel chart, #43 --- examples/funnel_chart/main.go | 4 ++++ funnel_chart.go | 13 +++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/examples/funnel_chart/main.go b/examples/funnel_chart/main.go index 8f21db6..24f8afe 100644 --- a/examples/funnel_chart/main.go +++ b/examples/funnel_chart/main.go @@ -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 { diff --git a/funnel_chart.go b/funnel_chart.go index 300b539..d4a8bdd 100644 --- a/funnel_chart.go +++ b/funnel_chart.go @@ -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) }