refactor: enhance legend render function

This commit is contained in:
vicanso 2022-02-01 09:48:47 +08:00
parent 9dbea37f55
commit 910e2dc422
3 changed files with 19 additions and 3 deletions

View file

@ -81,6 +81,7 @@ func (o *ChartOption) FillDefault(t *Theme) {
Bottom: 5, Bottom: 5,
} }
} }
o.Legend.Theme = t
if o.Legend.Style.FontSize == 0 { if o.Legend.Style.FontSize == 0 {
o.Legend.Style.FontSize = 8 o.Legend.Style.FontSize = 8
} }

View file

@ -27,20 +27,35 @@ import (
) )
type LegendOption struct { type LegendOption struct {
Theme *Theme
Style chart.Style Style chart.Style
Data []string Data []string
Left string Left string
Right string Right string
Align string Align string
} }
type legend struct {
d *Draw
opt *LegendOption
}
func drawLegend(p *Draw, opt *LegendOption, theme *Theme) (chart.Box, error) { func NewLegend(d *Draw, opt LegendOption) *legend {
return &legend{
d: d,
opt: &opt,
}
}
func (l *legend) Render() (chart.Box, error) {
d := l.d
opt := l.opt
if len(opt.Data) == 0 { if len(opt.Data) == 0 {
return chart.BoxZero, nil return chart.BoxZero, nil
} }
theme := opt.Theme
padding := opt.Style.Padding padding := opt.Style.Padding
legendDraw, err := NewDraw(DrawOption{ legendDraw, err := NewDraw(DrawOption{
Parent: p, Parent: d,
}, PaddingOption(padding)) }, PaddingOption(padding))
if err != nil { if err != nil {
return chart.BoxZero, err return chart.BoxZero, err

View file

@ -58,7 +58,7 @@ func NewLineChart(opt LineChartOption) (*Draw, error) {
return nil, err return nil, err
} }
_, err = drawLegend(d, &opt.Legend, &theme) _, err = NewLegend(d, opt.Legend).Render()
if err != nil { if err != nil {
return nil, err return nil, err
} }