Skip to content

Commit

Permalink
add support for mini
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Jan 28, 2024
1 parent 47ea94e commit eb40795
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
10 changes: 9 additions & 1 deletion pkg/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
63 changes: 50 additions & 13 deletions pkg/line_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 {
Expand Down

0 comments on commit eb40795

Please sign in to comment.