Conditions | 22 |
Paths | 9505 |
Total Lines | 167 |
Code Lines | 99 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
63 | public function MakePie( |
||
64 | $filename, |
||
65 | $pieWidth, |
||
66 | $pieHeight, |
||
67 | $ShadowDistance, |
||
68 | $pieBackgroundColor, |
||
69 | $EQpieData, |
||
70 | $legend |
||
71 | ) { |
||
72 | if (!\function_exists('imagecreatetruecolor')) { |
||
73 | exit('Error, GD Library 2 needed.'); |
||
|
|||
74 | } |
||
75 | |||
76 | //set some limitations |
||
77 | if ($pieWidth < 100 | $pieWidth > 500) { |
||
78 | $pieWidth = 100; |
||
79 | } |
||
80 | if ($pieHeight < 100 | $pieHeight > 500) { |
||
81 | $pieHeight = 100; |
||
82 | } |
||
83 | if ($ShadowDistance < 1 | $ShadowDistance > 50) { |
||
84 | $ShadowDistance = 10; |
||
85 | } |
||
86 | |||
87 | $pieWidth *= 3; |
||
88 | $pieHeight *= 3; |
||
89 | $ShadowDistance *= 3; |
||
90 | $pieBackgroundColor = $pieBackgroundColor; |
||
91 | |||
92 | $pie = @\imagecreatetruecolor($pieWidth, $pieHeight + $ShadowDistance); |
||
93 | |||
94 | $colR = \hexdec(mb_substr($pieBackgroundColor, 1, 2)); |
||
95 | $colG = \hexdec(mb_substr($pieBackgroundColor, 3, 2)); |
||
96 | $colB = \hexdec(mb_substr($pieBackgroundColor, 5, 2)); |
||
97 | $pieBG = \imagefilledarc($pie, $colR, $colG, $colB); |
||
98 | \imagefill($pie, 0, 0, $pieBG); |
||
99 | |||
100 | // get the total value for percentage calculations |
||
101 | $this->total = 0; |
||
102 | |||
103 | $maxStringLenght = 0; |
||
104 | foreach ($EQpieData as $i => $value) { |
||
105 | $this->total += $value[1]; |
||
106 | if (mb_strlen($value[0]) > $maxStringLenght) { |
||
107 | $maxStringLenght = mb_strlen($value[0]); |
||
108 | } |
||
109 | } |
||
110 | |||
111 | $pieParts = $i + 1; |
||
112 | \reset($EQpieData); |
||
113 | $legendWidth = (($legend > 0) ? \imagefontwidth(2) * ($maxStringLenght + 6) + 40 : 0); |
||
114 | |||
115 | // the first pie-part starts with offset in degrees up from horizantal right, looks better this way |
||
116 | $pieStart = 135; |
||
117 | |||
118 | foreach ($EQpieData as $i => $value) { |
||
119 | // the name for each part is $value[0] |
||
120 | // the value for each part is $value[1] |
||
121 | // the color for each part is $value[2] |
||
122 | |||
123 | $piePart = $value[1]; |
||
124 | if (isset($this->total) && $this->total > 0) { |
||
125 | $piePart100 = \round($piePart / $this->total * 100, 2); // value in percentage, the rounding and * 100 for extra accuracy for pie w/o gaps |
||
126 | } else { |
||
127 | $piePart100 = 0; |
||
128 | } |
||
129 | |||
130 | $piePart360 = $piePart100 * 3.6; // in degrees |
||
131 | |||
132 | $colR = \hexdec(mb_substr($value[2], 1, 2)); |
||
133 | $colG = \hexdec(mb_substr($value[2], 3, 2)); |
||
134 | $colB = \hexdec(mb_substr($value[2], 5, 2)); |
||
135 | $PartColor = \imagefilledarc($pie, $colR, $colG, $colB); |
||
136 | |||
137 | $ShadowColR = (($colR > 79) ? $colR - 80 : 0); |
||
138 | $ShadowColG = (($colG > 79) ? $colG - 80 : 0); |
||
139 | $ShadowColB = (($colB > 79) ? $colB - 80 : 0); |
||
140 | |||
141 | $ShadowColor = \imagefilledarc($pie, $ShadowColR, $ShadowColG, $ShadowColB); |
||
142 | |||
143 | //Here we create the shadow down-worths |
||
144 | for ($i = 0; $i < $ShadowDistance; ++$i) { |
||
145 | \imagefilledarc($pie, $pieWidth / 2, $pieHeight / 2 + $i, $pieWidth - 20, $pieHeight - 20, \round($pieStart), \round($pieStart + $piePart360), $ShadowColor, \IMG_ARC_NOFILL); |
||
146 | } |
||
147 | |||
148 | $pieStart += $piePart360; |
||
149 | } |
||
150 | \reset($EQpieData); |
||
151 | |||
152 | $pieStart = 135; |
||
153 | |||
154 | foreach ($EQpieData as $i => $value) { |
||
155 | $piePart = $value[1]; |
||
156 | if (isset($this->total) && $this->total > 0) { |
||
157 | $piePart100 = \round($piePart / $this->total * 100, 2); // value in percentage, the rounding and * 100 for extra accuracy for pie w/o gaps |
||
158 | } else { |
||
159 | $piePart100 = 0; |
||
160 | } |
||
161 | $piePart360 = $piePart100 * 3.6; // in degrees |
||
162 | |||
163 | $colR = \hexdec(mb_substr($value[2], 1, 2)); |
||
164 | $colG = \hexdec(mb_substr($value[2], 3, 2)); |
||
165 | $colB = \hexdec(mb_substr($value[2], 5, 2)); |
||
166 | $PartColor = \imagefilledarc($pie, $colR, $colG, $colB); |
||
167 | |||
168 | //Here we create the real pie chart |
||
169 | \imagefilledarc($pie, $pieWidth / 2, $pieHeight / 2, $pieWidth - 20, $pieHeight - 20, \round($pieStart), \round($pieStart + $piePart360), $PartColor, \IMG_ARC_PIE); |
||
170 | |||
171 | $pieStart += $piePart360; |
||
172 | } |
||
173 | \reset($EQpieData); |
||
174 | |||
175 | // create final pie picture with proper background color |
||
176 | $finalPie = \imagecreatetruecolor($pieWidth / 3 + $legendWidth, ($pieHeight + $ShadowDistance) / 3); |
||
177 | \imagefill($finalPie, 0, 0, $pieBG); |
||
178 | |||
179 | // resample with pieGraph inside (3x smaller) |
||
180 | \imagecopyresampled($finalPie, $pie, 0, 0, 0, 0, $pieWidth / 3, ($pieHeight + $ShadowDistance) / 3, $pieWidth, $pieHeight + $ShadowDistance); |
||
181 | |||
182 | // Create the ledgend ... |
||
183 | if ($legendWidth > 0) { |
||
184 | // Legend Box |
||
185 | $leg_width = $legendWidth - 10; |
||
186 | $leg_height = $pieParts * (\imagefontheight(2) + 2) + 2; |
||
187 | $legendImage = \imagecreatetruecolor($leg_width, $leg_height); |
||
188 | //ImageFill($legendImage, 0, 0, $pieBG); |
||
189 | |||
190 | $borderColor = \imagefilledarc($pie, '155', '155', '155'); |
||
191 | $boxColor = \imagefilledarc($pie, '255', '255', '255'); |
||
192 | $textColor = \imagefilledarc($pie, '55', '55', '55'); |
||
193 | |||
194 | \imagefilledrectangle($legendImage, 0, 0, $leg_width, $leg_height, $boxColor); |
||
195 | \imagerectangle($legendImage, 0, 0, $leg_width - 1, $leg_height - 1, $borderColor); |
||
196 | |||
197 | $box_width = \imagefontwidth(2) - 5; |
||
198 | $box_height = \imagefontheight(2) - 5; |
||
199 | $yOffset = 2; |
||
200 | |||
201 | foreach ($EQpieData as $i => $value) { |
||
202 | $piePart = $value[1]; |
||
203 | if (isset($this->total) && $this->total > 0) { |
||
204 | $piePart100 = \round($piePart / $this->total * 100, 2); // value in percentage, the rounding and * 100 for extra accuracy for pie w/o gaps |
||
205 | } else { |
||
206 | $piePart100 = 0; |
||
207 | } |
||
208 | $colR = \hexdec(mb_substr($value[2], 1, 2)); |
||
209 | $colG = \hexdec(mb_substr($value[2], 3, 2)); |
||
210 | $colB = \hexdec(mb_substr($value[2], 5, 2)); |
||
211 | $PartColor = \imagefilledarc($legendImage, $colR, $colG, $colB); |
||
212 | |||
213 | \imagefilledrectangle($legendImage, 5, $yOffset + 2, 5 + $box_width, $yOffset + $box_height + 2, $PartColor); |
||
214 | \imagerectangle($legendImage, 5, $yOffset + 2, 5 + $box_width, $yOffset + $box_height + 2, $borderColor); |
||
215 | |||
216 | $text = $value[0] . ' ' . $piePart100 . '%'; |
||
217 | \imagestring($legendImage, 2, '20', $yOffset, $text, $textColor); |
||
218 | $yOffset += 15; |
||
219 | } |
||
220 | |||
221 | \reset($EQpieData); // reset pointer in array to first |
||
222 | |||
223 | \imagecopyresampled($finalPie, $legendImage, $pieWidth / 3, 10, 0, 0, $leg_width, $leg_height, $leg_width, $leg_height); |
||
224 | \imagedestroy($legendImage); |
||
225 | } |
||
226 | \header('Content-type: image/png'); |
||
227 | \imagepng($finalPie, $filename); |
||
228 | \imagedestroy($pie); |
||
229 | \imagedestroy($finalPie); |
||
230 | } |
||
232 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.