feat: support label format for funnel chart, #41

This commit is contained in:
vicanso 2023-01-05 19:15:58 +08:00
parent b3cb5a75cb
commit e10175594b
2 changed files with 11 additions and 5 deletions

View file

@ -23,9 +23,6 @@
package charts package charts
import ( import (
"fmt"
"github.com/dustin/go-humanize"
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
) )
@ -95,13 +92,14 @@ func (f *funnelChart) render(result *defaultRenderResult, seriesList SeriesList)
y := 0 y := 0
widthList := make([]int, len(seriesList)) widthList := make([]int, len(seriesList))
textList := make([]string, len(seriesList)) textList := make([]string, len(seriesList))
seriesNames := seriesList.Names()
for index, item := range seriesList { for index, item := range seriesList {
value := item.Data[0].Value value := item.Data[0].Value
widthPercent := (value - min) / (max - min) widthPercent := (value - min) / (max - min)
w := int(widthPercent * float64(width)) w := int(widthPercent * float64(width))
widthList[index] = w widthList[index] = w
p := humanize.CommafWithDigits(value/max*100, 2) + "%" percent := value / max
textList[index] = fmt.Sprintf("%s(%s)", item.Name, p) textList[index] = NewFunnelLabelFormatter(seriesNames, item.Label.Formatter)(index, value, percent)
} }
for index, w := range widthList { for index, w := range widthList {

View file

@ -279,6 +279,14 @@ func NewPieLabelFormatter(seriesNames []string, layout string) LabelFormatter {
return NewLabelFormatter(seriesNames, layout) 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 // NewValueLabelFormatter returns a value formatter
func NewValueLabelFormatter(seriesNames []string, layout string) LabelFormatter { func NewValueLabelFormatter(seriesNames []string, layout string) LabelFormatter {
if len(layout) == 0 { if len(layout) == 0 {