diff --git a/line_chart_test.go b/line_chart_test.go index e169f90..9216970 100644 --- a/line_chart_test.go +++ b/line_chart_test.go @@ -203,6 +203,97 @@ func TestLineChart(t *testing.T) { }, result: "\\nEmailUnion AdsVideo AdsDirectSearch EngineLine1.44k1.2k9607204802400MonTueWedThuFriSatSun", }, + { + render: func(p *Painter) ([]byte, error) { + values := [][]float64{ + { + 1200, + 1320, + 1010, + 1340, + 900, + 2300, + 2100, + }, + { + 2200, + 1820, + 1910, + 2340, + 2900, + 3300, + 3100, + }, + { + 1500, + 2320, + 2010, + 1540, + 1900, + 3300, + 4100, + }, + { + 3200, + 3320, + 3010, + 3340, + 3900, + 3300, + 3200, + }, + { + 8200, + 9320, + 9010, + 9340, + 1290, + 1330, + 1320, + }, + } + _, err := NewLineChart(p, LineChartOption{ + Title: TitleOption{ + Text: "Line", + }, + Padding: Box{ + Top: 10, + Right: 10, + Bottom: 10, + Left: 10, + }, + XAxis: NewXAxisOption([]string{ + "Mon", + "Tue", + "Wed", + "Thu", + "Fri", + "Sat", + "Sun", + }, FalseFlag()), + Legend: NewLegendOption([]string{ + "Email", + "Union Ads", + "Video Ads", + "Direct", + "Search Engine", + }, PositionCenter), + SeriesList: func(list SeriesList) SeriesList { + for index := range list { + list[index].Label.Show = true + list[index].Label.Position = "top" + list[index].Label.Formatter = "{e}" + } + return list + }(NewSeriesListDataFromValues(values)), + }).Render() + if err != nil { + return nil, err + } + return p.Bytes() + }, + result: "\\nEmailUnion AdsVideo AdsDirectSearch EngineLine9.48k7.9k6.32k4.74k3.16k1.58k0MonTueWedThuFriSatSun1,2001,3201,0101,3409002,3002,1002,2001,8201,9102,3402,9003,3003,1001,5002,3202,0101,5401,9003,3004,1003,2003,3203,0103,3403,9003,3003,2008,2009,3209,0109,3401,2901,3301,320", + }, } for _, tt := range tests { diff --git a/series.go b/series.go index 0ad135f..3ca12bb 100644 --- a/series.go +++ b/series.go @@ -72,6 +72,7 @@ type SeriesLabel struct { // {b}: the name of a data item. // {c}: the value of a data item. // {d}: the percent of a data item(pie chart). + // {e}: the value with commas of a data item Formatter string // The color for label Color Color @@ -306,6 +307,7 @@ func NewLabelFormatter(seriesNames []string, layout string) LabelFormatter { percentText = humanize.FtoaWithDigits(percent*100, 2) + "%" } valueText := humanize.FtoaWithDigits(value, 2) + valueTextWithCommas := humanize.CommafWithDigits(value, 2) name := "" if len(seriesNames) > index { name = seriesNames[index] @@ -313,6 +315,7 @@ func NewLabelFormatter(seriesNames []string, layout string) LabelFormatter { text := strings.ReplaceAll(layout, "{c}", valueText) text = strings.ReplaceAll(text, "{d}", percentText) text = strings.ReplaceAll(text, "{b}", name) + text = strings.ReplaceAll(text, "{e}", valueTextWithCommas) return text } } diff --git a/series_test.go b/series_test.go index 40d2f91..1dd412d 100644 --- a/series_test.go +++ b/series_test.go @@ -86,4 +86,9 @@ func TestFormatter(t *testing.T) { "a", "b", }, "")(0, 10, 0.12)) + + assert.Equal("10,000.01", NewLabelFormatter([]string{ + "a", + "b", + }, "{e}")(0, 10000.01, 0)) }