-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpie_chart.go
175 lines (147 loc) · 3.96 KB
/
pie_chart.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
/*
Package gopie renders pie charts to SVG.
*/
package gopie
import (
"github.com/eugenezinoviev/gopie/assets"
"github.com/golang/freetype/truetype"
)
//go:generate go run mbed/mbed.go -d ./assets -o ./assets/assets.go -p assets
// Value represents chart value.
type Value struct {
Value float64 // Value.
Label string // Label of value.
}
// PieChart struct represents a pie chart rendering options.
type PieChart struct {
Width float64 // SVG Width. Default is 200.
Height float64 // SVG Height. Default is 200.
Values []Value // Chart values.
SliceColors []string // Colors to be used for pie slices and label lines.
StrokeColor string // Slice stroke color. Default is "#ffffff".
StrokeWidth float64 // Slice stroke width. Default is 0 px.
FontSize float64 // Label font size. Default is 12 px.
FontFamily string // Label font family. Default is "Roboto Medium".
LabelLine float64 // Label line length. Default is 10 px.
LabelLineWidth float64 // Label line width. Default is 2 px.
LabelPadding float64 // Label text padding. Default is 4 px.
DPI float64 // DPI. Default is 92.
font *truetype.Font // label font
fontBytes []byte // font bytes
EmbedFont bool // Whether embed font or not. Default is false.
InnerRadius float64 // Inner radius or donut chart. Default is 0 which means by default pie chart will be rendered.
}
func (c PieChart) getFontFamily() string {
if c.FontFamily == "" {
return defaultFontFamily
}
return c.FontFamily
}
func (c PieChart) getFontSize() float64 {
if c.FontSize == 0 {
return defaultFontSize
}
return c.FontSize
}
func (c PieChart) getLabelLineFullLength() float64 {
stroke := c.getStrokeWidth()
labelLine := c.getLabelLine()
return labelLine + stroke
}
func (c PieChart) getLabelLine() float64 {
if c.LabelLine == 0 {
return defaultLabelLine
}
return c.LabelLine
}
func (c PieChart) getLabelLineWidth() float64 {
if c.LabelLineWidth == 0 {
return defaultLabelLineWidth
}
return c.LabelLineWidth
}
func (c PieChart) getLabelPadding() float64 {
if c.LabelPadding == 0 {
return defaultLabelPadding
}
return c.LabelPadding
}
func (c PieChart) getDPI() float64 {
if c.DPI == 0 {
return defaultDPI
}
return c.DPI
}
func (c PieChart) getSliceColors() []string {
if len(c.SliceColors) == 0 {
return defaultColors
}
return c.SliceColors
}
func (c PieChart) getSliceColor(index int) string {
colors := c.getSliceColors()
return colors[index%len(colors)]
}
func (c PieChart) getStrokeWidth() float64 {
if c.StrokeWidth <= 0 {
return defaultStrokeWidth
}
return c.StrokeWidth
}
func (c PieChart) getStrokeColor() string {
if c.StrokeColor == "" {
return defaultStrokeColor
}
return c.StrokeColor
}
func (c PieChart) getWidth() float64 {
if c.Width == 0 {
return defaultWidth
}
return c.Width
}
func (c PieChart) getHeight() float64 {
if c.Height == 0 {
return defaultHeight
}
return c.Height
}
func (c PieChart) getCenter() (centerX, centerY float64) {
return c.getWidth() / 2, c.getHeight() / 2
}
func (c PieChart) calculateTotalValue() float64 {
total := float64(0)
for _, value := range c.Values {
total = total + value.Value
}
return total
}
func (c PieChart) getFont() (*truetype.Font, error) {
if c.font != nil {
return c.font, nil
}
return truetype.Parse(c.getFontBytes())
}
func (c PieChart) getFontBytes() []byte {
if len(c.fontBytes) != 0 {
return c.fontBytes
}
return assets.GetFileBytes("assets/Roboto-Medium.ttf")
}
// SetFont function sets font for the chart.
// The parameter should contain bytes of TTF font.
func (c *PieChart) SetFont(ttf []byte) (err error) {
font, err := truetype.Parse(ttf)
if err != nil {
return
}
c.fontBytes = ttf
c.font = font
return
}
func (c PieChart) getInnerRadius() float64 {
if c.InnerRadius > 0 {
return c.InnerRadius
}
return defaultInnerRadius
}