From 910e2dc422fbd36cd0c60f4975cc7f723507c565 Mon Sep 17 00:00:00 2001 From: vicanso Date: Tue, 1 Feb 2022 09:48:47 +0800 Subject: [PATCH] refactor: enhance legend render function --- chart.go | 1 + legend.go | 19 +++++++++++++++++-- line_chart.go | 2 +- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/chart.go b/chart.go index 9383809..33ed71b 100644 --- a/chart.go +++ b/chart.go @@ -81,6 +81,7 @@ func (o *ChartOption) FillDefault(t *Theme) { Bottom: 5, } } + o.Legend.Theme = t if o.Legend.Style.FontSize == 0 { o.Legend.Style.FontSize = 8 } diff --git a/legend.go b/legend.go index ad9be61..abffeb3 100644 --- a/legend.go +++ b/legend.go @@ -27,20 +27,35 @@ import ( ) type LegendOption struct { + Theme *Theme Style chart.Style Data []string Left string Right 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 { return chart.BoxZero, nil } + theme := opt.Theme padding := opt.Style.Padding legendDraw, err := NewDraw(DrawOption{ - Parent: p, + Parent: d, }, PaddingOption(padding)) if err != nil { return chart.BoxZero, err diff --git a/line_chart.go b/line_chart.go index 391365b..3e04514 100644 --- a/line_chart.go +++ b/line_chart.go @@ -58,7 +58,7 @@ func NewLineChart(opt LineChartOption) (*Draw, error) { return nil, err } - _, err = drawLegend(d, &opt.Legend, &theme) + _, err = NewLegend(d, opt.Legend).Render() if err != nil { return nil, err }