jpgraph_canvtools.php 16.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 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 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 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 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
<?php
/*=======================================================================
 // File:        JPGRAPH_CANVTOOLS.PHP
 // Description: Some utilities for text and shape drawing on a canvas
 // Created:     2002-08-23
 // Ver:         $Id: jpgraph_canvtools.php 1857 2009-09-28 14:38:14Z ljp $
 //
 // Copyright (c) Asial Corporation. All rights reserved.
 //========================================================================
 */

define('CORNER_TOPLEFT',0);
define('CORNER_TOPRIGHT',1);
define('CORNER_BOTTOMRIGHT',2);
define('CORNER_BOTTOMLEFT',3);


//===================================================
// CLASS CanvasScale
// Description: Define a scale for canvas so we
// can abstract away with absolute pixels
//===================================================

class CanvasScale {
    private $g;
    private $w,$h;
    private $ixmin=0,$ixmax=10,$iymin=0,$iymax=10;

    function __construct($graph,$xmin=0,$xmax=10,$ymin=0,$ymax=10) {
        $this->g = $graph;
        $this->w = $graph->img->width;
        $this->h = $graph->img->height;
        $this->ixmin = $xmin;
        $this->ixmax = $xmax;
        $this->iymin = $ymin;
        $this->iymax = $ymax;
    }

    function Set($xmin=0,$xmax=10,$ymin=0,$ymax=10) {
        $this->ixmin = $xmin;
        $this->ixmax = $xmax;
        $this->iymin = $ymin;
        $this->iymax = $ymax;
    }

    function Get() {
        return array($this->ixmin,$this->ixmax,$this->iymin,$this->iymax);
    }

    function Translate($x,$y) {
        $xp = round(($x-$this->ixmin)/($this->ixmax - $this->ixmin) * $this->w);
        $yp = round(($y-$this->iymin)/($this->iymax - $this->iymin) * $this->h);
        return array($xp,$yp);
    }

    function TranslateX($x) {
        $xp = round(($x-$this->ixmin)/($this->ixmax - $this->ixmin) * $this->w);
        return $xp;
    }

    function TranslateY($y) {
        $yp = round(($y-$this->iymin)/($this->iymax - $this->iymin) * $this->h);
        return $yp;
    }

}


//===================================================
// CLASS Shape
// Description: Methods to draw shapes on canvas
//===================================================
class Shape {
    private $img,$scale;

    function __construct($aGraph,$scale) {
        $this->img = $aGraph->img;
        $this->img->SetColor('black');
        $this->scale = $scale;
    }

    function SetColor($aColor) {
        $this->img->SetColor($aColor);
    }

    function Line($x1,$y1,$x2,$y2) {
        list($x1,$y1) = $this->scale->Translate($x1,$y1);
        list($x2,$y2) = $this->scale->Translate($x2,$y2);
        $this->img->Line($x1,$y1,$x2,$y2);
    }

    function SetLineWeight($aWeight) {
        $this->img->SetLineWeight($aWeight);
    }

    function Polygon($p,$aClosed=false) {
        $n=count($p);
        for($i=0; $i < $n; $i+=2 ) {
            $p[$i]   = $this->scale->TranslateX($p[$i]);
            $p[$i+1] = $this->scale->TranslateY($p[$i+1]);
        }
        $this->img->Polygon($p,$aClosed);
    }

    function FilledPolygon($p) {
        $n=count($p);
        for($i=0; $i < $n; $i+=2 ) {
            $p[$i]   = $this->scale->TranslateX($p[$i]);
            $p[$i+1] = $this->scale->TranslateY($p[$i+1]);
        }
        $this->img->FilledPolygon($p);
    }


    // Draw a bezier curve with defining points in the $aPnts array
    // using $aSteps steps.
    // 0=x0, 1=y0
    // 2=x1, 3=y1
    // 4=x2, 5=y2
    // 6=x3, 7=y3
    function Bezier($p,$aSteps=40) {
        $x0 = $p[0];
        $y0 = $p[1];
        // Calculate coefficients
        $cx = 3*($p[2]-$p[0]);
        $bx = 3*($p[4]-$p[2])-$cx;
        $ax = $p[6]-$p[0]-$cx-$bx;
        $cy = 3*($p[3]-$p[1]);
        $by = 3*($p[5]-$p[3])-$cy;
        $ay = $p[7]-$p[1]-$cy-$by;

        // Step size
        $delta = 1.0/$aSteps;

        $x_old = $x0;
        $y_old = $y0;
        for($t=$delta; $t<=1.0; $t+=$delta) {
            $tt = $t*$t; $ttt=$tt*$t;
            $x  = $ax*$ttt + $bx*$tt + $cx*$t + $x0;
            $y = $ay*$ttt + $by*$tt + $cy*$t + $y0;
            $this->Line($x_old,$y_old,$x,$y);
            $x_old = $x;
            $y_old = $y;
        }
        $this->Line($x_old,$y_old,$p[6],$p[7]);
    }

    function Rectangle($x1,$y1,$x2,$y2) {
        list($x1,$y1) = $this->scale->Translate($x1,$y1);
        list($x2,$y2)   = $this->scale->Translate($x2,$y2);
        $this->img->Rectangle($x1,$y1,$x2,$y2);
    }

    function FilledRectangle($x1,$y1,$x2,$y2) {
        list($x1,$y1) = $this->scale->Translate($x1,$y1);
        list($x2,$y2)   = $this->scale->Translate($x2,$y2);
        $this->img->FilledRectangle($x1,$y1,$x2,$y2);
    }

    function Circle($x1,$y1,$r) {
        list($x1,$y1) = $this->scale->Translate($x1,$y1);
        if( $r >= 0 )
        $r   = $this->scale->TranslateX($r);
        else
        $r = -$r;
        $this->img->Circle($x1,$y1,$r);
    }

    function FilledCircle($x1,$y1,$r) {
        list($x1,$y1) = $this->scale->Translate($x1,$y1);
        if( $r >= 0 )
        $r   = $this->scale->TranslateX($r);
        else
        $r = -$r;
        $this->img->FilledCircle($x1,$y1,$r);
    }

    function RoundedRectangle($x1,$y1,$x2,$y2,$r=null) {
        list($x1,$y1) = $this->scale->Translate($x1,$y1);
        list($x2,$y2)   = $this->scale->Translate($x2,$y2);

        if( $r == null )
        $r = 5;
        elseif( $r >= 0 )
        $r = $this->scale->TranslateX($r);
        else
        $r = -$r;
        $this->img->RoundedRectangle($x1,$y1,$x2,$y2,$r);
    }

    function FilledRoundedRectangle($x1,$y1,$x2,$y2,$r=null) {
        list($x1,$y1) = $this->scale->Translate($x1,$y1);
        list($x2,$y2)   = $this->scale->Translate($x2,$y2);

        if( $r == null )
        $r = 5;
        elseif( $r > 0 )
        $r = $this->scale->TranslateX($r);
        else
        $r = -$r;
        $this->img->FilledRoundedRectangle($x1,$y1,$x2,$y2,$r);
    }

    function ShadowRectangle($x1,$y1,$x2,$y2,$fcolor=false,$shadow_width=null,$shadow_color=array(102,102,102)) {
        list($x1,$y1) = $this->scale->Translate($x1,$y1);
        list($x2,$y2) = $this->scale->Translate($x2,$y2);
        if( $shadow_width == null )
        $shadow_width=4;
        else
        $shadow_width=$this->scale->TranslateX($shadow_width);
        $this->img->ShadowRectangle($x1,$y1,$x2,$y2,$fcolor,$shadow_width,$shadow_color);
    }

    function SetTextAlign($halign,$valign="bottom") {
        $this->img->SetTextAlign($halign,$valign="bottom");
    }

    function StrokeText($x1,$y1,$txt,$dir=0,$paragraph_align="left") {
        list($x1,$y1) = $this->scale->Translate($x1,$y1);
        $this->img->StrokeText($x1,$y1,$txt,$dir,$paragraph_align);
    }

    // A rounded rectangle where one of the corner has been moved "into" the
    // rectangle 'iw' width and 'ih' height. Corners:
    // 0=Top left, 1=top right, 2=bottom right, 3=bottom left
    function IndentedRectangle($xt,$yt,$w,$h,$iw=0,$ih=0,$aCorner=3,$aFillColor="",$r=4) {

        list($xt,$yt) = $this->scale->Translate($xt,$yt);
        list($w,$h)   = $this->scale->Translate($w,$h);
        list($iw,$ih) = $this->scale->Translate($iw,$ih);

        $xr = $xt + $w - 0;
        $yl = $yt + $h - 0;

        switch( $aCorner ) {
            case 0: // Upper left
                 
                // Bottom line, left &  right arc
                $this->img->Line($xt+$r,$yl,$xr-$r,$yl);
                $this->img->Arc($xt+$r,$yl-$r,$r*2,$r*2,90,180);
                $this->img->Arc($xr-$r,$yl-$r,$r*2,$r*2,0,90);

                // Right line, Top right arc
                $this->img->Line($xr,$yt+$r,$xr,$yl-$r);
                $this->img->Arc($xr-$r,$yt+$r,$r*2,$r*2,270,360);

                // Top line, Top left arc
                $this->img->Line($xt+$iw+$r,$yt,$xr-$r,$yt);
                $this->img->Arc($xt+$iw+$r,$yt+$r,$r*2,$r*2,180,270);

                // Left line
                $this->img->Line($xt,$yt+$ih+$r,$xt,$yl-$r);

                // Indent horizontal, Lower left arc
                $this->img->Line($xt+$r,$yt+$ih,$xt+$iw-$r,$yt+$ih);
                $this->img->Arc($xt+$r,$yt+$ih+$r,$r*2,$r*2,180,270);

                // Indent vertical, Indent arc
                $this->img->Line($xt+$iw,$yt+$r,$xt+$iw,$yt+$ih-$r);
                $this->img->Arc($xt+$iw-$r,$yt+$ih-$r,$r*2,$r*2,0,90);

                if( $aFillColor != '' ) {
                    $bc = $this->img->current_color_name;
                    $this->img->PushColor($aFillColor);
                    $this->img->FillToBorder($xr-$r,$yl-$r,$bc);
                    $this->img->PopColor();
                }

                break;

            case 1: // Upper right

                // Bottom line, left &  right arc
                $this->img->Line($xt+$r,$yl,$xr-$r,$yl);
                $this->img->Arc($xt+$r,$yl-$r,$r*2,$r*2,90,180);
                $this->img->Arc($xr-$r,$yl-$r,$r*2,$r*2,0,90);

                // Left line, Top left arc
                $this->img->Line($xt,$yt+$r,$xt,$yl-$r);
                $this->img->Arc($xt+$r,$yt+$r,$r*2,$r*2,180,270);

                // Top line, Top right arc
                $this->img->Line($xt+$r,$yt,$xr-$iw-$r,$yt);
                $this->img->Arc($xr-$iw-$r,$yt+$r,$r*2,$r*2,270,360);

                // Right line
                $this->img->Line($xr,$yt+$ih+$r,$xr,$yl-$r);

                // Indent horizontal, Lower right arc
                $this->img->Line($xr-$iw+$r,$yt+$ih,$xr-$r,$yt+$ih);
                $this->img->Arc($xr-$r,$yt+$ih+$r,$r*2,$r*2,270,360);

                // Indent vertical, Indent arc
                $this->img->Line($xr-$iw,$yt+$r,$xr-$iw,$yt+$ih-$r);
                $this->img->Arc($xr-$iw+$r,$yt+$ih-$r,$r*2,$r*2,90,180);

                if( $aFillColor != '' ) {
                    $bc = $this->img->current_color_name;
                    $this->img->PushColor($aFillColor);
                    $this->img->FillToBorder($xt+$r,$yl-$r,$bc);
                    $this->img->PopColor();
                }

                break;

            case 2: // Lower right
                // Top line, Top left & Top right arc
                $this->img->Line($xt+$r,$yt,$xr-$r,$yt);
                $this->img->Arc($xt+$r,$yt+$r,$r*2,$r*2,180,270);
                $this->img->Arc($xr-$r,$yt+$r,$r*2,$r*2,270,360);

                // Left line, Bottom left arc
                $this->img->Line($xt,$yt+$r,$xt,$yl-$r);
                $this->img->Arc($xt+$r,$yl-$r,$r*2,$r*2,90,180);

                // Bottom line, Bottom right arc
                $this->img->Line($xt+$r,$yl,$xr-$iw-$r,$yl);
                $this->img->Arc($xr-$iw-$r,$yl-$r,$r*2,$r*2,0,90);

                // Right line
                $this->img->Line($xr,$yt+$r,$xr,$yl-$ih-$r);
                 
                // Indent horizontal, Lower right arc
                $this->img->Line($xr-$r,$yl-$ih,$xr-$iw+$r,$yl-$ih);
                $this->img->Arc($xr-$r,$yl-$ih-$r,$r*2,$r*2,0,90);

                // Indent vertical, Indent arc
                $this->img->Line($xr-$iw,$yl-$r,$xr-$iw,$yl-$ih+$r);
                $this->img->Arc($xr-$iw+$r,$yl-$ih+$r,$r*2,$r*2,180,270);

                if( $aFillColor != '' ) {
                    $bc = $this->img->current_color_name;
                    $this->img->PushColor($aFillColor);
                    $this->img->FillToBorder($xt+$r,$yt+$r,$bc);
                    $this->img->PopColor();
                }

                break;

            case 3: // Lower left
                // Top line, Top left & Top right arc
                $this->img->Line($xt+$r,$yt,$xr-$r,$yt);
                $this->img->Arc($xt+$r,$yt+$r,$r*2,$r*2,180,270);
                $this->img->Arc($xr-$r,$yt+$r,$r*2,$r*2,270,360);

                // Right line, Bottom right arc
                $this->img->Line($xr,$yt+$r,$xr,$yl-$r);
                $this->img->Arc($xr-$r,$yl-$r,$r*2,$r*2,0,90);

                // Bottom line, Bottom left arc
                $this->img->Line($xt+$iw+$r,$yl,$xr-$r,$yl);
                $this->img->Arc($xt+$iw+$r,$yl-$r,$r*2,$r*2,90,180);

                // Left line
                $this->img->Line($xt,$yt+$r,$xt,$yl-$ih-$r);
                 
                // Indent horizontal, Lower left arc
                $this->img->Line($xt+$r,$yl-$ih,$xt+$iw-$r,$yl-$ih);
                $this->img->Arc($xt+$r,$yl-$ih-$r,$r*2,$r*2,90,180);

                // Indent vertical, Indent arc
                $this->img->Line($xt+$iw,$yl-$ih+$r,$xt+$iw,$yl-$r);
                $this->img->Arc($xt+$iw-$r,$yl-$ih+$r,$r*2,$r*2,270,360);

                if( $aFillColor != '' ) {
                    $bc = $this->img->current_color_name;
                    $this->img->PushColor($aFillColor);
                    $this->img->FillToBorder($xr-$r,$yt+$r,$bc);
                    $this->img->PopColor();
                }

                break;
        }
    }
}


//===================================================
// CLASS RectangleText
// Description: Draws a text paragraph inside a
// rounded, possible filled, rectangle.
//===================================================
class CanvasRectangleText {
    private $ix,$iy,$iw,$ih,$ir=4;
    private $iTxt,$iColor='black',$iFillColor='',$iFontColor='black';
    private $iParaAlign='center';
    private $iAutoBoxMargin=5;
    private $iShadowWidth=3,$iShadowColor='';

    function __construct($aTxt='',$xl=0,$yt=0,$w=0,$h=0) {
        $this->iTxt = new Text($aTxt);
        $this->ix = $xl;
        $this->iy = $yt;
        $this->iw = $w;
        $this->ih = $h;
    }

    function SetShadow($aColor='gray',$aWidth=3) {
        $this->iShadowColor = $aColor;
        $this->iShadowWidth = $aWidth;
    }

    function SetFont($FontFam,$aFontStyle,$aFontSize=12) {
        $this->iTxt->SetFont($FontFam,$aFontStyle,$aFontSize);
    }

    function SetTxt($aTxt) {
        $this->iTxt->Set($aTxt);
    }

    function ParagraphAlign($aParaAlign) {
        $this->iParaAlign = $aParaAlign;
    }

    function SetFillColor($aFillColor) {
        $this->iFillColor = $aFillColor;
    }

    function SetAutoMargin($aMargin) {
        $this->iAutoBoxMargin=$aMargin;
    }

    function SetColor($aColor) {
        $this->iColor = $aColor;
    }

    function SetFontColor($aColor) {
        $this->iFontColor = $aColor;
    }

    function SetPos($xl=0,$yt=0,$w=0,$h=0) {
        $this->ix = $xl;
        $this->iy = $yt;
        $this->iw = $w;
        $this->ih = $h;
    }

    function Pos($xl=0,$yt=0,$w=0,$h=0) {
        $this->ix = $xl;
        $this->iy = $yt;
        $this->iw = $w;
        $this->ih = $h;
    }

    function Set($aTxt,$xl,$yt,$w=0,$h=0) {
        $this->iTxt->Set($aTxt);
        $this->ix = $xl;
        $this->iy = $yt;
        $this->iw = $w;
        $this->ih = $h;
    }

    function SetCornerRadius($aRad=5) {
        $this->ir = $aRad;
    }

    function Stroke($aImg,$scale) {

        // If coordinates are specifed as negative this means we should
        // treat them as abolsute (pixels) coordinates
        if( $this->ix > 0 ) {
            $this->ix = $scale->TranslateX($this->ix) ;
        }
        else {
            $this->ix = -$this->ix;
        }

        if( $this->iy > 0 ) {
            $this->iy = $scale->TranslateY($this->iy) ;
        }
        else {
            $this->iy = -$this->iy;
        }
         
        list($this->iw,$this->ih) = $scale->Translate($this->iw,$this->ih) ;

        if( $this->iw == 0 )
        $this->iw = round($this->iTxt->GetWidth($aImg) + $this->iAutoBoxMargin);
        if( $this->ih == 0 ) {
            $this->ih = round($this->iTxt->GetTextHeight($aImg) + $this->iAutoBoxMargin);
        }

        if( $this->iShadowColor != '' ) {
            $aImg->PushColor($this->iShadowColor);
            $aImg->FilledRoundedRectangle($this->ix+$this->iShadowWidth,
            $this->iy+$this->iShadowWidth,
            $this->ix+$this->iw-1+$this->iShadowWidth,
            $this->iy+$this->ih-1+$this->iShadowWidth,
            $this->ir);
            $aImg->PopColor();
        }

        if( $this->iFillColor != '' ) {
            $aImg->PushColor($this->iFillColor);
            $aImg->FilledRoundedRectangle($this->ix,$this->iy,
            $this->ix+$this->iw-1,
            $this->iy+$this->ih-1,
            $this->ir);
            $aImg->PopColor();
        }

        if( $this->iColor != '' ) {
            $aImg->PushColor($this->iColor);
            $aImg->RoundedRectangle($this->ix,$this->iy,
            $this->ix+$this->iw-1,
            $this->iy+$this->ih-1,
            $this->ir);
            $aImg->PopColor();
        }

        $this->iTxt->Align('center','center');
        $this->iTxt->ParagraphAlign($this->iParaAlign);
        $this->iTxt->SetColor($this->iFontColor);
        $this->iTxt->Stroke($aImg, $this->ix+$this->iw/2, $this->iy+$this->ih/2);

        return array($this->iw, $this->ih);

    }

}


?>