11
11
12
12
use Fundevogel \Helpers \Butler ;
13
13
14
- use SVG \SVG ;
15
- use SVG \Nodes \Shapes \SVGCircle ;
16
14
17
15
/**
18
16
* Class Donut
19
17
*
20
- * Creates a donut chart
18
+ * Creates a { donut,pie} chart
21
19
*
22
20
* @package tiny-phpeanuts
23
21
*/
24
22
class Donut
25
23
{
26
24
/**
27
- * Current version number of tiny-phpeanuts
25
+ * Current version
28
26
*/
29
- const VERSION = '1.0.1 ' ;
27
+ const VERSION = '1.0.2 ' ;
30
28
31
29
32
30
/**
33
31
* Data points being visualized
34
- * Each entry consists of
35
- * - a color string
36
- * - a value representing the share (between 0 and 1)
32
+ * Each entry (= array) consists of two key-value pairs
33
+ * - ` color` => string
34
+ * - ` value` => float (between 0 and 1)
37
35
*
38
36
* @var array
39
37
*/
@@ -104,6 +102,14 @@ class Donut
104
102
private $ isPieChart = false ;
105
103
106
104
105
+ /**
106
+ * Constructor
107
+ *
108
+ * @param array $entries Array with data points to be rendered
109
+ * @param float $thickness Chart thickness (ignored when enabling `pieChart`)
110
+ * @param float $spacing Spacing between segments (= donut elements)
111
+ * @return void
112
+ */
107
113
public function __construct (
108
114
array $ entries ,
109
115
float $ thickness = 3 ,
@@ -128,7 +134,7 @@ public function setSize(int $size)
128
134
$ this ->size = $ size ;
129
135
}
130
136
131
- public function getSize ()
137
+ public function getSize (): int
132
138
{
133
139
return $ this ->size ;
134
140
}
@@ -138,7 +144,7 @@ public function setPreferViewbox(bool $preferViewbox)
138
144
$ this ->preferViewbox = $ preferViewbox ;
139
145
}
140
146
141
- public function getPreferViewbox ()
147
+ public function getPreferViewbox (): bool
142
148
{
143
149
return $ this ->preferViewbox ;
144
150
}
@@ -148,7 +154,7 @@ public function setBackgroundColor(string $backgroundColor)
148
154
$ this ->backgroundColor = $ backgroundColor ;
149
155
}
150
156
151
- public function getBackgroundColor ()
157
+ public function getBackgroundColor (): string
152
158
{
153
159
return $ this ->backgroundColor ;
154
160
}
@@ -158,7 +164,7 @@ public function setClasses(string $classes)
158
164
$ this ->classes = $ classes ;
159
165
}
160
166
161
- public function getClasses ()
167
+ public function getClasses (): string
162
168
{
163
169
return $ this ->classes ;
164
170
}
@@ -168,7 +174,7 @@ public function setRole(string $role)
168
174
$ this ->role = $ role ;
169
175
}
170
176
171
- public function getRole ()
177
+ public function getRole (): string
172
178
{
173
179
return $ this ->role ;
174
180
}
@@ -178,19 +184,24 @@ public function setPieChart(bool $isPieChart)
178
184
$ this ->isPieChart = $ isPieChart ;
179
185
}
180
186
181
- public function getPieChart ()
187
+ public function getPieChart (): bool
182
188
{
183
189
return $ this ->isPieChart ;
184
190
}
185
191
186
192
187
193
/**
188
- * Functionality
194
+ * Methods
189
195
*/
190
196
197
+ /**
198
+ * Renders SVG chart
199
+ *
200
+ * @return string SVG chart as string
201
+ */
191
202
public function render (): string
192
203
{
193
- $ svg = new SVG ($ this ->size , $ this ->size );
204
+ $ svg = new \ SVG \ SVG ($ this ->size , $ this ->size );
194
205
$ doc = $ svg ->getDocument ();
195
206
196
207
# If enabled, remove replace width & height with viewBox
@@ -227,6 +238,12 @@ public function render(): string
227
238
return $ svg ->toXMLString (false );
228
239
}
229
240
241
+
242
+ /**
243
+ * Initiates construction of segments
244
+ *
245
+ * @return array
246
+ */
230
247
private function constructSegments (): array
231
248
{
232
249
$ thickness = $ this ->isPieChart ? $ this ->size / 2 : $ this ->thickness ;
@@ -251,6 +268,15 @@ private function constructSegments(): array
251
268
return $ segments ;
252
269
}
253
270
271
+
272
+ /**
273
+ * Corrects segments according to spacing
274
+ *
275
+ * @param array $segments Build instructions for segments
276
+ * @param float $spacing Spacing between segments
277
+ *
278
+ * @return array Constructed segments
279
+ */
254
280
private function correctSegmentsForSpacing (array $ segments , float $ spacing ): array
255
281
{
256
282
$ totalLengthWithoutSpacing = 1 - $ spacing * count ($ segments );
@@ -267,6 +293,17 @@ private function correctSegmentsForSpacing(array $segments, float $spacing): arr
267
293
return $ results ;
268
294
}
269
295
296
+
297
+ /**
298
+ * Constructs single segment
299
+ *
300
+ * @param string $color Segment color
301
+ * @param float $length Segment value
302
+ * @param float $thickness Chart thickness
303
+ * @param float $start Segment starting position
304
+ *
305
+ * @return \SVG\Nodes\Shapes\SVGCircle
306
+ */
270
307
private function constructSegment (
271
308
string $ color ,
272
309
float $ length ,
@@ -279,7 +316,7 @@ private function constructSegment(
279
316
$ offset = $ circumference - ($ base * ($ start * 100 )) + ($ circumference / 4 );
280
317
$ lengthOnCircle = $ base * ($ length * 100 );
281
318
282
- $ circle = (new SVGCircle (
319
+ $ circle = (new \ SVG \ Nodes \ Shapes \ SVGCircle (
283
320
$ this ->size / 2 ,
284
321
$ this ->size / 2 ,
285
322
$ radius
0 commit comments