- Timestamp:
- Apr 13, 2019, 8:05:15 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/client/modules/Elezioni/grafici/jpgraph_canvtools.php
r2 r265 1 1 <?php 2 2 /*======================================================================= 3 // File:JPGRAPH_CANVTOOLS.PHP4 // Description:Some utilities for text and shape drawing on a canvas5 // Created:2002-08-236 // Ver: $Id: jpgraph_canvtools.php 1091 2009-01-18 22:57:40Z ljp $7 //8 // Copyright (c) Aditus Consulting. All rights reserved.9 //========================================================================10 */3 // File: JPGRAPH_CANVTOOLS.PHP 4 // Description: Some utilities for text and shape drawing on a canvas 5 // Created: 2002-08-23 6 // Ver: $Id: jpgraph_canvtools.php 1857 2009-09-28 14:38:14Z ljp $ 7 // 8 // Copyright (c) Asial Corporation. All rights reserved. 9 //======================================================================== 10 */ 11 11 12 12 define('CORNER_TOPLEFT',0); … … 21 21 // can abstract away with absolute pixels 22 22 //=================================================== 23 23 24 24 class CanvasScale { 25 25 private $g; … … 27 27 private $ixmin=0,$ixmax=10,$iymin=0,$iymax=10; 28 28 29 function CanvasScale($graph,$xmin=0,$xmax=10,$ymin=0,$ymax=10) {30 31 32 33 34 35 36 37 } 38 29 function __construct($graph,$xmin=0,$xmax=10,$ymin=0,$ymax=10) { 30 $this->g = $graph; 31 $this->w = $graph->img->width; 32 $this->h = $graph->img->height; 33 $this->ixmin = $xmin; 34 $this->ixmax = $xmax; 35 $this->iymin = $ymin; 36 $this->iymax = $ymax; 37 } 38 39 39 function Set($xmin=0,$xmax=10,$ymin=0,$ymax=10) { 40 $this->ixmin = $xmin; 41 $this->ixmax = $xmax; 42 $this->iymin = $ymin; 43 $this->iymax = $ymax; 40 $this->ixmin = $xmin; 41 $this->ixmax = $xmax; 42 $this->iymin = $ymin; 43 $this->iymax = $ymax; 44 } 45 46 function Get() { 47 return array($this->ixmin,$this->ixmax,$this->iymin,$this->iymax); 44 48 } 45 49 46 50 function Translate($x,$y) { 47 48 49 51 $xp = round(($x-$this->ixmin)/($this->ixmax - $this->ixmin) * $this->w); 52 $yp = round(($y-$this->iymin)/($this->iymax - $this->iymin) * $this->h); 53 return array($xp,$yp); 50 54 } 51 55 52 56 function TranslateX($x) { 53 54 57 $xp = round(($x-$this->ixmin)/($this->ixmax - $this->ixmin) * $this->w); 58 return $xp; 55 59 } 56 60 57 61 function TranslateY($y) { 58 59 62 $yp = round(($y-$this->iymin)/($this->iymax - $this->iymin) * $this->h); 63 return $yp; 60 64 } 61 65 … … 70 74 private $img,$scale; 71 75 72 function Shape($aGraph,$scale) {73 74 75 76 function __construct($aGraph,$scale) { 77 $this->img = $aGraph->img; 78 $this->img->SetColor('black'); 79 $this->scale = $scale; 76 80 } 77 81 78 82 function SetColor($aColor) { 79 83 $this->img->SetColor($aColor); 80 84 } 81 85 82 86 function Line($x1,$y1,$x2,$y2) { 83 list($x1,$y1) = $this->scale->Translate($x1,$y1); 84 list($x2,$y2) = $this->scale->Translate($x2,$y2); 85 $this->img->Line($x1,$y1,$x2,$y2); 87 list($x1,$y1) = $this->scale->Translate($x1,$y1); 88 list($x2,$y2) = $this->scale->Translate($x2,$y2); 89 $this->img->Line($x1,$y1,$x2,$y2); 90 } 91 92 function SetLineWeight($aWeight) { 93 $this->img->SetLineWeight($aWeight); 86 94 } 87 95 88 96 function Polygon($p,$aClosed=false) { 89 90 91 92 93 94 97 $n=count($p); 98 for($i=0; $i < $n; $i+=2 ) { 99 $p[$i] = $this->scale->TranslateX($p[$i]); 100 $p[$i+1] = $this->scale->TranslateY($p[$i+1]); 101 } 102 $this->img->Polygon($p,$aClosed); 95 103 } 96 104 97 105 function FilledPolygon($p) { 98 99 100 101 102 103 104 } 105 106 $n=count($p); 107 for($i=0; $i < $n; $i+=2 ) { 108 $p[$i] = $this->scale->TranslateX($p[$i]); 109 $p[$i+1] = $this->scale->TranslateY($p[$i+1]); 110 } 111 $this->img->FilledPolygon($p); 112 } 113 106 114 107 115 // Draw a bezier curve with defining points in the $aPnts array … … 112 120 // 6=x3, 7=y3 113 121 function Bezier($p,$aSteps=40) { 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 122 $x0 = $p[0]; 123 $y0 = $p[1]; 124 // Calculate coefficients 125 $cx = 3*($p[2]-$p[0]); 126 $bx = 3*($p[4]-$p[2])-$cx; 127 $ax = $p[6]-$p[0]-$cx-$bx; 128 $cy = 3*($p[3]-$p[1]); 129 $by = 3*($p[5]-$p[3])-$cy; 130 $ay = $p[7]-$p[1]-$cy-$by; 131 132 // Step size 133 $delta = 1.0/$aSteps; 134 135 $x_old = $x0; 136 $y_old = $y0; 137 for($t=$delta; $t<=1.0; $t+=$delta) { 138 $tt = $t*$t; $ttt=$tt*$t; 139 $x = $ax*$ttt + $bx*$tt + $cx*$t + $x0; 140 $y = $ay*$ttt + $by*$tt + $cy*$t + $y0; 141 $this->Line($x_old,$y_old,$x,$y); 142 $x_old = $x; 143 $y_old = $y; 144 } 145 $this->Line($x_old,$y_old,$p[6],$p[7]); 138 146 } 139 147 140 148 function Rectangle($x1,$y1,$x2,$y2) { 141 142 143 149 list($x1,$y1) = $this->scale->Translate($x1,$y1); 150 list($x2,$y2) = $this->scale->Translate($x2,$y2); 151 $this->img->Rectangle($x1,$y1,$x2,$y2); 144 152 } 145 153 146 154 function FilledRectangle($x1,$y1,$x2,$y2) { 147 148 149 150 } 151 155 list($x1,$y1) = $this->scale->Translate($x1,$y1); 156 list($x2,$y2) = $this->scale->Translate($x2,$y2); 157 $this->img->FilledRectangle($x1,$y1,$x2,$y2); 158 } 159 152 160 function Circle($x1,$y1,$r) { 153 154 155 156 157 158 161 list($x1,$y1) = $this->scale->Translate($x1,$y1); 162 if( $r >= 0 ) 163 $r = $this->scale->TranslateX($r); 164 else 165 $r = -$r; 166 $this->img->Circle($x1,$y1,$r); 159 167 } 160 168 161 169 function FilledCircle($x1,$y1,$r) { 162 163 164 165 166 167 168 } 169 170 function RoundedRectangle($x1,$y1,$x2,$y2,$r=null) { 171 172 173 174 175 176 177 178 179 180 181 } 182 183 function FilledRoundedRectangle($x1,$y1,$x2,$y2,$r=null) { 184 185 186 187 188 189 190 191 192 193 $this->img->FilledRoundedRectangle($x1,$y1,$x2,$y2,$r); 170 list($x1,$y1) = $this->scale->Translate($x1,$y1); 171 if( $r >= 0 ) 172 $r = $this->scale->TranslateX($r); 173 else 174 $r = -$r; 175 $this->img->FilledCircle($x1,$y1,$r); 176 } 177 178 function RoundedRectangle($x1,$y1,$x2,$y2,$r=null) { 179 list($x1,$y1) = $this->scale->Translate($x1,$y1); 180 list($x2,$y2) = $this->scale->Translate($x2,$y2); 181 182 if( $r == null ) 183 $r = 5; 184 elseif( $r >= 0 ) 185 $r = $this->scale->TranslateX($r); 186 else 187 $r = -$r; 188 $this->img->RoundedRectangle($x1,$y1,$x2,$y2,$r); 189 } 190 191 function FilledRoundedRectangle($x1,$y1,$x2,$y2,$r=null) { 192 list($x1,$y1) = $this->scale->Translate($x1,$y1); 193 list($x2,$y2) = $this->scale->Translate($x2,$y2); 194 195 if( $r == null ) 196 $r = 5; 197 elseif( $r > 0 ) 198 $r = $this->scale->TranslateX($r); 199 else 200 $r = -$r; 201 $this->img->FilledRoundedRectangle($x1,$y1,$x2,$y2,$r); 194 202 } 195 203 196 204 function ShadowRectangle($x1,$y1,$x2,$y2,$fcolor=false,$shadow_width=null,$shadow_color=array(102,102,102)) { 197 198 199 if( $shadow_width == null ) 200 201 202 203 205 list($x1,$y1) = $this->scale->Translate($x1,$y1); 206 list($x2,$y2) = $this->scale->Translate($x2,$y2); 207 if( $shadow_width == null ) 208 $shadow_width=4; 209 else 210 $shadow_width=$this->scale->TranslateX($shadow_width); 211 $this->img->ShadowRectangle($x1,$y1,$x2,$y2,$fcolor,$shadow_width,$shadow_color); 204 212 } 205 213 206 214 function SetTextAlign($halign,$valign="bottom") { 207 215 $this->img->SetTextAlign($halign,$valign="bottom"); 208 216 } 209 217 210 218 function StrokeText($x1,$y1,$txt,$dir=0,$paragraph_align="left") { 211 212 219 list($x1,$y1) = $this->scale->Translate($x1,$y1); 220 $this->img->StrokeText($x1,$y1,$txt,$dir,$paragraph_align); 213 221 } 214 222 … … 217 225 // 0=Top left, 1=top right, 2=bottom right, 3=bottom left 218 226 function IndentedRectangle($xt,$yt,$w,$h,$iw=0,$ih=0,$aCorner=3,$aFillColor="",$r=4) { 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 227 228 list($xt,$yt) = $this->scale->Translate($xt,$yt); 229 list($w,$h) = $this->scale->Translate($w,$h); 230 list($iw,$ih) = $this->scale->Translate($iw,$ih); 231 232 $xr = $xt + $w - 0; 233 $yl = $yt + $h - 0; 234 235 switch( $aCorner ) { 236 case 0: // Upper left 237 238 // Bottom line, left & right arc 239 $this->img->Line($xt+$r,$yl,$xr-$r,$yl); 240 $this->img->Arc($xt+$r,$yl-$r,$r*2,$r*2,90,180); 241 $this->img->Arc($xr-$r,$yl-$r,$r*2,$r*2,0,90); 242 243 // Right line, Top right arc 244 $this->img->Line($xr,$yt+$r,$xr,$yl-$r); 245 $this->img->Arc($xr-$r,$yt+$r,$r*2,$r*2,270,360); 246 247 // Top line, Top left arc 248 $this->img->Line($xt+$iw+$r,$yt,$xr-$r,$yt); 249 $this->img->Arc($xt+$iw+$r,$yt+$r,$r*2,$r*2,180,270); 250 251 // Left line 252 $this->img->Line($xt,$yt+$ih+$r,$xt,$yl-$r); 253 254 // Indent horizontal, Lower left arc 255 $this->img->Line($xt+$r,$yt+$ih,$xt+$iw-$r,$yt+$ih); 256 $this->img->Arc($xt+$r,$yt+$ih+$r,$r*2,$r*2,180,270); 257 258 // Indent vertical, Indent arc 259 $this->img->Line($xt+$iw,$yt+$r,$xt+$iw,$yt+$ih-$r); 260 $this->img->Arc($xt+$iw-$r,$yt+$ih-$r,$r*2,$r*2,0,90); 261 262 if( $aFillColor != '' ) { 263 $bc = $this->img->current_color_name; 264 $this->img->PushColor($aFillColor); 265 $this->img->FillToBorder($xr-$r,$yl-$r,$bc); 266 $this->img->PopColor(); 267 } 268 269 break; 270 271 case 1: // Upper right 272 273 // Bottom line, left & right arc 274 $this->img->Line($xt+$r,$yl,$xr-$r,$yl); 275 $this->img->Arc($xt+$r,$yl-$r,$r*2,$r*2,90,180); 276 $this->img->Arc($xr-$r,$yl-$r,$r*2,$r*2,0,90); 277 278 // Left line, Top left arc 279 $this->img->Line($xt,$yt+$r,$xt,$yl-$r); 280 $this->img->Arc($xt+$r,$yt+$r,$r*2,$r*2,180,270); 281 282 // Top line, Top right arc 283 $this->img->Line($xt+$r,$yt,$xr-$iw-$r,$yt); 284 $this->img->Arc($xr-$iw-$r,$yt+$r,$r*2,$r*2,270,360); 285 286 // Right line 287 $this->img->Line($xr,$yt+$ih+$r,$xr,$yl-$r); 288 289 // Indent horizontal, Lower right arc 290 $this->img->Line($xr-$iw+$r,$yt+$ih,$xr-$r,$yt+$ih); 291 $this->img->Arc($xr-$r,$yt+$ih+$r,$r*2,$r*2,270,360); 292 293 // Indent vertical, Indent arc 294 $this->img->Line($xr-$iw,$yt+$r,$xr-$iw,$yt+$ih-$r); 295 $this->img->Arc($xr-$iw+$r,$yt+$ih-$r,$r*2,$r*2,90,180); 296 297 if( $aFillColor != '' ) { 298 $bc = $this->img->current_color_name; 299 $this->img->PushColor($aFillColor); 300 $this->img->FillToBorder($xt+$r,$yl-$r,$bc); 301 $this->img->PopColor(); 302 } 303 304 break; 305 306 case 2: // Lower right 307 // Top line, Top left & Top right arc 308 $this->img->Line($xt+$r,$yt,$xr-$r,$yt); 309 $this->img->Arc($xt+$r,$yt+$r,$r*2,$r*2,180,270); 310 $this->img->Arc($xr-$r,$yt+$r,$r*2,$r*2,270,360); 311 312 // Left line, Bottom left arc 313 $this->img->Line($xt,$yt+$r,$xt,$yl-$r); 314 $this->img->Arc($xt+$r,$yl-$r,$r*2,$r*2,90,180); 315 316 // Bottom line, Bottom right arc 317 $this->img->Line($xt+$r,$yl,$xr-$iw-$r,$yl); 318 $this->img->Arc($xr-$iw-$r,$yl-$r,$r*2,$r*2,0,90); 319 320 // Right line 321 $this->img->Line($xr,$yt+$r,$xr,$yl-$ih-$r); 322 323 // Indent horizontal, Lower right arc 324 $this->img->Line($xr-$r,$yl-$ih,$xr-$iw+$r,$yl-$ih); 325 $this->img->Arc($xr-$r,$yl-$ih-$r,$r*2,$r*2,0,90); 326 327 // Indent vertical, Indent arc 328 $this->img->Line($xr-$iw,$yl-$r,$xr-$iw,$yl-$ih+$r); 329 $this->img->Arc($xr-$iw+$r,$yl-$ih+$r,$r*2,$r*2,180,270); 330 331 if( $aFillColor != '' ) { 332 $bc = $this->img->current_color_name; 333 $this->img->PushColor($aFillColor); 334 $this->img->FillToBorder($xt+$r,$yt+$r,$bc); 335 $this->img->PopColor(); 336 } 337 338 break; 339 340 case 3: // Lower left 341 // Top line, Top left & Top right arc 342 $this->img->Line($xt+$r,$yt,$xr-$r,$yt); 343 $this->img->Arc($xt+$r,$yt+$r,$r*2,$r*2,180,270); 344 $this->img->Arc($xr-$r,$yt+$r,$r*2,$r*2,270,360); 345 346 // Right line, Bottom right arc 347 $this->img->Line($xr,$yt+$r,$xr,$yl-$r); 348 $this->img->Arc($xr-$r,$yl-$r,$r*2,$r*2,0,90); 349 350 // Bottom line, Bottom left arc 351 $this->img->Line($xt+$iw+$r,$yl,$xr-$r,$yl); 352 $this->img->Arc($xt+$iw+$r,$yl-$r,$r*2,$r*2,90,180); 353 354 // Left line 355 $this->img->Line($xt,$yt+$r,$xt,$yl-$ih-$r); 356 357 // Indent horizontal, Lower left arc 358 $this->img->Line($xt+$r,$yl-$ih,$xt+$iw-$r,$yl-$ih); 359 $this->img->Arc($xt+$r,$yl-$ih-$r,$r*2,$r*2,90,180); 360 361 // Indent vertical, Indent arc 362 $this->img->Line($xt+$iw,$yl-$ih+$r,$xt+$iw,$yl-$r); 363 $this->img->Arc($xt+$iw-$r,$yl-$ih+$r,$r*2,$r*2,270,360); 364 365 if( $aFillColor != '' ) { 366 $bc = $this->img->current_color_name; 367 $this->img->PushColor($aFillColor); 368 $this->img->FillToBorder($xr-$r,$yt+$r,$bc); 369 $this->img->PopColor(); 370 } 371 372 break; 373 } 366 374 } 367 375 } … … 370 378 //=================================================== 371 379 // CLASS RectangleText 372 // Description: Draws a text paragraph inside a 380 // Description: Draws a text paragraph inside a 373 381 // rounded, possible filled, rectangle. 374 382 //=================================================== … … 380 388 private $iShadowWidth=3,$iShadowColor=''; 381 389 382 function CanvasRectangleText($aTxt='',$xl=0,$yt=0,$w=0,$h=0) {383 384 385 386 387 388 } 389 390 function __construct($aTxt='',$xl=0,$yt=0,$w=0,$h=0) { 391 $this->iTxt = new Text($aTxt); 392 $this->ix = $xl; 393 $this->iy = $yt; 394 $this->iw = $w; 395 $this->ih = $h; 396 } 397 390 398 function SetShadow($aColor='gray',$aWidth=3) { 391 392 399 $this->iShadowColor = $aColor; 400 $this->iShadowWidth = $aWidth; 393 401 } 394 402 395 403 function SetFont($FontFam,$aFontStyle,$aFontSize=12) { 396 404 $this->iTxt->SetFont($FontFam,$aFontStyle,$aFontSize); 397 405 } 398 406 399 407 function SetTxt($aTxt) { 400 408 $this->iTxt->Set($aTxt); 401 409 } 402 410 403 411 function ParagraphAlign($aParaAlign) { 404 412 $this->iParaAlign = $aParaAlign; 405 413 } 406 414 407 415 function SetFillColor($aFillColor) { 408 416 $this->iFillColor = $aFillColor; 409 417 } 410 418 411 419 function SetAutoMargin($aMargin) { 412 420 $this->iAutoBoxMargin=$aMargin; 413 421 } 414 422 415 423 function SetColor($aColor) { 416 424 $this->iColor = $aColor; 417 425 } 418 426 419 427 function SetFontColor($aColor) { 420 428 $this->iFontColor = $aColor; 421 429 } 422 430 423 431 function SetPos($xl=0,$yt=0,$w=0,$h=0) { 424 425 426 427 432 $this->ix = $xl; 433 $this->iy = $yt; 434 $this->iw = $w; 435 $this->ih = $h; 428 436 } 429 437 430 438 function Pos($xl=0,$yt=0,$w=0,$h=0) { 431 432 433 434 439 $this->ix = $xl; 440 $this->iy = $yt; 441 $this->iw = $w; 442 $this->ih = $h; 435 443 } 436 444 437 445 function Set($aTxt,$xl,$yt,$w=0,$h=0) { 438 439 440 441 442 446 $this->iTxt->Set($aTxt); 447 $this->ix = $xl; 448 $this->iy = $yt; 449 $this->iw = $w; 450 $this->ih = $h; 443 451 } 444 452 445 453 function SetCornerRadius($aRad=5) { 446 454 $this->ir = $aRad; 447 455 } 448 456 449 457 function Stroke($aImg,$scale) { 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 if( $this->iw == 0 ) 470 471 472 473 474 475 476 477 478 479 480 481 482 $aImg->PopColor(); 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 458 459 // If coordinates are specifed as negative this means we should 460 // treat them as abolsute (pixels) coordinates 461 if( $this->ix > 0 ) { 462 $this->ix = $scale->TranslateX($this->ix) ; 463 } 464 else { 465 $this->ix = -$this->ix; 466 } 467 468 if( $this->iy > 0 ) { 469 $this->iy = $scale->TranslateY($this->iy) ; 470 } 471 else { 472 $this->iy = -$this->iy; 473 } 474 475 list($this->iw,$this->ih) = $scale->Translate($this->iw,$this->ih) ; 476 477 if( $this->iw == 0 ) 478 $this->iw = round($this->iTxt->GetWidth($aImg) + $this->iAutoBoxMargin); 479 if( $this->ih == 0 ) { 480 $this->ih = round($this->iTxt->GetTextHeight($aImg) + $this->iAutoBoxMargin); 481 } 482 483 if( $this->iShadowColor != '' ) { 484 $aImg->PushColor($this->iShadowColor); 485 $aImg->FilledRoundedRectangle($this->ix+$this->iShadowWidth, 486 $this->iy+$this->iShadowWidth, 487 $this->ix+$this->iw-1+$this->iShadowWidth, 488 $this->iy+$this->ih-1+$this->iShadowWidth, 489 $this->ir); 490 $aImg->PopColor(); 491 } 492 493 if( $this->iFillColor != '' ) { 494 $aImg->PushColor($this->iFillColor); 495 $aImg->FilledRoundedRectangle($this->ix,$this->iy, 496 $this->ix+$this->iw-1, 497 $this->iy+$this->ih-1, 498 $this->ir); 499 $aImg->PopColor(); 500 } 501 502 if( $this->iColor != '' ) { 503 $aImg->PushColor($this->iColor); 504 $aImg->RoundedRectangle($this->ix,$this->iy, 505 $this->ix+$this->iw-1, 506 $this->iy+$this->ih-1, 507 $this->ir); 508 $aImg->PopColor(); 509 } 510 511 $this->iTxt->Align('center','center'); 512 $this->iTxt->ParagraphAlign($this->iParaAlign); 513 $this->iTxt->SetColor($this->iFontColor); 514 $this->iTxt->Stroke($aImg, $this->ix+$this->iw/2, $this->iy+$this->ih/2); 515 516 return array($this->iw, $this->ih); 509 517 510 518 }
Note:
See TracChangeset
for help on using the changeset viewer.