From 8740c55a1a90f6f895f6601a6afd8f3c4fcf0cbd Mon Sep 17 00:00:00 2001 From: vicanso Date: Tue, 19 Jul 2022 20:12:31 +0800 Subject: [PATCH] feat: support padding for legend --- examples/line_chart/main.go | 8 +++++++- legend.go | 12 ++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/examples/line_chart/main.go b/examples/line_chart/main.go index 45ff894..a941bca 100644 --- a/examples/line_chart/main.go +++ b/examples/line_chart/main.go @@ -89,7 +89,13 @@ func main() { "Video Ads", "Direct", "Search Engine", - }, charts.PositionCenter), + }, "50"), + func(opt *charts.ChartOption) { + opt.Legend.Padding = charts.Box{ + Top: 5, + Bottom: 10, + } + }, ) if err != nil { diff --git a/legend.go b/legend.go index 4e2bc82..8f21afb 100644 --- a/legend.go +++ b/legend.go @@ -59,6 +59,8 @@ type LegendOption struct { FontColor Color // The flag for show legend, set this to *false will hide legend Show *bool + // The padding of legend + Padding Box } // NewLegendOption returns a legend option @@ -111,9 +113,11 @@ func (l *legendPainter) Render() (Box, error) { if opt.Left == "" { opt.Left = PositionCenter } - p := l.p.Child(PainterPaddingOption(Box{ - Top: 5, - })) + padding := opt.Padding + if padding.IsZero() { + padding.Top = 5 + } + p := l.p.Child(PainterPaddingOption(padding)) p.SetTextStyle(Style{ FontSize: opt.FontSize, FontColor: opt.FontColor, @@ -242,6 +246,6 @@ func (l *legendPainter) Render() (Box, error) { return Box{ Right: width, - Bottom: height, + Bottom: height + padding.Bottom + padding.Top, }, nil }