diff --git a/chart_option_test.go b/chart_option_test.go index 0cdc2aa..a025c25 100644 --- a/chart_option_test.go +++ b/chart_option_test.go @@ -204,7 +204,7 @@ func TestLineRender(t *testing.T) { assert.Nil(err) data, err := p.Bytes() assert.Nil(err) - assert.Equal("\\nEmailUnion AdsVideo AdsDirectSearch EngineLine1.44k1.2k9607204802400MonTueWedThuFriSatSun", string(data)) + assert.Equal("\\nEmailUnion AdsVideo AdsDirectSearch EngineLine1.44k1.2k9607204802400MonTueWedThuFriSatSun", string(data)) } func TestBarRender(t *testing.T) { @@ -277,7 +277,7 @@ func TestBarRender(t *testing.T) { assert.Nil(err) data, err := p.Bytes() assert.Nil(err) - assert.Equal("\\nRainfallEvaporation24020016012080400FebMayAugNov162.22182.22.341.6248.07", string(data)) + assert.Equal("\\nRainfallEvaporation24020016012080400FebMayAugNov162.22182.22.341.6248.07", string(data)) } func TestHorizontalBarRender(t *testing.T) { @@ -368,7 +368,7 @@ func TestPieRender(t *testing.T) { assert.Nil(err) data, err := p.Bytes() assert.Nil(err) - assert.Equal("\\nSearch EngineDirectEmailUnion AdsVideo AdsRainfall vs EvaporationFake DataSearch Engine: 33.3%Direct: 23.35%Email: 18.43%Union Ads: 15.37%Video Ads: 9.53%", string(data)) + assert.Equal("\\nSearch EngineDirectEmailUnion AdsVideo AdsRainfall vs EvaporationFake DataSearch Engine: 33.3%Direct: 23.35%Email: 18.43%Union Ads: 15.37%Video Ads: 9.53%", string(data)) } func TestRadarRender(t *testing.T) { diff --git a/charts.go b/charts.go index 6c1c92b..41802d9 100644 --- a/charts.go +++ b/charts.go @@ -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, })) } diff --git a/legend.go b/legend.go index 820f1b5..4e2bc82 100644 --- a/legend.go +++ b/legend.go @@ -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{ diff --git a/line_chart_test.go b/line_chart_test.go index ff80741..856cdf3 100644 --- a/line_chart_test.go +++ b/line_chart_test.go @@ -117,7 +117,7 @@ func TestLineChart(t *testing.T) { } return p.Bytes() }, - result: "\\nEmailUnion AdsVideo AdsDirectSearch EngineLine1.44k1.2k9607204802400MonTueWedThuFriSatSun", + result: "\\nEmailUnion AdsVideo AdsDirectSearch EngineLine1.44k1.2k9607204802400MonTueWedThuFriSatSun", }, { render: func(p *Painter) ([]byte, error) { @@ -201,7 +201,7 @@ func TestLineChart(t *testing.T) { } return p.Bytes() }, - result: "\\nEmailUnion AdsVideo AdsDirectSearch EngineLine1.44k1.2k9607204802400MonTueWedThuFriSatSun", + result: "\\nEmailUnion AdsVideo AdsDirectSearch EngineLine1.44k1.2k9607204802400MonTueWedThuFriSatSun", }, } diff --git a/pie_chart_test.go b/pie_chart_test.go index c373a7e..070fb03 100644 --- a/pie_chart_test.go +++ b/pie_chart_test.go @@ -78,7 +78,7 @@ func TestPieChart(t *testing.T) { } return p.Bytes() }, - result: "\\nSearch EngineDirectEmailUnion AdsVideo AdsRainfall vs EvaporationFake DataSearch Engine: 33.3%Direct: 23.35%Email: 18.43%Union Ads: 15.37%Video Ads: 9.53%", + result: "\\nSearch EngineDirectEmailUnion AdsVideo AdsRainfall vs EvaporationFake DataSearch Engine: 33.3%Direct: 23.35%Email: 18.43%Union Ads: 15.37%Video Ads: 9.53%", }, } for _, tt := range tests {