From eb4079527f31b032fa68e796937527508cba5f63 Mon Sep 17 00:00:00 2001 From: Pulkit Kathuria Date: Sun, 28 Jan 2024 20:08:27 +0900 Subject: [PATCH] add support for mini --- pkg/chart.go | 10 +++++++- pkg/line_chart.go | 63 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/pkg/chart.go b/pkg/chart.go index 538fd79..e030ba8 100644 --- a/pkg/chart.go +++ b/pkg/chart.go @@ -17,7 +17,8 @@ const ( DEFAULT_PADDING_RIGHT = 20 DEFAULT_PADDING_BOTTOM = 20 DEFAULT_PADDING_LEFT = 20 - DEFAULT_SUBTITLE_FONT_SIZE = 11 + DEFAULT_TITLE_FONT_SIZE = 12 + DEFAULT_SUBTITLE_FONT_SIZE = 10 BAR_STYLE_VERTICAL = "vertical" BAR_STYLE_HORIZONTAL = "horizontal" @@ -169,3 +170,10 @@ func NumberToK(num *float64) string { return formatNumber(*num/1000000) + "m" } + +func Truncate(s string, max int) string { + if len(s) > max { + return s[:max] + "..." + } + return s +} diff --git a/pkg/line_chart.go b/pkg/line_chart.go index a190290..7d9e1c4 100644 --- a/pkg/line_chart.go +++ b/pkg/line_chart.go @@ -17,36 +17,70 @@ func NewLineChart() *LineChart { } func (c *LineChart) Get(xData []string, yData [][]float64, names []string, req *ChartRequest) ([]byte, error) { - fill := false + fill := true if req.Line == "fill" { fill = true } + isMini := false + showLegend := true + paddings := charts.Box{ + Top: 10, + Bottom: 10, + Left: 10, + Right: 10, + } + titleSizes := charts.TitleOption{ + Text: req.ChartTitle, + Subtext: req.ChartSubtitle, + FontSize: DEFAULT_TITLE_FONT_SIZE, + SubtextFontSize: DEFAULT_SUBTITLE_FONT_SIZE, + Left: charts.PositionCenter, + SubtextFontColor: DEFAULT_SUBTITLE_COLOR, + } + if req.Width <= 300 && req.Height <= 300 { + showLegend = false + isMini = true + paddings = charts.Box{ + Top: 10, + Bottom: -20, + Left: -10, + Right: 10, + } + titleSizes = charts.TitleOption{ + Text: Truncate(req.ChartTitle, 17), + Subtext: Truncate(req.ChartSubtitle, 17), + FontSize: DEFAULT_TITLE_FONT_SIZE, + SubtextFontSize: DEFAULT_SUBTITLE_FONT_SIZE, + Left: charts.PositionCenter, + SubtextFontColor: DEFAULT_SUBTITLE_COLOR, + } + } p, err := charts.LineRender( yData, charts.HeightOptionFunc(req.Height), charts.WidthOptionFunc(req.Width), - charts.TitleOptionFunc(charts.TitleOption{ - Text: req.ChartTitle, - Subtext: req.ChartSubtitle, - SubtextFontSize: DEFAULT_SUBTITLE_FONT_SIZE, - Left: charts.PositionCenter, - SubtextFontColor: DEFAULT_SUBTITLE_COLOR, - }), + charts.PaddingOptionFunc(paddings), + charts.TitleOptionFunc(titleSizes), charts.XAxisDataOptionFunc(xData), charts.LegendOptionFunc(charts.LegendOption{ Orient: charts.OrientHorizontal, Data: names, Left: charts.PositionLeft, + Show: &showLegend, }), func(opt *charts.ChartOption) { opt.Type = req.Output opt.Theme = req.Theme opt.Legend.Padding = charts.Box{ - Top: DEFAULT_PADDING_TOP, - Bottom: DEFAULT_PADDING_BOTTOM * 2, + Top: 0, + Bottom: 0, } opt.ValueFormatter = func(f float64) string { + if isMini { + return "-" + } return fmt.Sprintf("%s %s", NumberToK(&f), req.Metric) + } opt.FillArea = fill @@ -58,9 +92,12 @@ func (c *LineChart) Get(xData []string, yData [][]float64, names []string, req * charts.SeriesMarkDataTypeMax, charts.SeriesMarkDataTypeMin, ) - opt.SeriesList[idx].MarkLine = charts.NewMarkLine( - charts.SeriesMarkDataTypeAverage, - ) + if !isMini { + opt.SeriesList[idx].MarkLine = charts.NewMarkLine( + charts.SeriesMarkDataTypeAverage, + ) + } + }, ) if err != nil {