Debian Codegraph @

donut_chart.go

raw symbols SCIP
1 package chart
2
3 import (
4 "errors"
5 "fmt"
6 "io"
7
8 "github.com/golang/freetype/truetype"
9 )
10
11 // DonutChart is a chart that draws sections of a circle based on percentages with an hole.
12 type DonutChart struct {
13 Title string
14 TitleStyle Style
15
16 ColorPalette ColorPalette
17
18 Width int
19 Height int
20 DPI float64
21
22 Background Style
23 Canvas Style
24 SliceStyle Style
25
26 Font *truetype.Font
27 defaultFont *truetype.Font
28
29 Values []Value
30 Elements []Renderable
31 }
32
33 // GetDPI returns the dpi for the chart.
34 func (pc DonutChart) GetDPI(defaults ...float64) float64 {
35 if pc.DPI == 0 {
36 if len(defaults) > 0 {
37 return defaults[0]
38 }
39 return DefaultDPI
40 }
41 return pc.DPI
42 }
43
44 // GetFont returns the text font.
45 func (pc DonutChart) GetFont() *truetype.Font {
46 if pc.Font == nil {
47 return pc.defaultFont
48 }
49 return pc.Font
50 }
51
52 // GetWidth returns the chart width or the default value.
53 func (pc DonutChart) GetWidth() int {
54 if pc.Width == 0 {
55 return DefaultChartWidth
56 }
57 return pc.Width
58 }
59
60 // GetHeight returns the chart height or the default value.
61 func (pc DonutChart) GetHeight() int {
62 if pc.Height == 0 {
63 return DefaultChartWidth
64 }
65 return pc.Height
66 }
67
68 // Render renders the chart with the given renderer to the given io.Writer.
69 func (pc DonutChart) Render(rp RendererProvider, w io.Writer) error {
70 if len(pc.Values) == 0 {
71 return errors.New("please provide at least one value")
72 }
73
74 r, err := rp(pc.GetWidth(), pc.GetHeight())
75 if err != nil {
76 return err
77 }
78
79 if pc.Font == nil {
80 defaultFont, err := GetDefaultFont()
81 if err != nil {
82 return err
83 }
84 pc.defaultFont = defaultFont
85 }
86 r.SetDPI(pc.GetDPI(DefaultDPI))
87
88 canvasBox := pc.getDefaultCanvasBox()
89 canvasBox = pc.getCircleAdjustedCanvasBox(canvasBox)
90
91 pc.drawBackground(r)
92 pc.drawCanvas(r, canvasBox)
93
94 finalValues, err := pc.finalizeValues(pc.Values)
95 if err != nil {
96 return err
97 }
98 pc.drawSlices(r, canvasBox, finalValues)
99 pc.drawTitle(r)
100 for _, a := range pc.Elements {
101 a(r, canvasBox, pc.styleDefaultsElements())
102 }
103
104 return r.Save(w)
105 }
106
107 func (pc DonutChart) drawBackground(r Renderer) {
108 Draw.Box(r, Box{
109 Right: pc.GetWidth(),
110 Bottom: pc.GetHeight(),
111 }, pc.getBackgroundStyle())
112 }
113
114 func (pc DonutChart) drawCanvas(r Renderer, canvasBox Box) {
115 Draw.Box(r, canvasBox, pc.getCanvasStyle())
116 }
117
118 func (pc DonutChart) drawTitle(r Renderer) {
119 if len(pc.Title) > 0 && !pc.TitleStyle.Hidden {
120 Draw.TextWithin(r, pc.Title, pc.Box(), pc.styleDefaultsTitle())
121 }
122 }
123
124 func (pc DonutChart) drawSlices(r Renderer, canvasBox Box, values []Value) {
125 cx, cy := canvasBox.Center()
126 diameter := MinInt(canvasBox.Width(), canvasBox.Height())
127 radius := float64(diameter>>1) / 1.1
128 labelRadius := (radius * 2.83) / 3.0
129
130 // draw the donut slices
131 var rads, delta, delta2, total float64
132 var lx, ly int
133
134 if len(values) == 1 {
135 pc.styleDonutChartValue(0).WriteToRenderer(r)
136 r.MoveTo(cx, cy)
137 r.Circle(radius, cx, cy)
138 } else {
139 for index, v := range values {
140 v.Style.InheritFrom(pc.styleDonutChartValue(index)).WriteToRenderer(r)
141 r.MoveTo(cx, cy)
142 rads = PercentToRadians(total)
143 delta = PercentToRadians(v.Value)
144
145 r.ArcTo(cx, cy, (radius / 1.25), (radius / 1.25), rads, delta)
146
147 r.LineTo(cx, cy)
148 r.Close()
149 r.FillStroke()
150 total = total + v.Value
151 }
152 }
153
154 //making the donut hole
155 v := Value{Value: 100, Label: "center"}
156 styletemp := pc.SliceStyle.InheritFrom(Style{
157 StrokeColor: ColorWhite, StrokeWidth: 4.0, FillColor: ColorWhite, FontColor: ColorWhite, //Font: pc.GetFont(),//FontSize: pc.getScaledFontSize(),
158 })
159 v.Style.InheritFrom(styletemp).WriteToRenderer(r)
160 r.MoveTo(cx, cy)
161 r.ArcTo(cx, cy, (radius / 3.5), (radius / 3.5), DegreesToRadians(0), DegreesToRadians(359))
162 r.LineTo(cx, cy)
163 r.Close()
164 r.FillStroke()
165
166 // draw the labels
167 total = 0
168 for index, v := range values {
169 v.Style.InheritFrom(pc.styleDonutChartValue(index)).WriteToRenderer(r)
170 if len(v.Label) > 0 {
171 delta2 = PercentToRadians(total + (v.Value / 2.0))
172 delta2 = RadianAdd(delta2, _pi2)
173 lx, ly = CirclePoint(cx, cy, labelRadius, delta2)
174
175 tb := r.MeasureText(v.Label)
176 lx = lx - (tb.Width() >> 1)
177 ly = ly + (tb.Height() >> 1)
178
179 r.Text(v.Label, lx, ly)
180 }
181 total = total + v.Value
182 }
183 }
184
185 func (pc DonutChart) finalizeValues(values []Value) ([]Value, error) {
186 finalValues := Values(values).Normalize()
187 if len(finalValues) == 0 {
188 return nil, fmt.Errorf("donut chart must contain at least (1) non-zero value")
189 }
190 return finalValues, nil
191 }
192
193 func (pc DonutChart) getDefaultCanvasBox() Box {
194 return pc.Box()
195 }
196
197 func (pc DonutChart) getCircleAdjustedCanvasBox(canvasBox Box) Box {
198 circleDiameter := MinInt(canvasBox.Width(), canvasBox.Height())
199
200 square := Box{
201 Right: circleDiameter,
202 Bottom: circleDiameter,
203 }
204
205 return canvasBox.Fit(square)
206 }
207
208 func (pc DonutChart) getBackgroundStyle() Style {
209 return pc.Background.InheritFrom(pc.styleDefaultsBackground())
210 }
211
212 func (pc DonutChart) getCanvasStyle() Style {
213 return pc.Canvas.InheritFrom(pc.styleDefaultsCanvas())
214 }
215
216 func (pc DonutChart) styleDefaultsCanvas() Style {
217 return Style{
218 FillColor: pc.GetColorPalette().CanvasColor(),
219 StrokeColor: pc.GetColorPalette().CanvasStrokeColor(),
220 StrokeWidth: DefaultStrokeWidth,
221 }
222 }
223
224 func (pc DonutChart) styleDefaultsDonutChartValue() Style {
225 return Style{
226 StrokeColor: pc.GetColorPalette().TextColor(),
227 StrokeWidth: 4.0,
228 FillColor: pc.GetColorPalette().TextColor(),
229 }
230 }
231
232 func (pc DonutChart) styleDonutChartValue(index int) Style {
233 return pc.SliceStyle.InheritFrom(Style{
234 StrokeColor: ColorWhite,
235 StrokeWidth: 4.0,
236 FillColor: pc.GetColorPalette().GetSeriesColor(index),
237 FontSize: pc.getScaledFontSize(),
238 FontColor: pc.GetColorPalette().TextColor(),
239 Font: pc.GetFont(),
240 })
241 }
242
243 func (pc DonutChart) getScaledFontSize() float64 {
244 effectiveDimension := MinInt(pc.GetWidth(), pc.GetHeight())
245 if effectiveDimension >= 2048 {
246 return 48.0
247 } else if effectiveDimension >= 1024 {
248 return 24.0
249 } else if effectiveDimension > 512 {
250 return 18.0
251 } else if effectiveDimension > 256 {
252 return 12.0
253 }
254 return 10.0
255 }
256
257 func (pc DonutChart) styleDefaultsBackground() Style {
258 return Style{
259 FillColor: pc.GetColorPalette().BackgroundColor(),
260 StrokeColor: pc.GetColorPalette().BackgroundStrokeColor(),
261 StrokeWidth: DefaultStrokeWidth,
262 }
263 }
264
265 func (pc DonutChart) styleDefaultsElements() Style {
266 return Style{
267 Font: pc.GetFont(),
268 }
269 }
270
271 func (pc DonutChart) styleDefaultsTitle() Style {
272 return pc.TitleStyle.InheritFrom(Style{
273 FontColor: pc.GetColorPalette().TextColor(),
274 Font: pc.GetFont(),
275 FontSize: pc.getTitleFontSize(),
276 TextHorizontalAlign: TextHorizontalAlignCenter,
277 TextVerticalAlign: TextVerticalAlignTop,
278 TextWrap: TextWrapWord,
279 })
280 }
281
282 func (pc DonutChart) getTitleFontSize() float64 {
283 effectiveDimension := MinInt(pc.GetWidth(), pc.GetHeight())
284 if effectiveDimension >= 2048 {
285 return 48
286 } else if effectiveDimension >= 1024 {
287 return 24
288 } else if effectiveDimension >= 512 {
289 return 18
290 } else if effectiveDimension >= 256 {
291 return 12
292 }
293 return 10
294 }
295
296 // GetColorPalette returns the color palette for the chart.
297 func (pc DonutChart) GetColorPalette() ColorPalette {
298 if pc.ColorPalette != nil {
299 return pc.ColorPalette
300 }
301 return AlternateColorPalette
302 }
303
304 // Box returns the chart bounds as a box.
305 func (pc DonutChart) Box() Box {
306 dpr := pc.Background.Padding.GetRight(DefaultBackgroundPadding.Right)
307 dpb := pc.Background.Padding.GetBottom(DefaultBackgroundPadding.Bottom)
308
309 return Box{
310 Top: pc.Background.Padding.GetTop(DefaultBackgroundPadding.Top),
311 Left: pc.Background.Padding.GetLeft(DefaultBackgroundPadding.Left),
312 Right: pc.GetWidth() - dpr,
313 Bottom: pc.GetHeight() - dpb,
314 }
315 }