feat: support padding for legend

This commit is contained in:
vicanso 2022-07-19 20:12:31 +08:00
parent 3af0d4d445
commit 8740c55a1a
2 changed files with 15 additions and 5 deletions

View file

@ -89,7 +89,13 @@ func main() {
"Video Ads", "Video Ads",
"Direct", "Direct",
"Search Engine", "Search Engine",
}, charts.PositionCenter), }, "50"),
func(opt *charts.ChartOption) {
opt.Legend.Padding = charts.Box{
Top: 5,
Bottom: 10,
}
},
) )
if err != nil { if err != nil {

View file

@ -59,6 +59,8 @@ type LegendOption struct {
FontColor Color FontColor Color
// The flag for show legend, set this to *false will hide legend // The flag for show legend, set this to *false will hide legend
Show *bool Show *bool
// The padding of legend
Padding Box
} }
// NewLegendOption returns a legend option // NewLegendOption returns a legend option
@ -111,9 +113,11 @@ func (l *legendPainter) Render() (Box, error) {
if opt.Left == "" { if opt.Left == "" {
opt.Left = PositionCenter opt.Left = PositionCenter
} }
p := l.p.Child(PainterPaddingOption(Box{ padding := opt.Padding
Top: 5, if padding.IsZero() {
})) padding.Top = 5
}
p := l.p.Child(PainterPaddingOption(padding))
p.SetTextStyle(Style{ p.SetTextStyle(Style{
FontSize: opt.FontSize, FontSize: opt.FontSize,
FontColor: opt.FontColor, FontColor: opt.FontColor,
@ -242,6 +246,6 @@ func (l *legendPainter) Render() (Box, error) {
return Box{ return Box{
Right: width, Right: width,
Bottom: height, Bottom: height + padding.Bottom + padding.Top,
}, nil }, nil
} }