refactor: adjust series label render
This commit is contained in:
parent
de49ef8c68
commit
1f5b9d513e
4 changed files with 94 additions and 55 deletions
|
|
@ -22,29 +22,80 @@
|
|||
|
||||
package charts
|
||||
|
||||
import "github.com/wcharczuk/go-chart/v2"
|
||||
import (
|
||||
"github.com/golang/freetype/truetype"
|
||||
"github.com/wcharczuk/go-chart/v2"
|
||||
)
|
||||
|
||||
type LabelValue struct {
|
||||
type labelRenderValue struct {
|
||||
Text string
|
||||
Style Style
|
||||
X int
|
||||
Y int
|
||||
}
|
||||
|
||||
type SeriesLabelPainter struct {
|
||||
p *Painter
|
||||
values []LabelValue
|
||||
type LabelValue struct {
|
||||
Index int
|
||||
Value float64
|
||||
X int
|
||||
Y int
|
||||
}
|
||||
|
||||
func NewSeriesLabelPainter(p *Painter) *SeriesLabelPainter {
|
||||
type SeriesLabelPainter struct {
|
||||
p *Painter
|
||||
seriesNames []string
|
||||
label *SeriesLabel
|
||||
theme ColorPalette
|
||||
font *truetype.Font
|
||||
values []labelRenderValue
|
||||
}
|
||||
|
||||
type SeriesLabelPainterParams struct {
|
||||
P *Painter
|
||||
SeriesNames []string
|
||||
Label SeriesLabel
|
||||
Theme ColorPalette
|
||||
Font *truetype.Font
|
||||
}
|
||||
|
||||
func NewSeriesLabelPainter(params SeriesLabelPainterParams) *SeriesLabelPainter {
|
||||
return &SeriesLabelPainter{
|
||||
p: p,
|
||||
values: make([]LabelValue, 0),
|
||||
p: params.P,
|
||||
seriesNames: params.SeriesNames,
|
||||
label: ¶ms.Label,
|
||||
theme: params.Theme,
|
||||
font: params.Font,
|
||||
values: make([]labelRenderValue, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (o *SeriesLabelPainter) Add(value LabelValue) {
|
||||
o.values = append(o.values, value)
|
||||
label := o.label
|
||||
distance := label.Distance
|
||||
if distance == 0 {
|
||||
distance = 5
|
||||
}
|
||||
text := NewValueLabelFormatter(o.seriesNames, label.Formatter)(value.Index, value.Value, -1)
|
||||
labelStyle := Style{
|
||||
FontColor: o.theme.GetTextColor(),
|
||||
FontSize: labelFontSize,
|
||||
Font: o.font,
|
||||
}
|
||||
if !label.Color.IsZero() {
|
||||
labelStyle.FontColor = label.Color
|
||||
}
|
||||
o.p.OverrideDrawingStyle(labelStyle)
|
||||
textBox := o.p.MeasureText(text)
|
||||
renderValue := labelRenderValue{
|
||||
Text: text,
|
||||
Style: labelStyle,
|
||||
X: value.X - textBox.Width()>>1,
|
||||
Y: value.Y - distance,
|
||||
}
|
||||
if textBox.Width()%2 != 0 {
|
||||
renderValue.X++
|
||||
}
|
||||
o.values = append(o.values, renderValue)
|
||||
}
|
||||
|
||||
func (o *SeriesLabelPainter) Render() (Box, error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue