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