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

View file

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

View file

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