refactor: enhance legend render function
This commit is contained in:
parent
9dbea37f55
commit
910e2dc422
3 changed files with 19 additions and 3 deletions
1
chart.go
1
chart.go
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
19
legend.go
19
legend.go
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue