feat: support series label for horizontal bar
This commit is contained in:
parent
4fc250aefc
commit
6db8e2c8dc
4 changed files with 58 additions and 2 deletions
|
|
@ -186,6 +186,7 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B
|
|||
// 旋转
|
||||
Radians: radians,
|
||||
FontColor: fontColor,
|
||||
Offset: series.Label.Offset,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -99,11 +99,25 @@ func (h *horizontalBarChart) render(result *defaultRenderResult, seriesList Seri
|
|||
DivideCount: defaultAxisDivideCount,
|
||||
Size: seriesPainter.Width(),
|
||||
})
|
||||
seriesNames := seriesList.Names()
|
||||
|
||||
rendererList := []Renderer{}
|
||||
for index := range seriesList {
|
||||
series := seriesList[index]
|
||||
seriesColor := theme.GetSeriesColor(series.index)
|
||||
divideValues := yRange.AutoDivide()
|
||||
|
||||
var labelPainter *SeriesLabelPainter
|
||||
if series.Label.Show {
|
||||
labelPainter = NewSeriesLabelPainter(SeriesLabelPainterParams{
|
||||
P: seriesPainter,
|
||||
SeriesNames: seriesNames,
|
||||
Label: series.Label,
|
||||
Theme: opt.Theme,
|
||||
Font: opt.Font,
|
||||
})
|
||||
rendererList = append(rendererList, labelPainter)
|
||||
}
|
||||
for j, item := range series.Data {
|
||||
if j >= yRange.divideCount {
|
||||
continue
|
||||
|
|
@ -130,8 +144,35 @@ func (h *horizontalBarChart) render(result *defaultRenderResult, seriesList Seri
|
|||
Right: right,
|
||||
Bottom: y + barHeight,
|
||||
})
|
||||
// 如果label不需要展示,则返回
|
||||
if labelPainter == nil {
|
||||
continue
|
||||
}
|
||||
x := right
|
||||
var fontColor Color
|
||||
if series.Label.Position == PositionLeft {
|
||||
x = 0
|
||||
if isLightColor(fillColor) {
|
||||
fontColor = defaultLightFontColor
|
||||
} else {
|
||||
fontColor = defaultDarkFontColor
|
||||
}
|
||||
}
|
||||
labelPainter.Add(LabelValue{
|
||||
Orient: OrientHorizontal,
|
||||
Index: index,
|
||||
Value: item.Value,
|
||||
X: x,
|
||||
Y: y + barHeight>>1,
|
||||
FontColor: fontColor,
|
||||
Offset: series.Label.Offset,
|
||||
})
|
||||
}
|
||||
}
|
||||
err := doRender(rendererList...)
|
||||
if err != nil {
|
||||
return BoxZero, err
|
||||
}
|
||||
return p.box, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -81,6 +81,8 @@ type SeriesLabel struct {
|
|||
Distance int
|
||||
// The position of label
|
||||
Position string
|
||||
// The offset of label's position
|
||||
Offset Box
|
||||
}
|
||||
|
||||
const (
|
||||
|
|
|
|||
|
|
@ -45,6 +45,8 @@ type LabelValue struct {
|
|||
Radians float64
|
||||
// 字体颜色
|
||||
FontColor Color
|
||||
Orient string
|
||||
Offset Box
|
||||
}
|
||||
|
||||
type SeriesLabelPainter struct {
|
||||
|
|
@ -103,10 +105,18 @@ func (o *SeriesLabelPainter) Add(value LabelValue) {
|
|||
renderValue := labelRenderValue{
|
||||
Text: text,
|
||||
Style: labelStyle,
|
||||
X: value.X - textBox.Width()>>1,
|
||||
Y: value.Y - distance,
|
||||
X: value.X,
|
||||
Y: value.Y,
|
||||
Radians: value.Radians,
|
||||
}
|
||||
if value.Orient != OrientHorizontal {
|
||||
renderValue.X -= textBox.Width() >> 1
|
||||
renderValue.Y -= distance
|
||||
} else {
|
||||
renderValue.X += distance
|
||||
renderValue.Y += textBox.Height() >> 1
|
||||
renderValue.Y -= 2
|
||||
}
|
||||
if rotated {
|
||||
renderValue.X = value.X + textBox.Width()>>1 - 1
|
||||
p.ClearTextRotation()
|
||||
|
|
@ -115,6 +125,8 @@ func (o *SeriesLabelPainter) Add(value LabelValue) {
|
|||
renderValue.X++
|
||||
}
|
||||
}
|
||||
renderValue.X += value.Offset.Left
|
||||
renderValue.Y += value.Offset.Top
|
||||
o.values = append(o.values, renderValue)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue