fix: fix multi line legend
This commit is contained in:
parent
959377542e
commit
805f4381a3
5 changed files with 21 additions and 9 deletions
File diff suppressed because one or more lines are too long
|
|
@ -25,6 +25,8 @@ package charts
|
|||
import (
|
||||
"errors"
|
||||
"sort"
|
||||
|
||||
"github.com/wcharczuk/go-chart/v2"
|
||||
)
|
||||
|
||||
const labelFontSize = 10
|
||||
|
|
@ -110,14 +112,16 @@ func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, e
|
|||
p = p.Child(PainterPaddingOption(opt.Padding))
|
||||
}
|
||||
|
||||
legendHeight := 0
|
||||
if len(opt.LegendOption.Data) != 0 {
|
||||
if opt.LegendOption.Theme == nil {
|
||||
opt.LegendOption.Theme = opt.Theme
|
||||
}
|
||||
_, err := NewLegendPainter(p, opt.LegendOption).Render()
|
||||
legendResult, err := NewLegendPainter(p, opt.LegendOption).Render()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
legendHeight = legendResult.Height()
|
||||
}
|
||||
|
||||
// 如果有标题
|
||||
|
|
@ -131,9 +135,10 @@ func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, e
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p = p.Child(PainterPaddingOption(Box{
|
||||
// 标题下留白
|
||||
Top: titleBox.Height() + 20,
|
||||
Top: chart.MaxInt(legendHeight, titleBox.Height()) + 20,
|
||||
}))
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -182,6 +182,7 @@ func (l *legendPainter) Render() (Box, error) {
|
|||
|
||||
x := int(left)
|
||||
y := int(top) + 10
|
||||
startY := y
|
||||
x0 := x
|
||||
y0 := y
|
||||
|
||||
|
|
@ -203,13 +204,18 @@ func (l *legendPainter) Render() (Box, error) {
|
|||
}
|
||||
return left + legendWidth
|
||||
}
|
||||
lastIndex := len(opt.Data) - 1
|
||||
for index, text := range opt.Data {
|
||||
color := theme.GetSeriesColor(index)
|
||||
p.SetDrawingStyle(Style{
|
||||
FillColor: 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
|
||||
y += itemMaxHeight
|
||||
y0 = y
|
||||
|
|
@ -231,6 +237,7 @@ func (l *legendPainter) Render() (Box, error) {
|
|||
x0 += offset
|
||||
y0 = y
|
||||
}
|
||||
height = y0 - startY + 10
|
||||
}
|
||||
|
||||
return Box{
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue