forked from wcharczuk/go-chart
/
style.go
292 lines (253 loc) · 6.91 KB
/
style.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package chart
import (
"fmt"
"strings"
"github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/drawing"
)
// Style is a simple style set.
type Style struct {
Show bool
Padding Box
StrokeWidth float64
StrokeColor drawing.Color
StrokeDashArray []float64
FillColor drawing.Color
FontSize float64
FontColor drawing.Color
Font *truetype.Font
}
// IsZero returns if the object is set or not.
func (s Style) IsZero() bool {
return s.StrokeColor.IsZero() && s.FillColor.IsZero() && s.StrokeWidth == 0 && s.FontColor.IsZero() && s.FontSize == 0 && s.Font == nil
}
func (s Style) String() string {
if s.IsZero() {
return "{}"
}
var output []string
if s.Show {
output = []string{"\"show\": true"}
} else {
output = []string{"\"show\": false"}
}
if !s.Padding.IsZero() {
output = append(output, fmt.Sprintf("\"padding\": %s", s.Padding.String()))
} else {
output = append(output, "\"padding\": null")
}
if s.StrokeWidth >= 0 {
output = append(output, fmt.Sprintf("\"stroke_width\": %0.2f", s.StrokeWidth))
} else {
output = append(output, "\"stroke_width\": null")
}
if !s.StrokeColor.IsZero() {
output = append(output, fmt.Sprintf("\"stroke_color\": %s", s.StrokeColor.String()))
} else {
output = append(output, "\"stroke_color\": null")
}
if len(s.StrokeDashArray) > 0 {
var elements []string
for _, v := range s.StrokeDashArray {
elements = append(elements, fmt.Sprintf("%.2f", v))
}
dashArray := strings.Join(elements, ", ")
output = append(output, fmt.Sprintf("\"stroke_dash_array\": [%s]", dashArray))
} else {
output = append(output, "\"stroke_dash_array\": null")
}
if !s.FillColor.IsZero() {
output = append(output, fmt.Sprintf("\"fill_color\": %s", s.FillColor.String()))
} else {
output = append(output, "\"fill_color\": null")
}
if s.FontSize != 0 {
output = append(output, fmt.Sprintf("\"font_size\": \"%0.2fpt\"", s.FontSize))
} else {
output = append(output, "\"fill_color\": null")
}
if !s.FillColor.IsZero() {
output = append(output, fmt.Sprintf("\"font_color\": %s", s.FillColor.String()))
} else {
output = append(output, "\"font_color\": null")
}
if s.Font != nil {
output = append(output, fmt.Sprintf("\"font\": \"%s\"", s.Font.Name(truetype.NameIDFontFamily)))
} else {
output = append(output, "\"font_color\": null")
}
return "{" + strings.Join(output, ", ") + "}"
}
// GetStrokeColor returns the stroke color.
func (s Style) GetStrokeColor(defaults ...drawing.Color) drawing.Color {
if s.StrokeColor.IsZero() {
if len(defaults) > 0 {
return defaults[0]
}
return drawing.ColorTransparent
}
return s.StrokeColor
}
// GetFillColor returns the fill color.
func (s Style) GetFillColor(defaults ...drawing.Color) drawing.Color {
if s.FillColor.IsZero() {
if len(defaults) > 0 {
return defaults[0]
}
return drawing.ColorTransparent
}
return s.FillColor
}
// GetStrokeWidth returns the stroke width.
func (s Style) GetStrokeWidth(defaults ...float64) float64 {
if s.StrokeWidth == 0 {
if len(defaults) > 0 {
return defaults[0]
}
return DefaultStrokeWidth
}
return s.StrokeWidth
}
// GetStrokeDashArray returns the stroke dash array.
func (s Style) GetStrokeDashArray(defaults ...[]float64) []float64 {
if len(s.StrokeDashArray) == 0 {
if len(defaults) > 0 {
return defaults[0]
}
return nil
}
return s.StrokeDashArray
}
// GetFontSize gets the font size.
func (s Style) GetFontSize(defaults ...float64) float64 {
if s.FontSize == 0 {
if len(defaults) > 0 {
return defaults[0]
}
return DefaultFontSize
}
return s.FontSize
}
// GetFontColor gets the font size.
func (s Style) GetFontColor(defaults ...drawing.Color) drawing.Color {
if s.FontColor.IsZero() {
if len(defaults) > 0 {
return defaults[0]
}
return drawing.ColorTransparent
}
return s.FontColor
}
// GetFont returns the font face.
func (s Style) GetFont(defaults ...*truetype.Font) *truetype.Font {
if s.Font == nil {
if len(defaults) > 0 {
return defaults[0]
}
return nil
}
return s.Font
}
// GetPadding returns the padding.
func (s Style) GetPadding(defaults ...Box) Box {
if s.Padding.IsZero() {
if len(defaults) > 0 {
return defaults[0]
}
return Box{}
}
return s.Padding
}
// WithDefaultsFrom coalesces two styles into a new style.
func (s Style) WithDefaultsFrom(defaults Style) (final Style) {
final.StrokeColor = s.GetStrokeColor(defaults.StrokeColor)
final.StrokeWidth = s.GetStrokeWidth(defaults.StrokeWidth)
final.StrokeDashArray = s.GetStrokeDashArray(defaults.StrokeDashArray)
final.FillColor = s.GetFillColor(defaults.FillColor)
final.FontColor = s.GetFontColor(defaults.FontColor)
final.FontSize = s.GetFontSize(defaults.FontSize)
final.Font = s.GetFont(defaults.Font)
final.Padding = s.GetPadding(defaults.Padding)
return
}
// SVG returns the style as a svg style string.
func (s Style) SVG(dpi float64) string {
sw := s.StrokeWidth
sc := s.StrokeColor
fc := s.FillColor
fs := s.FontSize
fnc := s.FontColor
strokeWidthText := "stroke-width:0"
if sw != 0 {
strokeWidthText = "stroke-width:" + fmt.Sprintf("%d", int(sw))
}
strokeText := "stroke:none"
if !sc.IsZero() {
strokeText = "stroke:" + sc.String()
}
fillText := "fill:none"
if !fc.IsZero() {
fillText = "fill:" + fc.String()
}
fontSizeText := ""
if fs != 0 {
fontSizeText = "font-size:" + fmt.Sprintf("%.1fpx", drawing.PointsToPixels(dpi, fs))
}
if !fnc.IsZero() {
fillText = "fill:" + fnc.String()
}
fontText := s.SVGFontFace()
return strings.Join([]string{strokeWidthText, strokeText, fillText, fontSizeText, fontText}, ";")
}
// SVGStroke returns the stroke components.
func (s Style) SVGStroke() Style {
return Style{
StrokeDashArray: s.StrokeDashArray,
StrokeColor: s.StrokeColor,
StrokeWidth: s.StrokeWidth,
}
}
// SVGFill returns the fill components.
func (s Style) SVGFill() Style {
return Style{
FillColor: s.FillColor,
}
}
// SVGFillAndStroke returns the fill and stroke components.
func (s Style) SVGFillAndStroke() Style {
return Style{
StrokeDashArray: s.StrokeDashArray,
FillColor: s.FillColor,
StrokeColor: s.StrokeColor,
StrokeWidth: s.StrokeWidth,
}
}
// SVGText returns just the text components of the style.
func (s Style) SVGText() Style {
return Style{
FontColor: s.FontColor,
FontSize: s.FontSize,
}
}
// SVGFontFace returns the font face for the style.
func (s Style) SVGFontFace() string {
family := "sans-serif"
if s.GetFont() != nil {
name := s.GetFont().Name(truetype.NameIDFontFamily)
if len(name) != 0 {
family = fmt.Sprintf(`'%s',%s`, name, family)
}
}
return fmt.Sprintf("font-family:%s", family)
}
// SVGStrokeDashArray returns the stroke-dasharray property of a style.
func (s Style) SVGStrokeDashArray() string {
if len(s.StrokeDashArray) > 0 {
var values []string
for _, v := range s.StrokeDashArray {
values = append(values, fmt.Sprintf("%0.1f", v))
}
return "stroke-dasharray=\"" + strings.Join(values, ", ") + "\""
}
return ""
}