| Conditions | 47 |
| Paths | > 20000 |
| Total Lines | 306 |
| Code Lines | 208 |
| 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 |
||
| 156 | public function Stroke($img, $xscale, $yscale) |
||
| 157 | { |
||
| 158 | $pattern = null; |
||
| 159 | $img->SetLineWeight($this->weight); |
||
| 160 | $grad = null; |
||
| 161 | for ($i = 0; $i < $this->numpoints - 1; ++$i) { |
||
| 162 | $accy = 0; |
||
| 163 | $accy_neg = 0; |
||
| 164 | for ($j = 0; $j < $this->nbrplots; ++$j) { |
||
| 165 | $img->SetColor($this->plots[$j]->color); |
||
| 166 | |||
| 167 | if ($this->plots[$j]->coords[0][$i] >= 0) { |
||
| 168 | $yt = $yscale->Translate($this->plots[$j]->coords[0][$i] + $accy); |
||
| 169 | $accyt = $yscale->Translate($accy); |
||
| 170 | $accy += $this->plots[$j]->coords[0][$i]; |
||
| 171 | } else { |
||
| 172 | //if ( $this->plots[$j]->coords[0][$i] < 0 || $accy_neg < 0 ) { |
||
| 173 | $yt = $yscale->Translate($this->plots[$j]->coords[0][$i] + $accy_neg); |
||
| 174 | $accyt = $yscale->Translate($accy_neg); |
||
| 175 | $accy_neg += $this->plots[$j]->coords[0][$i]; |
||
| 176 | } |
||
| 177 | |||
| 178 | $xt = $xscale->Translate($i); |
||
| 179 | |||
| 180 | if ($this->abswidth > -1) { |
||
| 181 | $abswidth = $this->abswidth; |
||
| 182 | } else { |
||
| 183 | $abswidth = round($this->width * $xscale->scale_factor, 0); |
||
| 184 | } |
||
| 185 | |||
| 186 | $pts = [$xt, $accyt, $xt, $yt, $xt + $abswidth, $yt, $xt + $abswidth, $accyt]; |
||
| 187 | |||
| 188 | if ($this->bar_shadow) { |
||
| 189 | $ssh = $this->bar_shadow_hsize; |
||
| 190 | $ssv = $this->bar_shadow_vsize; |
||
| 191 | |||
| 192 | // We must also differ if we are a positive or negative bar. |
||
| 193 | if ($j === 0) { |
||
| 194 | // This gets extra complicated since we have to |
||
| 195 | // see all plots to see if we are negative. It could |
||
| 196 | // for example be that all plots are 0 until the very |
||
| 197 | // last one. We therefore need to save the initial setup |
||
| 198 | // for both the negative and positive case |
||
| 199 | |||
| 200 | // In case the final bar is positive |
||
| 201 | $sp[0] = $pts[6] + 1; |
||
| 202 | $sp[1] = $pts[7]; |
||
| 203 | $sp[2] = $pts[6] + $ssh; |
||
| 204 | $sp[3] = $pts[7] - $ssv; |
||
| 205 | |||
| 206 | // In case the final bar is negative |
||
| 207 | $nsp[0] = $pts[0]; |
||
| 208 | $nsp[1] = $pts[1]; |
||
| 209 | $nsp[2] = $pts[0] + $ssh; |
||
| 210 | $nsp[3] = $pts[1] - $ssv; |
||
| 211 | $nsp[4] = $pts[6] + $ssh; |
||
| 212 | $nsp[5] = $pts[7] - $ssv; |
||
| 213 | $nsp[10] = $pts[6] + 1; |
||
| 214 | $nsp[11] = $pts[7]; |
||
| 215 | } |
||
| 216 | |||
| 217 | if ($j === $this->nbrplots - 1) { |
||
| 218 | // If this is the last plot of the bar and |
||
| 219 | // the total value is larger than 0 then we |
||
| 220 | // add the shadow. |
||
| 221 | if (is_array($this->bar_shadow_color)) { |
||
| 222 | $numcolors = safe_count($this->bar_shadow_color); |
||
| 223 | if ($numcolors == 0) { |
||
| 224 | Util\JpGraphError::RaiseL(2013); //('You have specified an empty array for shadow colors in the bar plot.'); |
||
| 225 | } |
||
| 226 | $img->PushColor($this->bar_shadow_color[$i % $numcolors]); |
||
| 227 | } else { |
||
| 228 | $img->PushColor($this->bar_shadow_color); |
||
| 229 | } |
||
| 230 | |||
| 231 | if ($accy > 0) { |
||
| 232 | $sp[4] = $pts[4] + $ssh; |
||
| 233 | $sp[5] = $pts[5] - $ssv; |
||
| 234 | $sp[6] = $pts[2] + $ssh; |
||
| 235 | $sp[7] = $pts[3] - $ssv; |
||
| 236 | $sp[8] = $pts[2]; |
||
| 237 | $sp[9] = $pts[3] - 1; |
||
| 238 | $sp[10] = $pts[4] + 1; |
||
| 239 | $sp[11] = $pts[5]; |
||
| 240 | $img->FilledPolygon($sp, 4); |
||
| 241 | } elseif ($accy_neg < 0) { |
||
| 242 | $nsp[6] = $pts[4] + $ssh; |
||
| 243 | $nsp[7] = $pts[5] - $ssv; |
||
| 244 | $nsp[8] = $pts[4] + 1; |
||
| 245 | $nsp[9] = $pts[5]; |
||
| 246 | $img->FilledPolygon($nsp, 4); |
||
| 247 | } |
||
| 248 | $img->PopColor(); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | // If value is NULL or 0, then don't draw a bar at all |
||
| 253 | if ($this->plots[$j]->coords[0][$i] == 0) { |
||
| 254 | continue; |
||
| 255 | } |
||
| 256 | |||
| 257 | if ($this->plots[$j]->grad) { |
||
| 258 | if ($grad === null) { |
||
| 259 | $grad = new Gradient($img); |
||
| 260 | } |
||
| 261 | if (is_array($this->plots[$j]->grad_fromcolor)) { |
||
| 262 | // The first argument (grad_fromcolor) can be either an array or a single color. If it is an array |
||
| 263 | // then we have two choices. It can either a) be a single color specified as an Image\RGB triple or it can be |
||
| 264 | // an array to specify both (from, to style) for each individual bar. The way to know the difference is |
||
| 265 | // to investgate the first element. If this element is an integer [0,255] then we assume it is an Image\RGB |
||
| 266 | // triple. |
||
| 267 | $ng = safe_count($this->plots[$j]->grad_fromcolor); |
||
| 268 | if ($ng === 3) { |
||
| 269 | if (is_numeric($this->plots[$j]->grad_fromcolor[0]) && $this->plots[$j]->grad_fromcolor[0] > 0 && |
||
| 270 | $this->plots[$j]->grad_fromcolor[0] < 256) { |
||
| 271 | // Image\RGB Triple |
||
| 272 | $fromcolor = $this->plots[$j]->grad_fromcolor; |
||
| 273 | $tocolor = $this->plots[$j]->grad_tocolor; |
||
| 274 | $style = $this->plots[$j]->grad_style; |
||
| 275 | } else { |
||
| 276 | $fromcolor = $this->plots[$j]->grad_fromcolor[$i % $ng][0]; |
||
| 277 | $tocolor = $this->plots[$j]->grad_fromcolor[$i % $ng][1]; |
||
| 278 | $style = $this->plots[$j]->grad_fromcolor[$i % $ng][2]; |
||
| 279 | } |
||
| 280 | } else { |
||
| 281 | $fromcolor = $this->plots[$j]->grad_fromcolor[$i % $ng][0]; |
||
| 282 | $tocolor = $this->plots[$j]->grad_fromcolor[$i % $ng][1]; |
||
| 283 | $style = $this->plots[$j]->grad_fromcolor[$i % $ng][2]; |
||
| 284 | } |
||
| 285 | $grad->FilledRectangle( |
||
| 286 | $pts[2], |
||
| 287 | $pts[3], |
||
| 288 | $pts[6], |
||
| 289 | $pts[7], |
||
| 290 | $fromcolor, |
||
| 291 | $tocolor, |
||
| 292 | $style |
||
| 293 | ); |
||
| 294 | } else { |
||
| 295 | $grad->FilledRectangle( |
||
| 296 | $pts[2], |
||
| 297 | $pts[3], |
||
| 298 | $pts[6], |
||
| 299 | $pts[7], |
||
| 300 | $this->plots[$j]->grad_fromcolor, |
||
| 301 | $this->plots[$j]->grad_tocolor, |
||
| 302 | $this->plots[$j]->grad_style |
||
| 303 | ); |
||
| 304 | } |
||
| 305 | } else { |
||
| 306 | if (is_array($this->plots[$j]->fill_color)) { |
||
| 307 | $numcolors = safe_count($this->plots[$j]->fill_color); |
||
| 308 | $fillcolor = $this->plots[$j]->fill_color[$i % $numcolors]; |
||
| 309 | // If the bar is specified to be non filled then the fill color is false |
||
| 310 | if ($fillcolor !== false) { |
||
| 311 | $img->SetColor($this->plots[$j]->fill_color[$i % $numcolors]); |
||
| 312 | } |
||
| 313 | } else { |
||
| 314 | $fillcolor = $this->plots[$j]->fill_color; |
||
| 315 | if ($fillcolor !== false) { |
||
| 316 | $img->SetColor($this->plots[$j]->fill_color); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | if ($fillcolor !== false) { |
||
| 320 | $img->FilledPolygon($pts); |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | $img->SetColor($this->plots[$j]->color); |
||
| 325 | |||
| 326 | // Stroke the pattern |
||
| 327 | if ($this->plots[$j]->iPattern > -1) { |
||
| 328 | if ($pattern === null) { |
||
| 329 | $pattern = new Graph\RectPatternFactory(); |
||
| 330 | } |
||
| 331 | |||
| 332 | $prect = $pattern->Create($this->plots[$j]->iPattern, $this->plots[$j]->iPatternColor, 1); |
||
| 333 | $prect->SetDensity($this->plots[$j]->iPatternDensity); |
||
| 334 | if ($this->plots[$j]->coords[0][$i] < 0) { |
||
| 335 | $rx = $pts[0]; |
||
| 336 | $ry = $pts[1]; |
||
| 337 | } else { |
||
| 338 | $rx = $pts[2]; |
||
| 339 | $ry = $pts[3]; |
||
| 340 | } |
||
| 341 | $width = abs($pts[4] - $pts[0]) + 1; |
||
| 342 | $height = abs($pts[1] - $pts[3]) + 1; |
||
| 343 | $prect->SetPos(new Util\Rectangle($rx, $ry, $width, $height)); |
||
| 344 | $prect->Stroke($img); |
||
| 345 | } |
||
| 346 | |||
| 347 | // CSIM array |
||
| 348 | |||
| 349 | if ($i < safe_count($this->plots[$j]->csimtargets)) { |
||
| 350 | // Create the client side image map |
||
| 351 | $rpts = $img->ArrRotate($pts); |
||
| 352 | $csimcoord = round($rpts[0]) . ', ' . round($rpts[1]); |
||
| 353 | for ($k = 1; $k < 4; ++$k) { |
||
| 354 | $csimcoord .= ', ' . round($rpts[2 * $k]) . ', ' . round($rpts[2 * $k + 1]); |
||
| 355 | } |
||
| 356 | if (!empty($this->plots[$j]->csimtargets[$i])) { |
||
| 357 | $this->csimareas .= '<area shape="poly" coords="' . $csimcoord . '" '; |
||
| 358 | $this->csimareas .= ' href="' . $this->plots[$j]->csimtargets[$i] . '" '; |
||
| 359 | |||
| 360 | if (!empty($this->plots[$j]->csimwintargets[$i])) { |
||
| 361 | $this->csimareas .= ' target="' . $this->plots[$j]->csimwintargets[$i] . '" '; |
||
| 362 | } |
||
| 363 | |||
| 364 | $sval = ''; |
||
| 365 | if (!empty($this->plots[$j]->csimalts[$i])) { |
||
| 366 | $sval = sprintf($this->plots[$j]->csimalts[$i], $this->plots[$j]->coords[0][$i]); |
||
| 367 | $this->csimareas .= " title=\"${sval}\" "; |
||
| 368 | } |
||
| 369 | $this->csimareas .= " alt=\"${sval}\" />\n"; |
||
| 370 | } |
||
| 371 | } |
||
| 372 | |||
| 373 | $pts[] = $pts[0]; |
||
| 374 | $pts[] = $pts[1]; |
||
| 375 | $img->SetLineWeight($this->plots[$j]->weight); |
||
| 376 | $img->Polygon($pts); |
||
| 377 | $img->SetLineWeight(1); |
||
| 378 | } |
||
| 379 | |||
| 380 | // Daw potential bar around the entire accbar bar |
||
| 381 | if ($this->weight > 0) { |
||
| 382 | $y = $yscale->Translate(0); |
||
| 383 | $img->SetColor($this->color); |
||
| 384 | $img->SetLineWeight($this->weight); |
||
| 385 | $img->Rectangle($pts[0], $y, $pts[6], $pts[5]); |
||
| 386 | } |
||
| 387 | |||
| 388 | // Draw labels for each acc.bar |
||
| 389 | |||
| 390 | $x = $pts[2] + ($pts[4] - $pts[2]) / 2; |
||
| 391 | if ($this->bar_shadow) { |
||
| 392 | $x += $ssh; |
||
| 393 | } |
||
| 394 | |||
| 395 | // First stroke the accumulated value for the entire bar |
||
| 396 | // This value is always placed at the top/bottom of the bars |
||
| 397 | if ($accy_neg < 0) { |
||
| 398 | $y = $yscale->Translate($accy_neg); |
||
| 399 | $this->value->Stroke($img, $accy_neg, $x, $y); |
||
| 400 | } else { |
||
| 401 | $y = $yscale->Translate($accy); |
||
| 402 | $this->value->Stroke($img, $accy, $x, $y); |
||
| 403 | } |
||
| 404 | |||
| 405 | $accy = 0; |
||
| 406 | $accy_neg = 0; |
||
| 407 | for ($j = 0; $j < $this->nbrplots; ++$j) { |
||
| 408 | // We don't print 0 values in an accumulated bar plot |
||
| 409 | if ($this->plots[$j]->coords[0][$i] == 0) { |
||
| 410 | continue; |
||
| 411 | } |
||
| 412 | |||
| 413 | if ($this->plots[$j]->coords[0][$i] > 0) { |
||
| 414 | $yt = $yscale->Translate($this->plots[$j]->coords[0][$i] + $accy); |
||
| 415 | $accyt = $yscale->Translate($accy); |
||
| 416 | if ($this->plots[$j]->valuepos == 'center') { |
||
| 417 | $y = $accyt - ($accyt - $yt) / 2; |
||
| 418 | } elseif ($this->plots[$j]->valuepos == 'bottom') { |
||
| 419 | $y = $accyt; |
||
| 420 | } else { |
||
| 421 | // top or max |
||
| 422 | $y = $accyt - ($accyt - $yt); |
||
| 423 | } |
||
| 424 | $accy += $this->plots[$j]->coords[0][$i]; |
||
| 425 | if ($this->plots[$j]->valuepos == 'center') { |
||
| 426 | $this->plots[$j]->value->SetAlign('center', 'center'); |
||
| 427 | $this->plots[$j]->value->SetMargin(0); |
||
| 428 | } elseif ($this->plots[$j]->valuepos == 'bottom') { |
||
| 429 | $this->plots[$j]->value->SetAlign('center', 'bottom'); |
||
| 430 | $this->plots[$j]->value->SetMargin(2); |
||
| 431 | } else { |
||
| 432 | $this->plots[$j]->value->SetAlign('center', 'top'); |
||
| 433 | $this->plots[$j]->value->SetMargin(1); |
||
| 434 | } |
||
| 435 | } else { |
||
| 436 | $yt = $yscale->Translate($this->plots[$j]->coords[0][$i] + $accy_neg); |
||
| 437 | $accyt = $yscale->Translate($accy_neg); |
||
| 438 | $accy_neg += $this->plots[$j]->coords[0][$i]; |
||
| 439 | if ($this->plots[$j]->valuepos == 'center') { |
||
| 440 | $y = $accyt - ($accyt - $yt) / 2; |
||
| 441 | } elseif ($this->plots[$j]->valuepos == 'bottom') { |
||
| 442 | $y = $accyt; |
||
| 443 | } else { |
||
| 444 | $y = $accyt - ($accyt - $yt); |
||
| 445 | } |
||
| 446 | if ($this->plots[$j]->valuepos == 'center') { |
||
| 447 | $this->plots[$j]->value->SetAlign('center', 'center'); |
||
| 448 | $this->plots[$j]->value->SetMargin(0); |
||
| 449 | } elseif ($this->plots[$j]->valuepos == 'bottom') { |
||
| 450 | $this->plots[$j]->value->SetAlign('center', $j == 0 ? 'bottom' : 'top'); |
||
| 451 | $this->plots[$j]->value->SetMargin(-2); |
||
| 452 | } else { |
||
| 453 | $this->plots[$j]->value->SetAlign('center', 'bottom'); |
||
| 454 | $this->plots[$j]->value->SetMargin(-1); |
||
| 455 | } |
||
| 456 | } |
||
| 457 | $this->plots[$j]->value->Stroke($img, $this->plots[$j]->coords[0][$i], $x, $y); |
||
| 458 | } |
||
| 459 | } |
||
| 460 | |||
| 461 | return true; |
||
| 462 | } |
||
| 464 |