-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmark_point.go
90 lines (82 loc) · 2.45 KB
/
mark_point.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
package charts
import (
"github.com/golang/freetype/truetype"
)
// NewMarkPoint returns a mark point for the provided types, this is set on a specific instance within a Series.
func NewMarkPoint(markPointTypes ...string) SeriesMarkPoint {
return SeriesMarkPoint{
Points: NewSeriesMarkList(markPointTypes...),
}
}
type markPointPainter struct {
p *Painter
options []markPointRenderOption
}
func (m *markPointPainter) add(opt markPointRenderOption) {
if opt.valueFormatter == nil {
opt.valueFormatter = defaultValueFormatter
}
if opt.symbolSize == 0 {
opt.symbolSize = 28
}
m.options = append(m.options, opt)
}
type markPointRenderOption struct {
fillColor Color
font *truetype.Font
symbolSize int
seriesValues []float64
markpoints []SeriesMark
seriesLabelPainter *seriesLabelPainter
points []Point
valueFormatter ValueFormatter
}
// newMarkPointPainter returns a mark point renderer
func newMarkPointPainter(p *Painter) *markPointPainter {
return &markPointPainter{
p: p,
}
}
func (m *markPointPainter) Render() (Box, error) {
painter := m.p
for _, opt := range m.options {
if len(opt.markpoints) == 0 {
continue
}
summary := summarizePopulationData(opt.seriesValues)
textStyle := FontStyle{
FontSize: defaultLabelFontSize,
Font: opt.font,
}
if isLightColor(opt.fillColor) {
textStyle.FontColor = defaultLightFontColor
} else {
textStyle.FontColor = defaultDarkFontColor
}
for _, markPointData := range opt.markpoints {
textStyle.FontSize = defaultLabelFontSize
index := summary.MinIndex
value := summary.Min
switch markPointData.Type {
case SeriesMarkTypeMax:
index = summary.MaxIndex
value = summary.Max
}
p := opt.points[index]
if opt.seriesLabelPainter != nil {
// the series label has been replaced by our MarkPoint
// This is why MarkPoints must be rendered BEFORE series labels
opt.seriesLabelPainter.values[index].Text = ""
}
painter.Pin(p.X, p.Y-opt.symbolSize>>1, opt.symbolSize, opt.fillColor, opt.fillColor, 0.0)
text := opt.valueFormatter(value)
textBox := painter.MeasureText(text, 0, textStyle)
if textStyle.FontSize > smallLabelFontSize && textBox.Width() > opt.symbolSize {
textStyle.FontSize = smallLabelFontSize
textBox = painter.MeasureText(text, 0, textStyle)
}
painter.Text(text, p.X-textBox.Width()>>1, p.Y-opt.symbolSize>>1-2, 0, textStyle)
}
}
return BoxZero, nil
}