-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathseries_label.go
101 lines (93 loc) · 2.45 KB
/
series_label.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package charts
import (
"github.com/golang/freetype/truetype"
"github.com/go-analyze/charts/chartdraw"
)
type labelRenderValue struct {
Text string
FontStyle FontStyle
X int
Y int
Radians float64
}
type labelValue struct {
index int
value float64
x int
y int
radians float64
fontStyle FontStyle
vertical bool
offset OffsetInt
}
type seriesLabelPainter struct {
p *Painter
seriesNames []string
label *SeriesLabel
theme ColorPalette
font *truetype.Font
values []labelRenderValue
}
func newSeriesLabelPainter(p *Painter, seriesNames []string, label SeriesLabel,
theme ColorPalette, font *truetype.Font) *seriesLabelPainter {
return &seriesLabelPainter{
p: p,
seriesNames: seriesNames,
label: &label,
theme: theme,
font: font,
values: make([]labelRenderValue, 0),
}
}
func (o *seriesLabelPainter) Add(value labelValue) {
label := o.label
distance := label.Distance
if distance == 0 {
distance = 5
}
text := labelFormatValue(o.seriesNames, label.Formatter, value.index, value.value, -1)
labelStyle := FontStyle{
FontColor: o.theme.GetTextColor(),
FontSize: labelFontSize,
Font: getPreferredFont(label.FontStyle.Font, value.fontStyle.Font, o.font),
}
if label.FontStyle.FontSize != 0 {
labelStyle.FontSize = label.FontStyle.FontSize
} else if value.fontStyle.FontSize != 0 {
labelStyle.FontSize = value.fontStyle.FontSize
}
if !label.FontStyle.FontColor.IsZero() {
labelStyle.FontColor = label.FontStyle.FontColor
} else if !value.fontStyle.FontColor.IsZero() {
labelStyle.FontColor = value.fontStyle.FontColor
}
p := o.p
textBox := p.MeasureText(text, value.radians, labelStyle)
renderValue := labelRenderValue{
Text: text,
FontStyle: labelStyle,
X: value.x,
Y: value.y,
Radians: value.radians,
}
if value.vertical {
renderValue.X -= textBox.Width() >> 1
renderValue.Y -= distance
} else {
renderValue.X += distance
renderValue.Y += textBox.Height() >> 1
renderValue.Y -= 2
}
if value.radians != 0 {
renderValue.X = value.x + (textBox.Width() >> 1) - 1
}
renderValue.X += value.offset.Left
renderValue.Y += value.offset.Top
o.values = append(o.values, renderValue)
}
func (o *seriesLabelPainter) Render() (Box, error) {
for _, item := range o.values {
o.p.Text(item.Text, item.X, item.Y, item.Radians, item.FontStyle)
}
return chartdraw.BoxZero, nil
}