Skip to content

Commit

Permalink
(feat) number to K
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Jan 4, 2024
1 parent 4365d8e commit e1c2bd3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pkg/bar_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (c *BarChart) GetVertical(xData []string, yData [][]float64, names []string
Bottom: DEFAULT_PADDING_BOTTOM,
}
opt.ValueFormatter = func(f float64) string {
return fmt.Sprintf("%.0f%s", f, req.Metric)
return fmt.Sprintf("%s %s", NumberToK(&f), req.Metric)
}
idx := len(opt.SeriesList) - 1
if len(opt.SeriesList) > 1 {
Expand Down Expand Up @@ -164,7 +164,7 @@ func (c *BarChart) GetHorizontal(xData []string, yData [][]float64, names []stri
func(opt *charts.ChartOption) {
opt.Theme = req.Theme
opt.ValueFormatter = func(f float64) string {
return fmt.Sprintf("%.0f%s", f, req.Metric)
return fmt.Sprintf("%s %s", NumberToK(&f), req.Metric)
}
},
)
Expand Down
18 changes: 18 additions & 0 deletions pkg/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net/http"
"net/url"
"strconv"
"strings"

"github.com/imroc/req/v3"
Expand Down Expand Up @@ -122,3 +123,20 @@ func SetDataIfRemoteURL(req *ChartRequest, allowedRemoteDomains string) error {
}
return nil
}

// NumberToK converts a number to a string with 'k' for thousands and 'm' for millions.
func NumberToK(num *float64) string {
if num == nil {
return "0"
}

if *num < 1000 {
return strconv.FormatFloat(*num, 'f', -1, 64)
}

if *num < 1000000 {
return strconv.FormatFloat(*num/1000, 'f', 1, 64) + "k"
}

return strconv.FormatFloat(*num/1000000, 'f', 1, 64) + "m"
}
2 changes: 1 addition & 1 deletion pkg/line_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c *LineChart) Get(xData []string, yData [][]float64, names []string, req *
Bottom: DEFAULT_PADDING_BOTTOM * 2,
}
opt.ValueFormatter = func(f float64) string {
return fmt.Sprintf("%.0f%s", f, req.Metric)
return fmt.Sprintf("%s %s", NumberToK(&f), req.Metric)
}
opt.FillArea = req.Fill

Expand Down

0 comments on commit e1c2bd3

Please sign in to comment.