fix: fix multi line legend

This commit is contained in:
vicanso 2022-07-11 20:20:41 +08:00
parent 959377542e
commit 805f4381a3
5 changed files with 21 additions and 9 deletions

File diff suppressed because one or more lines are too long

View file

@ -25,6 +25,8 @@ package charts
import ( import (
"errors" "errors"
"sort" "sort"
"github.com/wcharczuk/go-chart/v2"
) )
const labelFontSize = 10 const labelFontSize = 10
@ -110,14 +112,16 @@ func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, e
p = p.Child(PainterPaddingOption(opt.Padding)) p = p.Child(PainterPaddingOption(opt.Padding))
} }
legendHeight := 0
if len(opt.LegendOption.Data) != 0 { if len(opt.LegendOption.Data) != 0 {
if opt.LegendOption.Theme == nil { if opt.LegendOption.Theme == nil {
opt.LegendOption.Theme = opt.Theme opt.LegendOption.Theme = opt.Theme
} }
_, err := NewLegendPainter(p, opt.LegendOption).Render() legendResult, err := NewLegendPainter(p, opt.LegendOption).Render()
if err != nil { if err != nil {
return nil, err return nil, err
} }
legendHeight = legendResult.Height()
} }
// 如果有标题 // 如果有标题
@ -131,9 +135,10 @@ func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, e
if err != nil { if err != nil {
return nil, err return nil, err
} }
p = p.Child(PainterPaddingOption(Box{ p = p.Child(PainterPaddingOption(Box{
// 标题下留白 // 标题下留白
Top: titleBox.Height() + 20, Top: chart.MaxInt(legendHeight, titleBox.Height()) + 20,
})) }))
} }

View file

@ -182,6 +182,7 @@ func (l *legendPainter) Render() (Box, error) {
x := int(left) x := int(left)
y := int(top) + 10 y := int(top) + 10
startY := y
x0 := x x0 := x
y0 := y y0 := y
@ -203,13 +204,18 @@ func (l *legendPainter) Render() (Box, error) {
} }
return left + legendWidth return left + legendWidth
} }
lastIndex := len(opt.Data) - 1
for index, text := range opt.Data { for index, text := range opt.Data {
color := theme.GetSeriesColor(index) color := theme.GetSeriesColor(index)
p.SetDrawingStyle(Style{ p.SetDrawingStyle(Style{
FillColor: color, FillColor: color,
StrokeColor: color, StrokeColor: color,
}) })
if x0+measureList[index].Width()+textOffset+offset+legendWidth > p.Width() { itemWidth := x0 + measureList[index].Width() + textOffset + offset + legendWidth
if lastIndex == index {
itemWidth = x0 + measureList[index].Width() + legendWidth
}
if itemWidth > p.Width() {
x0 = 0 x0 = 0
y += itemMaxHeight y += itemMaxHeight
y0 = y y0 = y
@ -231,6 +237,7 @@ func (l *legendPainter) Render() (Box, error) {
x0 += offset x0 += offset
y0 = y y0 = y
} }
height = y0 - startY + 10
} }
return Box{ return Box{

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long