feat: support text rotation for series label, #38

This commit is contained in:
vicanso 2022-12-11 14:57:05 +08:00
parent ef04ac14ab
commit d5533447f5
3 changed files with 36 additions and 7 deletions

14
axis.go
View file

@ -75,6 +75,10 @@ type AxisOption struct {
SplitLineShow bool SplitLineShow bool
// The color of split line // The color of split line
SplitLineColor Color SplitLineColor Color
// The text rotation of label
TextRotation float64
// The offset of label
LabelOffset Box
Unit int Unit int
} }
@ -153,7 +157,15 @@ func (a *axisPainter) Render() (Box, error) {
} }
top.SetDrawingStyle(style).OverrideTextStyle(style) top.SetDrawingStyle(style).OverrideTextStyle(style)
isTextRotation := opt.TextRotation != 0
if isTextRotation {
top.SetTextRotation(opt.TextRotation)
}
textMaxWidth, textMaxHeight := top.MeasureTextMaxWidthHeight(data) textMaxWidth, textMaxHeight := top.MeasureTextMaxWidthHeight(data)
if isTextRotation {
top.ClearTextRotation()
}
// 增加30px来计算文本展示区域 // 增加30px来计算文本展示区域
textFillWidth := float64(textMaxWidth + 20) textFillWidth := float64(textMaxWidth + 20)
@ -266,6 +278,8 @@ func (a *axisPainter) Render() (Box, error) {
Orient: orient, Orient: orient,
Unit: unit, Unit: unit,
Position: labelPosition, Position: labelPosition,
TextRotation: opt.TextRotation,
Offset: opt.LabelOffset,
}) })
// 显示辅助线 // 显示辅助线
if opt.SplitLineShow { if opt.SplitLineShow {

View file

@ -71,6 +71,9 @@ type MultiTextOption struct {
Unit int Unit int
Position string Position string
Align string Align string
// The text rotation of label
TextRotation float64
Offset Box
} }
type GridOption struct { type GridOption struct {
@ -682,10 +685,13 @@ func (p *Painter) MultiText(opt MultiTextOption) *Painter {
} else { } else {
values = autoDivide(width, count) values = autoDivide(width, count)
} }
offset := opt.Offset
for index, text := range opt.TextList { for index, text := range opt.TextList {
if opt.Unit != 0 && index%opt.Unit != showIndex { if opt.Unit != 0 && index%opt.Unit != showIndex {
continue continue
} }
p.ClearTextRotation()
p.SetTextRotation(opt.TextRotation)
box := p.MeasureText(text) box := p.MeasureText(text)
start := values[index] start := values[index]
if positionCenter { if positionCenter {
@ -706,8 +712,11 @@ func (p *Painter) MultiText(opt MultiTextOption) *Painter {
} else { } else {
x = start - box.Width()>>1 x = start - box.Width()>>1
} }
x += offset.Left
y += offset.Top
p.Text(text, x, y) p.Text(text, x, y)
} }
p.ClearTextRotation()
return p return p
} }

View file

@ -48,6 +48,10 @@ type XAxisOption struct {
StrokeColor Color StrokeColor Color
// The color of label // The color of label
FontColor Color FontColor Color
// The text rotation of label
TextRotation float64
// The offset of label
LabelOffset Box
isValueAxis bool isValueAxis bool
} }
@ -81,6 +85,8 @@ func (opt *XAxisOption) ToAxisOption() AxisOption {
FontColor: opt.FontColor, FontColor: opt.FontColor,
Show: opt.Show, Show: opt.Show,
SplitLineColor: opt.Theme.GetAxisSplitLineColor(), SplitLineColor: opt.Theme.GetAxisSplitLineColor(),
TextRotation: opt.TextRotation,
LabelOffset: opt.LabelOffset,
} }
if opt.isValueAxis { if opt.isValueAxis {
axisOpt.SplitLineShow = true axisOpt.SplitLineShow = true