Debian Codegraph @

legend.go

raw symbols SCIP
1 package chart
2
3 import (
4 "github.com/wcharczuk/go-chart/v2/drawing"
5 )
6
7 // Legend returns a legend renderable function.
8 func Legend(c *Chart, userDefaults ...Style) Renderable {
9 return func(r Renderer, cb Box, chartDefaults Style) {
10 legendDefaults := Style{
11 FillColor: drawing.ColorWhite,
12 FontColor: DefaultTextColor,
13 FontSize: 8.0,
14 StrokeColor: DefaultAxisColor,
15 StrokeWidth: DefaultAxisLineWidth,
16 }
17
18 var legendStyle Style
19 if len(userDefaults) > 0 {
20 legendStyle = userDefaults[0].InheritFrom(chartDefaults.InheritFrom(legendDefaults))
21 } else {
22 legendStyle = chartDefaults.InheritFrom(legendDefaults)
23 }
24
25 // DEFAULTS
26 legendPadding := Box{
27 Top: 5,
28 Left: 5,
29 Right: 5,
30 Bottom: 5,
31 }
32 lineTextGap := 5
33 lineLengthMinimum := 25
34
35 var labels []string
36 var lines []Style
37 for index, s := range c.Series {
38 if !s.GetStyle().Hidden {
39 if _, isAnnotationSeries := s.(AnnotationSeries); !isAnnotationSeries {
40 labels = append(labels, s.GetName())
41 lines = append(lines, s.GetStyle().InheritFrom(c.styleDefaultsSeries(index)))
42 }
43 }
44 }
45
46 legend := Box{
47 Top: cb.Top,
48 Left: cb.Left,
49 // bottom and right will be sized by the legend content + relevant padding.
50 }
51
52 legendContent := Box{
53 Top: legend.Top + legendPadding.Top,
54 Left: legend.Left + legendPadding.Left,
55 Right: legend.Left + legendPadding.Left,
56 Bottom: legend.Top + legendPadding.Top,
57 }
58
59 legendStyle.GetTextOptions().WriteToRenderer(r)
60
61 // measure
62 labelCount := 0
63 for x := 0; x < len(labels); x++ {
64 if len(labels[x]) > 0 {
65 tb := r.MeasureText(labels[x])
66 if labelCount > 0 {
67 legendContent.Bottom += DefaultMinimumTickVerticalSpacing
68 }
69 legendContent.Bottom += tb.Height()
70 right := legendContent.Left + tb.Width() + lineTextGap + lineLengthMinimum
71 legendContent.Right = MaxInt(legendContent.Right, right)
72 labelCount++
73 }
74 }
75
76 legend = legend.Grow(legendContent)
77 legend.Right = legendContent.Right + legendPadding.Right
78 legend.Bottom = legendContent.Bottom + legendPadding.Bottom
79
80 Draw.Box(r, legend, legendStyle)
81
82 legendStyle.GetTextOptions().WriteToRenderer(r)
83
84 ycursor := legendContent.Top
85 tx := legendContent.Left
86 legendCount := 0
87 var label string
88 for x := 0; x < len(labels); x++ {
89 label = labels[x]
90 if len(label) > 0 {
91 if legendCount > 0 {
92 ycursor += DefaultMinimumTickVerticalSpacing
93 }
94
95 tb := r.MeasureText(label)
96
97 ty := ycursor + tb.Height()
98 r.Text(label, tx, ty)
99
100 th2 := tb.Height() >> 1
101
102 lx := tx + tb.Width() + lineTextGap
103 ly := ty - th2
104 lx2 := legendContent.Right - legendPadding.Right
105
106 r.SetStrokeColor(lines[x].GetStrokeColor())
107 r.SetStrokeWidth(lines[x].GetStrokeWidth())
108 r.SetStrokeDashArray(lines[x].GetStrokeDashArray())
109
110 r.MoveTo(lx, ly)
111 r.LineTo(lx2, ly)
112 r.Stroke()
113
114 ycursor += tb.Height()
115 legendCount++
116 }
117 }
118 }
119 }
120
121 // LegendThin is a legend that doesn't obscure the chart area.
122 func LegendThin(c *Chart, userDefaults ...Style) Renderable {
123 return func(r Renderer, cb Box, chartDefaults Style) {
124 legendDefaults := Style{
125 FillColor: drawing.ColorWhite,
126 FontColor: DefaultTextColor,
127 FontSize: 8.0,
128 StrokeColor: DefaultAxisColor,
129 StrokeWidth: DefaultAxisLineWidth,
130 Padding: Box{
131 Top: 2,
132 Left: 7,
133 Right: 7,
134 Bottom: 5,
135 },
136 }
137
138 var legendStyle Style
139 if len(userDefaults) > 0 {
140 legendStyle = userDefaults[0].InheritFrom(chartDefaults.InheritFrom(legendDefaults))
141 } else {
142 legendStyle = chartDefaults.InheritFrom(legendDefaults)
143 }
144
145 r.SetFont(legendStyle.GetFont())
146 r.SetFontColor(legendStyle.GetFontColor())
147 r.SetFontSize(legendStyle.GetFontSize())
148
149 var labels []string
150 var lines []Style
151 for index, s := range c.Series {
152 if !s.GetStyle().Hidden {
153 if _, isAnnotationSeries := s.(AnnotationSeries); !isAnnotationSeries {
154 labels = append(labels, s.GetName())
155 lines = append(lines, s.GetStyle().InheritFrom(c.styleDefaultsSeries(index)))
156 }
157 }
158 }
159
160 var textHeight int
161 var textWidth int
162 var textBox Box
163 for x := 0; x < len(labels); x++ {
164 if len(labels[x]) > 0 {
165 textBox = r.MeasureText(labels[x])
166 textHeight = MaxInt(textBox.Height(), textHeight)
167 textWidth = MaxInt(textBox.Width(), textWidth)
168 }
169 }
170
171 legendBoxHeight := textHeight + legendStyle.Padding.Top + legendStyle.Padding.Bottom
172 chartPadding := cb.Top
173 legendYMargin := (chartPadding - legendBoxHeight) >> 1
174
175 legendBox := Box{
176 Left: cb.Left,
177 Right: cb.Right,
178 Top: legendYMargin,
179 Bottom: legendYMargin + legendBoxHeight,
180 }
181
182 Draw.Box(r, legendBox, legendDefaults)
183
184 r.SetFont(legendStyle.GetFont())
185 r.SetFontColor(legendStyle.GetFontColor())
186 r.SetFontSize(legendStyle.GetFontSize())
187
188 lineTextGap := 5
189 lineLengthMinimum := 25
190
191 tx := legendBox.Left + legendStyle.Padding.Left
192 ty := legendYMargin + legendStyle.Padding.Top + textHeight
193 var label string
194 var lx, ly int
195 th2 := textHeight >> 1
196 for index := range labels {
197 label = labels[index]
198 if len(label) > 0 {
199 textBox = r.MeasureText(label)
200 r.Text(label, tx, ty)
201
202 lx = tx + textBox.Width() + lineTextGap
203 ly = ty - th2
204
205 r.SetStrokeColor(lines[index].GetStrokeColor())
206 r.SetStrokeWidth(lines[index].GetStrokeWidth())
207 r.SetStrokeDashArray(lines[index].GetStrokeDashArray())
208
209 r.MoveTo(lx, ly)
210 r.LineTo(lx+lineLengthMinimum, ly)
211 r.Stroke()
212
213 tx += textBox.Width() + DefaultMinimumTickHorizontalSpacing + lineTextGap + lineLengthMinimum
214 }
215 }
216 }
217 }
218
219 // LegendLeft is a legend that is designed for longer series lists.
220 func LegendLeft(c *Chart, userDefaults ...Style) Renderable {
221 return func(r Renderer, cb Box, chartDefaults Style) {
222 legendDefaults := Style{
223 FillColor: drawing.ColorWhite,
224 FontColor: DefaultTextColor,
225 FontSize: 8.0,
226 StrokeColor: DefaultAxisColor,
227 StrokeWidth: DefaultAxisLineWidth,
228 }
229
230 var legendStyle Style
231 if len(userDefaults) > 0 {
232 legendStyle = userDefaults[0].InheritFrom(chartDefaults.InheritFrom(legendDefaults))
233 } else {
234 legendStyle = chartDefaults.InheritFrom(legendDefaults)
235 }
236
237 // DEFAULTS
238 legendPadding := Box{
239 Top: 5,
240 Left: 5,
241 Right: 5,
242 Bottom: 5,
243 }
244 lineTextGap := 5
245 lineLengthMinimum := 25
246
247 var labels []string
248 var lines []Style
249 for index, s := range c.Series {
250 if !s.GetStyle().Hidden {
251 if _, isAnnotationSeries := s.(AnnotationSeries); !isAnnotationSeries {
252 labels = append(labels, s.GetName())
253 lines = append(lines, s.GetStyle().InheritFrom(c.styleDefaultsSeries(index)))
254 }
255 }
256 }
257
258 legend := Box{
259 Top: 5,
260 Left: 5,
261 // bottom and right will be sized by the legend content + relevant padding.
262 }
263
264 legendContent := Box{
265 Top: legend.Top + legendPadding.Top,
266 Left: legend.Left + legendPadding.Left,
267 Right: legend.Left + legendPadding.Left,
268 Bottom: legend.Top + legendPadding.Top,
269 }
270
271 legendStyle.GetTextOptions().WriteToRenderer(r)
272
273 // measure
274 labelCount := 0
275 for x := 0; x < len(labels); x++ {
276 if len(labels[x]) > 0 {
277 tb := r.MeasureText(labels[x])
278 if labelCount > 0 {
279 legendContent.Bottom += DefaultMinimumTickVerticalSpacing
280 }
281 legendContent.Bottom += tb.Height()
282 right := legendContent.Left + tb.Width() + lineTextGap + lineLengthMinimum
283 legendContent.Right = MaxInt(legendContent.Right, right)
284 labelCount++
285 }
286 }
287
288 legend = legend.Grow(legendContent)
289 legend.Right = legendContent.Right + legendPadding.Right
290 legend.Bottom = legendContent.Bottom + legendPadding.Bottom
291
292 Draw.Box(r, legend, legendStyle)
293
294 legendStyle.GetTextOptions().WriteToRenderer(r)
295
296 ycursor := legendContent.Top
297 tx := legendContent.Left
298 legendCount := 0
299 var label string
300 for x := 0; x < len(labels); x++ {
301 label = labels[x]
302 if len(label) > 0 {
303 if legendCount > 0 {
304 ycursor += DefaultMinimumTickVerticalSpacing
305 }
306
307 tb := r.MeasureText(label)
308
309 ty := ycursor + tb.Height()
310 r.Text(label, tx, ty)
311
312 th2 := tb.Height() >> 1
313
314 lx := tx + tb.Width() + lineTextGap
315 ly := ty - th2
316 lx2 := legendContent.Right - legendPadding.Right
317
318 r.SetStrokeColor(lines[x].GetStrokeColor())
319 r.SetStrokeWidth(lines[x].GetStrokeWidth())
320 r.SetStrokeDashArray(lines[x].GetStrokeDashArray())
321
322 r.MoveTo(lx, ly)
323 r.LineTo(lx2, ly)
324 r.Stroke()
325
326 ycursor += tb.Height()
327 legendCount++
328 }
329 }
330 }
331 }