[ Index ]
 

Code source de phpMyVisites 2.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/libs/artichow/php4/inc/ -> Legend.class.php (source)

   1  <?php
   2  /*
   3   * This work is hereby released into the Public Domain.
   4   * To view a copy of the public domain dedication,
   5   * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
   6   * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
   7   *
   8   */
   9  
  10   
  11  /* <php4> */
  12  
  13  define("LEGEND_LINE", 1);
  14  define("LEGEND_BACKGROUND", 2);
  15  define("LEGEND_MARK", 3);
  16  define("LEGEND_MARKONLY", 4);
  17  
  18  define("LEGEND_MODEL_RIGHT", 1);
  19  define("LEGEND_MODEL_BOTTOM", 2);
  20  
  21  define("LEGEND_LEFT", 1);
  22  define("LEGEND_RIGHT", 2);
  23  define("LEGEND_CENTER", 3);
  24  define("LEGEND_TOP", 4);
  25  define("LEGEND_BOTTOM", 5);
  26  define("LEGEND_MIDDLE", 6);
  27  
  28  /* </php4> */
  29  
  30  /**
  31   * Some legends
  32   *
  33   * @package Artichow
  34   */
  35  class awLegend {
  36  
  37      /**
  38       * Legends added
  39       *
  40       * @var array
  41       */
  42      var $legends = array();
  43  
  44      /**
  45       * The current component
  46       *
  47       * @var Component
  48       */
  49      var $component;
  50      
  51      /**
  52       * Background color or gradient
  53       *
  54       * @var Color, Gradient
  55       */
  56      var $background;
  57      
  58      /**
  59       * Text color
  60       *
  61       * @var Color
  62       */
  63      var $textColor;
  64      
  65      /**
  66       * Text font
  67       *
  68       * @var Font
  69       */
  70      var $textFont;
  71      
  72      /**
  73       * Text margin
  74       *
  75       * @var Side
  76       */
  77      var $textMargin;
  78      
  79      /**
  80       * Number of columns
  81       *
  82       * @var int
  83       */
  84      var $columns = NULL;
  85      
  86      /**
  87       * Number of rows
  88       *
  89       * @var int
  90       */
  91      var $rows = NULL;
  92      
  93      /**
  94       * Legend position
  95       *
  96       * @var Point
  97       */
  98      var $position;
  99      
 100      /**
 101       * Hide legend ?
 102       *
 103       * @var bool
 104       */
 105      var $hide = FALSE;
 106      
 107      /**
 108       * Space between each legend
 109       *
 110       * @var int
 111       */
 112      var $space = 4;
 113      
 114      /**
 115       * Horizontal alignment
 116       *
 117       * @var int
 118       */
 119      var $hAlign;
 120      
 121      /**
 122       * Vertical alignment
 123       *
 124       * @var int
 125       */
 126      var $vAlign;
 127  
 128      /**
 129       * Margin
 130       *
 131       * @var array Array for left, right, top and bottom margins
 132       */
 133      var $margin;
 134      
 135      /**
 136       * Legend shadow
 137       *
 138       * @var Shadow
 139       */
 140      var $shadow;
 141      
 142      /**
 143       * Legend border
 144       *
 145       * @var Border
 146       */
 147      var $border;
 148      
 149      /**
 150       * Line legend
 151       *
 152       * @var int
 153       */
 154      
 155      
 156      /**
 157       * Color/Gradient background legend
 158       *
 159       * @var int
 160       */
 161      
 162      
 163      /**
 164       * Use marks and line as legend
 165       *
 166       * @var int
 167       */
 168      
 169      
 170      /**
 171       * Use marks as legend
 172       *
 173       * @var int
 174       */
 175      
 176      
 177      /**
 178       * Right side model
 179       *
 180       * @var int
 181       */
 182      
 183      
 184      /**
 185       * Bottom side model
 186       *
 187       * @var int
 188       */
 189      
 190  
 191      /**
 192       * Build the legend
 193       *
 194       * @param int $model Legend model
 195       */
 196  	 function awLegend($model = LEGEND_MODEL_RIGHT) {
 197      
 198          $this->shadow = new awShadow(SHADOW_LEFT_BOTTOM);
 199          $this->border = new awBorder;
 200          
 201          $this->textMargin = new awSide(4);
 202          $this->setModel($model);
 203          
 204      }
 205      
 206      /**
 207       * Set a predefined model for the legend
 208       *
 209       * @param int $model
 210       */
 211  	 function setModel($model) {
 212          
 213          $this->setBackgroundColor(new awColor(255, 255, 255, 15));
 214          $this->setPadding(8, 8, 8, 8);
 215          $this->setTextFont(new awFont2);
 216          $this->shadow->setSize(3);
 217      
 218          switch($model) {
 219          
 220              case LEGEND_MODEL_RIGHT :
 221              
 222                  $this->setColumns(1);
 223                  $this->setAlign(LEGEND_RIGHT, LEGEND_MIDDLE);
 224                  $this->setPosition(0.96, 0.50);
 225              
 226                  break;
 227          
 228              case LEGEND_MODEL_BOTTOM :
 229              
 230                  $this->setRows(1);
 231                  $this->setAlign(LEGEND_CENTER, LEGEND_TOP);
 232                  $this->setPosition(0.50, 0.92);
 233              
 234                  break;
 235                  
 236              default :
 237              
 238                  $this->setPosition(0.5, 0.5);
 239                  
 240                  break;
 241          
 242          }
 243      
 244      }
 245      
 246      /**
 247       * Hide legend ?
 248       *
 249       * @param bool $hide TRUE to hide legend, FALSE otherwise
 250       */
 251  	 function hide($hide = TRUE) {
 252          $this->hide = (bool)$hide;
 253      }
 254      
 255      /**
 256       * Show legend ?
 257       *
 258       * @param bool $show
 259       */
 260  	 function show($show = TRUE) {
 261          $this->hide = (bool)!$show;
 262      }
 263      
 264      
 265      /**
 266       * Add a Legendable object to the legend
 267       *
 268       * @param &$legendable
 269       * @param string $title Legend title
 270       * @param int $type Legend type (default to LEGEND_LINE)
 271       */
 272  	 function add(&$legendable, $title, $type = LEGEND_LINE) {
 273      
 274          $legend = array($legendable, $title, $type);
 275      
 276          $this->legends[] = $legend;
 277          
 278      }
 279      
 280      /**
 281       * Change legend padding
 282       *
 283       * @param int $left
 284       * @param int $right
 285       * @param int $top
 286       * @param int $bottom
 287       */
 288  	 function setPadding($left, $right, $top, $bottom) {
 289          $this->padding = array((int)$left, (int)$right, (int)$top, (int)$bottom);
 290      }
 291      
 292      /**
 293       * Change space between each legend
 294       *
 295       * @param int $space
 296       */
 297  	 function setSpace($space) {
 298          $this->space = (int)$space;
 299      }
 300      
 301      /**
 302       * Change alignment
 303       *
 304       * @param int $h Horizontal alignment
 305       * @param int $v Vertical alignment
 306       */
 307  	 function setAlign($h = NULL, $v = NULL) {
 308          if($h !== NULL) {
 309              $this->hAlign = (int)$h;
 310          }
 311          if($v !== NULL) {
 312              $this->vAlign = (int)$v;
 313          }
 314      }
 315      
 316      /**
 317       * Change number of columns
 318       *
 319       * @param int $columns
 320       */
 321  	 function setColumns($columns) {
 322          $this->rows = NULL;
 323          $this->columns = (int)$columns;
 324      }
 325      
 326      /**
 327       * Change number of rows
 328       *
 329       * @param int $rows
 330       */
 331  	 function setRows($rows) {
 332          $this->columns = NULL;
 333          $this->rows = (int)$rows;
 334      }
 335      
 336      /**
 337       * Change legend position
 338       * X and Y positions must be between 0 and 1.
 339       *
 340       * @param float $x
 341       * @param float $y
 342       */
 343  	 function setPosition($x = NULL, $y = NULL) {
 344          $x = (is_null($x) and !is_null($this->position)) ? $this->position->x : $x;
 345          $y = (is_null($y) and !is_null($this->position)) ? $this->position->y : $y;
 346          
 347          $this->position = new awPoint($x, $y);
 348      }
 349      
 350      /**
 351       * Get legend position
 352       *
 353       * @return Point
 354       */
 355  	 function getPosition() {
 356          return $this->position;
 357      }
 358      
 359      /**
 360       * Change text font
 361       *
 362       * @param &$font
 363       */
 364  	 function setTextFont(&$font) {
 365          $this->textFont = $font;
 366      }
 367      
 368      /**
 369       * Change text margin
 370       *
 371       * @param int $left
 372       * @param int $right
 373       */
 374  	 function setTextMargin($left, $right) {
 375          $this->textMargin->set($left, $right);
 376      }
 377      
 378      /**
 379       * Change text color
 380       *
 381       * @param $color
 382       */
 383  	 function setTextColor($color) {
 384          $this->textColor = $color;
 385      }
 386      
 387      /**
 388       * Change background
 389       *
 390       * @param mixed $background
 391       */
 392  	 function setBackground($background) {
 393          $this->background = $background;
 394      }
 395      
 396      /**
 397       * Change background color
 398       *
 399       * @param $color
 400       */
 401  	 function setBackgroundColor($color) {
 402          $this->background = $color;
 403      }
 404      
 405      /**
 406       * Change background gradient
 407       *
 408       * @param $gradient
 409       */
 410  	 function setBackgroundGradient($gradient) {
 411          $this->background = $gradient;
 412      }
 413      
 414      /**
 415       * Count the number of Legendable objects in the legend
 416       *
 417       * @return int
 418       */
 419  	 function count() {
 420          return count($this->legends);
 421      }
 422      
 423  	 function draw($drawer) {
 424          
 425          if($this->hide) {
 426              return;
 427          }
 428      
 429          $count = $this->count();
 430          
 431          // No legend to print
 432          if($count === 0) {
 433              return;
 434          }
 435          
 436          // Get text widths and heights of each element of the legend
 437          $widths = array();
 438          $heights = array();
 439          $texts = array();
 440          for($i = 0; $i < $count; $i++) {
 441              list(, $title, ) = $this->legends[$i];
 442              $text = new awText(
 443                  $title,
 444                  $this->textFont,
 445                  $this->textColor,
 446                  0
 447              );
 448              $font = $text->getFont();
 449              $widths[$i] = $font->getTextWidth($text) + $this->textMargin->left + $this->textMargin->right;
 450              $heights[$i] = $font->getTextHeight($text);
 451              $texts[$i] = $text;
 452          }
 453          
 454          // Maximum height of the font used
 455          $heightMax = array_max($heights);
 456          
 457          // Get number of columns
 458          if($this->columns !== NULL) {
 459              $columns = $this->columns;
 460          } else if($this->rows !== NULL) {
 461              $columns = ceil($count / $this->rows);
 462          } else {
 463              $columns = $count;
 464          }
 465          
 466          // Number of  rows
 467          $rows = (int)ceil($count / $columns);
 468          
 469          // Get maximum with of each column
 470          $widthMax = array();
 471          for($i = 0; $i < $count; $i++) {
 472              // Get column width
 473              $column = $i % $columns;
 474              if(array_key_exists($column, $widthMax) === FALSE) {
 475                  $widthMax[$column] = $widths[$i];
 476              } else {
 477                  $widthMax[$column] = max($widthMax[$column], $widths[$i]);
 478              }
 479          }
 480          
 481          $width = $this->padding[0] + $this->padding[1] - $this->space;
 482          for($i = 0; $i < $columns; $i++) {
 483              $width += $this->space + 5 + 10 + $widthMax[$i];
 484          }
 485          
 486          $height = ($heightMax + $this->space) * $rows - $this->space + $this->padding[2] + $this->padding[3];
 487          
 488          // Look for legends position
 489          list($x, $y) = $drawer->getSize();
 490          
 491          $p = new awPoint(
 492              $this->position->x * $x,
 493              $this->position->y * $y
 494          );
 495          
 496          switch($this->hAlign) {
 497          
 498              case LEGEND_CENTER :
 499                  $p->x -= $width / 2;
 500                  break;
 501          
 502              case LEGEND_RIGHT :
 503                  $p->x -= $width;
 504                  break;
 505          
 506          }
 507          
 508          switch($this->vAlign) {
 509          
 510              case LEGEND_MIDDLE :
 511                  $p->y -= $height / 2;
 512                  break;
 513          
 514              case LEGEND_BOTTOM :
 515                  $p->y -= $height;
 516                  break;
 517          
 518          }
 519          
 520          // Draw legend shadow
 521          $this->shadow->draw(
 522              $drawer,
 523              $p,
 524              $p->move($width, $height),
 525              SHADOW_OUT
 526          );
 527          
 528          // Draw legends base
 529          $this->drawBase($drawer, $p, $width, $height);
 530          
 531          // Draw each legend
 532          for($i = 0; $i < $count; $i++) {
 533          
 534              list($component, $title, $type) = $this->legends[$i];
 535          
 536              $column = $i % $columns;
 537              $row = (int)floor($i / $columns);
 538              
 539              // Get width of all previous columns
 540              $previousColumns = 0;
 541              for($j = 0; $j < $column; $j++) {
 542                  $previousColumns += $this->space + 10 + 5 + $widthMax[$j];
 543              }
 544              
 545              // Draw legend text
 546              $drawer->string(
 547                  $texts[$i],
 548                  $p->move(
 549                      $this->padding[0] + $previousColumns + 10 + 5 + $this->textMargin->left,
 550                      $this->padding[2] + $row * ($heightMax + $this->space) + $heightMax / 2 - $heights[$i] / 2
 551                  )
 552              );
 553              
 554              // Draw legend icon
 555              switch($type) {
 556              
 557                  case LEGEND_LINE :
 558                  case LEGEND_MARK :
 559                  case LEGEND_MARKONLY :
 560                  
 561                      // Get vertical position
 562                      $x = $this->padding[0] + $previousColumns;
 563                      $y = $this->padding[2] + $row * ($heightMax + $this->space) + $heightMax / 2 - $component->getLegendLineThickness();
 564                      
 565                      // Draw two lines
 566                      if($component->getLegendLineColor() !== NULL) {
 567                      
 568                          $color = $component->getLegendLineColor();
 569                  
 570                          if(is_a($color, 'awColor') and $type !== LEGEND_MARKONLY) {
 571                          
 572                              $drawer->line(
 573                                  $color,
 574                                  new awLine(
 575                                      $p->move(
 576                                          $x, // YaPB ??
 577                                          $y + $component->getLegendLineThickness() / 2
 578                                      ),
 579                                      $p->move(
 580                                          $x + 10,
 581                                          $y + $component->getLegendLineThickness() / 2
 582                                      ),
 583                                      $component->getLegendLineStyle(),
 584                                      $component->getLegendLineThickness()
 585                                  )
 586                              );
 587                          
 588                              $color->free();
 589                              unset($color);
 590                              
 591                          }
 592                          
 593                      }
 594                      
 595                      if($type === LEGEND_MARK or $type === LEGEND_MARKONLY)  {
 596                      
 597                          $mark = $component->getLegendMark();
 598                      
 599                          if($mark !== NULL) {
 600                              $mark->draw(
 601                                  $drawer,
 602                                  $p->move(
 603                                      $x + 5.5,
 604                                      $y + $component->getLegendLineThickness() / 2
 605                                  )
 606                              );
 607                          }
 608                          
 609                          unset($mark);
 610                      
 611                      }
 612                      
 613                      break;
 614                      
 615                  case LEGEND_BACKGROUND :
 616                  
 617                      // Get vertical position
 618                      $x = $this->padding[0] + $previousColumns;
 619                      $y = $this->padding[2] + $row * ($heightMax + $this->space) + $heightMax / 2 - 5;
 620                      
 621                      $from = $p->move(
 622                          $x,
 623                          $y
 624                      );
 625                      
 626                      $to = $p->move(
 627                          $x + 10,
 628                          $y + 10
 629                      );
 630                      
 631                      $background = $component->getLegendBackground();
 632                      
 633                      if($background !== NULL) {
 634                  
 635                          $drawer->filledRectangle(
 636                              $background,
 637                              new awLine($from, $to)
 638                          );
 639              
 640                          // Draw rectangle border
 641                          $this->border->rectangle(
 642                              $drawer,
 643                              $from->move(0, 0),
 644                              $to->move(0, 0)
 645                          );
 646                          
 647                      }
 648                      
 649                      unset($background, $from, $to);
 650                  
 651                      break;
 652              
 653              }
 654          
 655          }
 656      
 657      }
 658      
 659  	 function drawBase($drawer, $p, $width, $height) {
 660  
 661          $this->border->rectangle(
 662              $drawer,
 663              $p,
 664              $p->move($width, $height)
 665          );
 666          
 667          $size = $this->border->visible() ? 1 : 0;
 668          
 669          $drawer->filledRectangle(
 670              $this->background,
 671              new awLine(
 672                  $p->move($size, $size),
 673                  $p->move($width - $size, $height - $size)
 674              )
 675          );
 676          
 677      }
 678  
 679  }
 680  
 681  registerClass('Legend');
 682  
 683  /**
 684   * You can add a legend to components which implements this interface
 685   *
 686   * @package Artichow
 687   */
 688  
 689  
 690  registerInterface('Legendable');
 691  ?>


Généré le : Mon Nov 26 14:10:01 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics