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,
|
Radians: radians,
|
||||||
FontColor: fontColor,
|
FontColor: fontColor,
|
||||||
|
Offset: series.Label.Offset,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -99,11 +99,25 @@ func (h *horizontalBarChart) render(result *defaultRenderResult, seriesList Seri
|
||||||
DivideCount: defaultAxisDivideCount,
|
DivideCount: defaultAxisDivideCount,
|
||||||
Size: seriesPainter.Width(),
|
Size: seriesPainter.Width(),
|
||||||
})
|
})
|
||||||
|
seriesNames := seriesList.Names()
|
||||||
|
|
||||||
|
rendererList := []Renderer{}
|
||||||
for index := range seriesList {
|
for index := range seriesList {
|
||||||
series := seriesList[index]
|
series := seriesList[index]
|
||||||
seriesColor := theme.GetSeriesColor(series.index)
|
seriesColor := theme.GetSeriesColor(series.index)
|
||||||
divideValues := yRange.AutoDivide()
|
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 {
|
for j, item := range series.Data {
|
||||||
if j >= yRange.divideCount {
|
if j >= yRange.divideCount {
|
||||||
continue
|
continue
|
||||||
|
|
@ -130,8 +144,35 @@ func (h *horizontalBarChart) render(result *defaultRenderResult, seriesList Seri
|
||||||
Right: right,
|
Right: right,
|
||||||
Bottom: y + barHeight,
|
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
|
return p.box, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -81,6 +81,8 @@ type SeriesLabel struct {
|
||||||
Distance int
|
Distance int
|
||||||
// The position of label
|
// The position of label
|
||||||
Position string
|
Position string
|
||||||
|
// The offset of label's position
|
||||||
|
Offset Box
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,8 @@ type LabelValue struct {
|
||||||
Radians float64
|
Radians float64
|
||||||
// 字体颜色
|
// 字体颜色
|
||||||
FontColor Color
|
FontColor Color
|
||||||
|
Orient string
|
||||||
|
Offset Box
|
||||||
}
|
}
|
||||||
|
|
||||||
type SeriesLabelPainter struct {
|
type SeriesLabelPainter struct {
|
||||||
|
|
@ -103,10 +105,18 @@ func (o *SeriesLabelPainter) Add(value LabelValue) {
|
||||||
renderValue := labelRenderValue{
|
renderValue := labelRenderValue{
|
||||||
Text: text,
|
Text: text,
|
||||||
Style: labelStyle,
|
Style: labelStyle,
|
||||||
X: value.X - textBox.Width()>>1,
|
X: value.X,
|
||||||
Y: value.Y - distance,
|
Y: value.Y,
|
||||||
Radians: value.Radians,
|
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 {
|
if rotated {
|
||||||
renderValue.X = value.X + textBox.Width()>>1 - 1
|
renderValue.X = value.X + textBox.Width()>>1 - 1
|
||||||
p.ClearTextRotation()
|
p.ClearTextRotation()
|
||||||
|
|
@ -115,6 +125,8 @@ func (o *SeriesLabelPainter) Add(value LabelValue) {
|
||||||
renderValue.X++
|
renderValue.X++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
renderValue.X += value.Offset.Left
|
||||||
|
renderValue.Y += value.Offset.Top
|
||||||
o.values = append(o.values, renderValue)
|
o.values = append(o.values, renderValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue