Debian Codegraph @

style.go

raw symbols SCIP
1 package chart
2
3 import (
4 "fmt"
5 "strings"
6
7 "github.com/golang/freetype/truetype"
8 "github.com/wcharczuk/go-chart/v2/drawing"
9 )
10
11 const (
12 // Disabled indicates if the value should be interpreted as set intentionally to zero.
13 // this is because golang optionals aren't here yet.
14 Disabled = -1
15 )
16
17 // Hidden is a prebuilt style with the `Hidden` property set to true.
18 func Hidden() Style {
19 return Style{
20 Hidden: true,
21 }
22 }
23
24 // Shown is a prebuilt style with the `Hidden` property set to false.
25 // You can also think of this as the default.
26 func Shown() Style {
27 return Style{
28 Hidden: false,
29 }
30 }
31
32 // StyleTextDefaults returns a style for drawing outside a
33 // chart context.
34 func StyleTextDefaults() Style {
35 font, _ := GetDefaultFont()
36 return Style{
37 Hidden: false,
38 Font: font,
39 FontColor: DefaultTextColor,
40 FontSize: DefaultTitleFontSize,
41 }
42 }
43
44 // Style is a simple style set.
45 type Style struct {
46 Hidden bool
47 Padding Box
48
49 ClassName string
50
51 StrokeWidth float64
52 StrokeColor drawing.Color
53 StrokeDashArray []float64
54
55 DotColor drawing.Color
56 DotWidth float64
57
58 DotWidthProvider SizeProvider
59 DotColorProvider DotColorProvider
60
61 FillColor drawing.Color
62
63 FontSize float64
64 FontColor drawing.Color
65 Font *truetype.Font
66
67 TextHorizontalAlign TextHorizontalAlign
68 TextVerticalAlign TextVerticalAlign
69 TextWrap TextWrap
70 TextLineSpacing int
71 TextRotationDegrees float64 //0 is unset or normal
72 }
73
74 // IsZero returns if the object is set or not.
75 func (s Style) IsZero() bool {
76 return !s.Hidden &&
77 s.StrokeColor.IsZero() &&
78 s.StrokeWidth == 0 &&
79 s.DotColor.IsZero() &&
80 s.DotWidth == 0 &&
81 s.FillColor.IsZero() &&
82 s.FontColor.IsZero() &&
83 s.FontSize == 0 &&
84 s.Font == nil &&
85 s.ClassName == ""
86 }
87
88 // String returns a text representation of the style.
89 func (s Style) String() string {
90 if s.IsZero() {
91 return "{}"
92 }
93
94 var output []string
95 if s.Hidden {
96 output = []string{"\"hidden\": true"}
97 } else {
98 output = []string{"\"hidden\": false"}
99 }
100
101 if s.ClassName != "" {
102 output = append(output, fmt.Sprintf("\"class_name\": %s", s.ClassName))
103 } else {
104 output = append(output, "\"class_name\": null")
105 }
106
107 if !s.Padding.IsZero() {
108 output = append(output, fmt.Sprintf("\"padding\": %s", s.Padding.String()))
109 } else {
110 output = append(output, "\"padding\": null")
111 }
112
113 if s.StrokeWidth >= 0 {
114 output = append(output, fmt.Sprintf("\"stroke_width\": %0.2f", s.StrokeWidth))
115 } else {
116 output = append(output, "\"stroke_width\": null")
117 }
118
119 if !s.StrokeColor.IsZero() {
120 output = append(output, fmt.Sprintf("\"stroke_color\": %s", s.StrokeColor.String()))
121 } else {
122 output = append(output, "\"stroke_color\": null")
123 }
124
125 if len(s.StrokeDashArray) > 0 {
126 var elements []string
127 for _, v := range s.StrokeDashArray {
128 elements = append(elements, fmt.Sprintf("%.2f", v))
129 }
130 dashArray := strings.Join(elements, ", ")
131 output = append(output, fmt.Sprintf("\"stroke_dash_array\": [%s]", dashArray))
132 } else {
133 output = append(output, "\"stroke_dash_array\": null")
134 }
135
136 if s.DotWidth >= 0 {
137 output = append(output, fmt.Sprintf("\"dot_width\": %0.2f", s.DotWidth))
138 } else {
139 output = append(output, "\"dot_width\": null")
140 }
141
142 if !s.DotColor.IsZero() {
143 output = append(output, fmt.Sprintf("\"dot_color\": %s", s.DotColor.String()))
144 } else {
145 output = append(output, "\"dot_color\": null")
146 }
147
148 if !s.FillColor.IsZero() {
149 output = append(output, fmt.Sprintf("\"fill_color\": %s", s.FillColor.String()))
150 } else {
151 output = append(output, "\"fill_color\": null")
152 }
153
154 if s.FontSize != 0 {
155 output = append(output, fmt.Sprintf("\"font_size\": \"%0.2fpt\"", s.FontSize))
156 } else {
157 output = append(output, "\"font_size\": null")
158 }
159
160 if !s.FontColor.IsZero() {
161 output = append(output, fmt.Sprintf("\"font_color\": %s", s.FontColor.String()))
162 } else {
163 output = append(output, "\"font_color\": null")
164 }
165
166 if s.Font != nil {
167 output = append(output, fmt.Sprintf("\"font\": \"%s\"", s.Font.Name(truetype.NameIDFontFamily)))
168 } else {
169 output = append(output, "\"font_color\": null")
170 }
171
172 return "{" + strings.Join(output, ", ") + "}"
173 }
174
175 // GetClassName returns the class name or a default.
176 func (s Style) GetClassName(defaults ...string) string {
177 if s.ClassName == "" {
178 if len(defaults) > 0 {
179 return defaults[0]
180 }
181 return ""
182 }
183 return s.ClassName
184 }
185
186 // GetStrokeColor returns the stroke color.
187 func (s Style) GetStrokeColor(defaults ...drawing.Color) drawing.Color {
188 if s.StrokeColor.IsZero() {
189 if len(defaults) > 0 {
190 return defaults[0]
191 }
192 return drawing.ColorTransparent
193 }
194 return s.StrokeColor
195 }
196
197 // GetFillColor returns the fill color.
198 func (s Style) GetFillColor(defaults ...drawing.Color) drawing.Color {
199 if s.FillColor.IsZero() {
200 if len(defaults) > 0 {
201 return defaults[0]
202 }
203 return drawing.ColorTransparent
204 }
205 return s.FillColor
206 }
207
208 // GetDotColor returns the stroke color.
209 func (s Style) GetDotColor(defaults ...drawing.Color) drawing.Color {
210 if s.DotColor.IsZero() {
211 if len(defaults) > 0 {
212 return defaults[0]
213 }
214 return drawing.ColorTransparent
215 }
216 return s.DotColor
217 }
218
219 // GetStrokeWidth returns the stroke width.
220 func (s Style) GetStrokeWidth(defaults ...float64) float64 {
221 if s.StrokeWidth == 0 {
222 if len(defaults) > 0 {
223 return defaults[0]
224 }
225 return DefaultStrokeWidth
226 }
227 return s.StrokeWidth
228 }
229
230 // GetDotWidth returns the dot width for scatter plots.
231 func (s Style) GetDotWidth(defaults ...float64) float64 {
232 if s.DotWidth == 0 {
233 if len(defaults) > 0 {
234 return defaults[0]
235 }
236 return DefaultDotWidth
237 }
238 return s.DotWidth
239 }
240
241 // GetStrokeDashArray returns the stroke dash array.
242 func (s Style) GetStrokeDashArray(defaults ...[]float64) []float64 {
243 if len(s.StrokeDashArray) == 0 {
244 if len(defaults) > 0 {
245 return defaults[0]
246 }
247 return nil
248 }
249 return s.StrokeDashArray
250 }
251
252 // GetFontSize gets the font size.
253 func (s Style) GetFontSize(defaults ...float64) float64 {
254 if s.FontSize == 0 {
255 if len(defaults) > 0 {
256 return defaults[0]
257 }
258 return DefaultFontSize
259 }
260 return s.FontSize
261 }
262
263 // GetFontColor gets the font size.
264 func (s Style) GetFontColor(defaults ...drawing.Color) drawing.Color {
265 if s.FontColor.IsZero() {
266 if len(defaults) > 0 {
267 return defaults[0]
268 }
269 return drawing.ColorTransparent
270 }
271 return s.FontColor
272 }
273
274 // GetFont returns the font face.
275 func (s Style) GetFont(defaults ...*truetype.Font) *truetype.Font {
276 if s.Font == nil {
277 if len(defaults) > 0 {
278 return defaults[0]
279 }
280 return nil
281 }
282 return s.Font
283 }
284
285 // GetPadding returns the padding.
286 func (s Style) GetPadding(defaults ...Box) Box {
287 if s.Padding.IsZero() {
288 if len(defaults) > 0 {
289 return defaults[0]
290 }
291 return Box{}
292 }
293 return s.Padding
294 }
295
296 // GetTextHorizontalAlign returns the horizontal alignment.
297 func (s Style) GetTextHorizontalAlign(defaults ...TextHorizontalAlign) TextHorizontalAlign {
298 if s.TextHorizontalAlign == TextHorizontalAlignUnset {
299 if len(defaults) > 0 {
300 return defaults[0]
301 }
302 return TextHorizontalAlignUnset
303 }
304 return s.TextHorizontalAlign
305 }
306
307 // GetTextVerticalAlign returns the vertical alignment.
308 func (s Style) GetTextVerticalAlign(defaults ...TextVerticalAlign) TextVerticalAlign {
309 if s.TextVerticalAlign == TextVerticalAlignUnset {
310 if len(defaults) > 0 {
311 return defaults[0]
312 }
313 return TextVerticalAlignUnset
314 }
315 return s.TextVerticalAlign
316 }
317
318 // GetTextWrap returns the word wrap.
319 func (s Style) GetTextWrap(defaults ...TextWrap) TextWrap {
320 if s.TextWrap == TextWrapUnset {
321 if len(defaults) > 0 {
322 return defaults[0]
323 }
324 return TextWrapUnset
325 }
326 return s.TextWrap
327 }
328
329 // GetTextLineSpacing returns the spacing in pixels between lines of text (vertically).
330 func (s Style) GetTextLineSpacing(defaults ...int) int {
331 if s.TextLineSpacing == 0 {
332 if len(defaults) > 0 {
333 return defaults[0]
334 }
335 return DefaultLineSpacing
336 }
337 return s.TextLineSpacing
338 }
339
340 // GetTextRotationDegrees returns the text rotation in degrees.
341 func (s Style) GetTextRotationDegrees(defaults ...float64) float64 {
342 if s.TextRotationDegrees == 0 {
343 if len(defaults) > 0 {
344 return defaults[0]
345 }
346 }
347 return s.TextRotationDegrees
348 }
349
350 // WriteToRenderer passes the style's options to a renderer.
351 func (s Style) WriteToRenderer(r Renderer) {
352 r.SetClassName(s.GetClassName())
353 r.SetStrokeColor(s.GetStrokeColor())
354 r.SetStrokeWidth(s.GetStrokeWidth())
355 r.SetStrokeDashArray(s.GetStrokeDashArray())
356 r.SetFillColor(s.GetFillColor())
357 r.SetFont(s.GetFont())
358 r.SetFontColor(s.GetFontColor())
359 r.SetFontSize(s.GetFontSize())
360
361 r.ClearTextRotation()
362 if s.GetTextRotationDegrees() != 0 {
363 r.SetTextRotation(DegreesToRadians(s.GetTextRotationDegrees()))
364 }
365 }
366
367 // WriteDrawingOptionsToRenderer passes just the drawing style options to a renderer.
368 func (s Style) WriteDrawingOptionsToRenderer(r Renderer) {
369 r.SetClassName(s.GetClassName())
370 r.SetStrokeColor(s.GetStrokeColor())
371 r.SetStrokeWidth(s.GetStrokeWidth())
372 r.SetStrokeDashArray(s.GetStrokeDashArray())
373 r.SetFillColor(s.GetFillColor())
374 }
375
376 // WriteTextOptionsToRenderer passes just the text style options to a renderer.
377 func (s Style) WriteTextOptionsToRenderer(r Renderer) {
378 r.SetClassName(s.GetClassName())
379 r.SetFont(s.GetFont())
380 r.SetFontColor(s.GetFontColor())
381 r.SetFontSize(s.GetFontSize())
382 }
383
384 // InheritFrom coalesces two styles into a new style.
385 func (s Style) InheritFrom(defaults Style) (final Style) {
386 final.ClassName = s.GetClassName(defaults.ClassName)
387
388 final.StrokeColor = s.GetStrokeColor(defaults.StrokeColor)
389 final.StrokeWidth = s.GetStrokeWidth(defaults.StrokeWidth)
390 final.StrokeDashArray = s.GetStrokeDashArray(defaults.StrokeDashArray)
391
392 final.DotColor = s.GetDotColor(defaults.DotColor)
393 final.DotWidth = s.GetDotWidth(defaults.DotWidth)
394
395 final.DotWidthProvider = s.DotWidthProvider
396 final.DotColorProvider = s.DotColorProvider
397
398 final.FillColor = s.GetFillColor(defaults.FillColor)
399 final.FontColor = s.GetFontColor(defaults.FontColor)
400 final.FontSize = s.GetFontSize(defaults.FontSize)
401 final.Font = s.GetFont(defaults.Font)
402 final.Padding = s.GetPadding(defaults.Padding)
403 final.TextHorizontalAlign = s.GetTextHorizontalAlign(defaults.TextHorizontalAlign)
404 final.TextVerticalAlign = s.GetTextVerticalAlign(defaults.TextVerticalAlign)
405 final.TextWrap = s.GetTextWrap(defaults.TextWrap)
406 final.TextLineSpacing = s.GetTextLineSpacing(defaults.TextLineSpacing)
407 final.TextRotationDegrees = s.GetTextRotationDegrees(defaults.TextRotationDegrees)
408
409 return
410 }
411
412 // GetStrokeOptions returns the stroke components.
413 func (s Style) GetStrokeOptions() Style {
414 return Style{
415 ClassName: s.ClassName,
416 StrokeDashArray: s.StrokeDashArray,
417 StrokeColor: s.StrokeColor,
418 StrokeWidth: s.StrokeWidth,
419 }
420 }
421
422 // GetFillOptions returns the fill components.
423 func (s Style) GetFillOptions() Style {
424 return Style{
425 ClassName: s.ClassName,
426 FillColor: s.FillColor,
427 }
428 }
429
430 // GetDotOptions returns the dot components.
431 func (s Style) GetDotOptions() Style {
432 return Style{
433 ClassName: s.ClassName,
434 StrokeDashArray: nil,
435 FillColor: s.DotColor,
436 StrokeColor: s.DotColor,
437 StrokeWidth: 1.0,
438 }
439 }
440
441 // GetFillAndStrokeOptions returns the fill and stroke components.
442 func (s Style) GetFillAndStrokeOptions() Style {
443 return Style{
444 ClassName: s.ClassName,
445 StrokeDashArray: s.StrokeDashArray,
446 FillColor: s.FillColor,
447 StrokeColor: s.StrokeColor,
448 StrokeWidth: s.StrokeWidth,
449 }
450 }
451
452 // GetTextOptions returns just the text components of the style.
453 func (s Style) GetTextOptions() Style {
454 return Style{
455 ClassName: s.ClassName,
456 FontColor: s.FontColor,
457 FontSize: s.FontSize,
458 Font: s.Font,
459 TextHorizontalAlign: s.TextHorizontalAlign,
460 TextVerticalAlign: s.TextVerticalAlign,
461 TextWrap: s.TextWrap,
462 TextLineSpacing: s.TextLineSpacing,
463 TextRotationDegrees: s.TextRotationDegrees,
464 }
465 }
466
467 // ShouldDrawStroke tells drawing functions if they should draw the stroke.
468 func (s Style) ShouldDrawStroke() bool {
469 return !s.StrokeColor.IsZero() && s.StrokeWidth > 0
470 }
471
472 // ShouldDrawDot tells drawing functions if they should draw the dot.
473 func (s Style) ShouldDrawDot() bool {
474 return (!s.DotColor.IsZero() && s.DotWidth > 0) || s.DotColorProvider != nil || s.DotWidthProvider != nil
475 }
476
477 // ShouldDrawFill tells drawing functions if they should draw the stroke.
478 func (s Style) ShouldDrawFill() bool {
479 return !s.FillColor.IsZero()
480 }