From e10175594b517f9a12217478b440faecc8d3c455 Mon Sep 17 00:00:00 2001 From: vicanso Date: Thu, 5 Jan 2023 19:15:58 +0800 Subject: [PATCH] feat: support label format for funnel chart, #41 --- funnel_chart.go | 8 +++----- series.go | 8 ++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/funnel_chart.go b/funnel_chart.go index 719853a..300b539 100644 --- a/funnel_chart.go +++ b/funnel_chart.go @@ -23,9 +23,6 @@ package charts import ( - "fmt" - - "github.com/dustin/go-humanize" "github.com/golang/freetype/truetype" ) @@ -95,13 +92,14 @@ func (f *funnelChart) render(result *defaultRenderResult, seriesList SeriesList) y := 0 widthList := make([]int, len(seriesList)) textList := make([]string, len(seriesList)) + seriesNames := seriesList.Names() for index, item := range seriesList { value := item.Data[0].Value widthPercent := (value - min) / (max - min) w := int(widthPercent * float64(width)) widthList[index] = w - p := humanize.CommafWithDigits(value/max*100, 2) + "%" - textList[index] = fmt.Sprintf("%s(%s)", item.Name, p) + percent := value / max + textList[index] = NewFunnelLabelFormatter(seriesNames, item.Label.Formatter)(index, value, percent) } for index, w := range widthList { diff --git a/series.go b/series.go index 13c637e..f28bfa9 100644 --- a/series.go +++ b/series.go @@ -279,6 +279,14 @@ func NewPieLabelFormatter(seriesNames []string, layout string) LabelFormatter { return NewLabelFormatter(seriesNames, layout) } +// NewFunnelLabelFormatter returns a funner label formatter +func NewFunnelLabelFormatter(seriesNames []string, layout string) LabelFormatter { + if len(layout) == 0 { + layout = "{b}({d})" + } + return NewLabelFormatter(seriesNames, layout) +} + // NewValueLabelFormatter returns a value formatter func NewValueLabelFormatter(seriesNames []string, layout string) LabelFormatter { if len(layout) == 0 {