From 6db8e2c8dc5f5ead957474fddb4af20787b82b95 Mon Sep 17 00:00:00 2001 From: vicanso Date: Wed, 23 Nov 2022 23:01:52 +0800 Subject: [PATCH] feat: support series label for horizontal bar --- bar_chart.go | 1 + horizontal_bar_chart.go | 41 +++++++++++++++++++++++++++++++++++++++++ series.go | 2 ++ series_label.go | 16 ++++++++++++++-- 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/bar_chart.go b/bar_chart.go index 695b9fd..8219472 100644 --- a/bar_chart.go +++ b/bar_chart.go @@ -186,6 +186,7 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B // 旋转 Radians: radians, FontColor: fontColor, + Offset: series.Label.Offset, }) } diff --git a/horizontal_bar_chart.go b/horizontal_bar_chart.go index 58c6e19..5e433a6 100644 --- a/horizontal_bar_chart.go +++ b/horizontal_bar_chart.go @@ -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 } diff --git a/series.go b/series.go index 373c7dc..c36fa8b 100644 --- a/series.go +++ b/series.go @@ -81,6 +81,8 @@ type SeriesLabel struct { Distance int // The position of label Position string + // The offset of label's position + Offset Box } const ( diff --git a/series_label.go b/series_label.go index f2dd40f..f0fb2ec 100644 --- a/series_label.go +++ b/series_label.go @@ -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) }