refactor: adjust legend to center as default
This commit is contained in:
parent
c4b5ac3f42
commit
29a1bdc1fb
2 changed files with 19 additions and 3 deletions
21
legend.go
21
legend.go
|
|
@ -54,6 +54,23 @@ func drawLegend(p *Draw, opt *LegendOption, theme *Theme) (chart.Box, error) {
|
|||
legendDotHeight := 5
|
||||
textPadding := 5
|
||||
legendMargin := 10
|
||||
|
||||
widthCount := 0
|
||||
// 文本宽度
|
||||
for _, text := range opt.Data {
|
||||
b := r.MeasureText(text)
|
||||
widthCount += b.Width()
|
||||
}
|
||||
// 加上标记
|
||||
widthCount += legendWidth * len(opt.Data)
|
||||
// 文本的padding
|
||||
widthCount += 2 * textPadding * len(opt.Data)
|
||||
// margin的宽度
|
||||
widthCount += legendMargin * (len(opt.Data) - 1)
|
||||
|
||||
// TODO 支持更多的定位方式
|
||||
// 居中
|
||||
x = (legendDraw.Box.Width() - widthCount) >> 1
|
||||
for index, text := range opt.Data {
|
||||
if index != 0 {
|
||||
x += legendMargin
|
||||
|
|
@ -61,12 +78,12 @@ func drawLegend(p *Draw, opt *LegendOption, theme *Theme) (chart.Box, error) {
|
|||
style := chart.Style{
|
||||
StrokeColor: theme.GetSeriesColor(index),
|
||||
FillColor: theme.GetSeriesColor(index),
|
||||
StrokeWidth: 2,
|
||||
StrokeWidth: 3,
|
||||
}
|
||||
textBox := r.MeasureText(text)
|
||||
renderText := func() {
|
||||
x += textPadding
|
||||
legendDraw.text(text, x, y+legendDotHeight-1)
|
||||
legendDraw.text(text, x, y+legendDotHeight-2)
|
||||
x += textBox.Width()
|
||||
x += textPadding
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,7 +58,6 @@ func NewLineChart(opt LineChartOption) (*Draw, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
opt.Legend.Style.Padding.Left += titleBox.Right
|
||||
_, err = drawLegend(d, &opt.Legend, &theme)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue