[ Index ] |
|
Code source de phpMyVisites 2.3 |
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 implements awPositionable { 36 37 /** 38 * Legends added 39 * 40 * @var array 41 */ 42 protected $legends = array(); 43 44 /** 45 * The current component 46 * 47 * @var Component 48 */ 49 protected $component; 50 51 /** 52 * Background color or gradient 53 * 54 * @var Color, Gradient 55 */ 56 protected $background; 57 58 /** 59 * Text color 60 * 61 * @var Color 62 */ 63 protected $textColor; 64 65 /** 66 * Text font 67 * 68 * @var Font 69 */ 70 protected $textFont; 71 72 /** 73 * Text margin 74 * 75 * @var Side 76 */ 77 protected $textMargin; 78 79 /** 80 * Number of columns 81 * 82 * @var int 83 */ 84 protected $columns = NULL; 85 86 /** 87 * Number of rows 88 * 89 * @var int 90 */ 91 protected $rows = NULL; 92 93 /** 94 * Legend position 95 * 96 * @var Point 97 */ 98 protected $position; 99 100 /** 101 * Hide legend ? 102 * 103 * @var bool 104 */ 105 protected $hide = FALSE; 106 107 /** 108 * Space between each legend 109 * 110 * @var int 111 */ 112 protected $space = 4; 113 114 /** 115 * Horizontal alignment 116 * 117 * @var int 118 */ 119 protected $hAlign; 120 121 /** 122 * Vertical alignment 123 * 124 * @var int 125 */ 126 protected $vAlign; 127 128 /** 129 * Margin 130 * 131 * @var array Array for left, right, top and bottom margins 132 */ 133 private $margin; 134 135 /** 136 * Legend shadow 137 * 138 * @var Shadow 139 */ 140 public $shadow; 141 142 /** 143 * Legend border 144 * 145 * @var Border 146 */ 147 public $border; 148 149 /** 150 * Line legend 151 * 152 * @var int 153 */ 154 const LINE = 1; 155 156 /** 157 * Color/Gradient background legend 158 * 159 * @var int 160 */ 161 const BACKGROUND = 2; 162 163 /** 164 * Use marks and line as legend 165 * 166 * @var int 167 */ 168 const MARK = 3; 169 170 /** 171 * Use marks as legend 172 * 173 * @var int 174 */ 175 const MARKONLY = 4; 176 177 /** 178 * Right side model 179 * 180 * @var int 181 */ 182 const MODEL_RIGHT = 1; 183 184 /** 185 * Bottom side model 186 * 187 * @var int 188 */ 189 const MODEL_BOTTOM = 2; 190 191 /** 192 * Build the legend 193 * 194 * @param int $model Legend model 195 */ 196 public function __construct($model = awLegend::MODEL_RIGHT) { 197 198 $this->shadow = new awShadow(awShadow::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 public 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 awLegend::MODEL_RIGHT : 221 222 $this->setColumns(1); 223 $this->setAlign(awLegend::RIGHT, awLegend::MIDDLE); 224 $this->setPosition(0.96, 0.50); 225 226 break; 227 228 case awLegend::MODEL_BOTTOM : 229 230 $this->setRows(1); 231 $this->setAlign(awLegend::CENTER, awLegend::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 public function hide($hide = TRUE) { 252 $this->hide = (bool)$hide; 253 } 254 255 /** 256 * Show legend ? 257 * 258 * @param bool $show 259 */ 260 public function show($show = TRUE) { 261 $this->hide = (bool)!$show; 262 } 263 264 265 /** 266 * Add a Legendable object to the legend 267 * 268 * @param awLegendable $legendable 269 * @param string $title Legend title 270 * @param int $type Legend type (default to awLegend::LINE) 271 */ 272 public function add(awLegendable $legendable, $title, $type = awLegend::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 public 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 public 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 public 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 public 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 public 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 public 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 public function getPosition() { 356 return $this->position; 357 } 358 359 /** 360 * Change text font 361 * 362 * @param awFont $font 363 */ 364 public function setTextFont(awFont $font) { 365 $this->textFont = $font; 366 } 367 368 /** 369 * Change text margin 370 * 371 * @param int $left 372 * @param int $right 373 */ 374 public function setTextMargin($left, $right) { 375 $this->textMargin->set($left, $right); 376 } 377 378 /** 379 * Change text color 380 * 381 * @param awColor $color 382 */ 383 public function setTextColor(awColor $color) { 384 $this->textColor = $color; 385 } 386 387 /** 388 * Change background 389 * 390 * @param mixed $background 391 */ 392 public function setBackground($background) { 393 $this->background = $background; 394 } 395 396 /** 397 * Change background color 398 * 399 * @param awColor $color 400 */ 401 public function setBackgroundColor(awColor $color) { 402 $this->background = $color; 403 } 404 405 /** 406 * Change background gradient 407 * 408 * @param awGradient $gradient 409 */ 410 public function setBackgroundGradient(awGradient $gradient) { 411 $this->background = $gradient; 412 } 413 414 /** 415 * Count the number of Legendable objects in the legend 416 * 417 * @return int 418 */ 419 public function count() { 420 return count($this->legends); 421 } 422 423 public function draw(awDrawer $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 awLegend::CENTER : 499 $p->x -= $width / 2; 500 break; 501 502 case awLegend::RIGHT : 503 $p->x -= $width; 504 break; 505 506 } 507 508 switch($this->vAlign) { 509 510 case awLegend::MIDDLE : 511 $p->y -= $height / 2; 512 break; 513 514 case awLegend::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 awShadow::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 awLegend::LINE : 558 case awLegend::MARK : 559 case awLegend::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($color instanceof awColor and $type !== awLegend::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 === awLegend::MARK or $type === awLegend::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 awLegend::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 $component->getLegendBackground(), 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 private function drawBase(awDrawer $drawer, awPoint $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 interface awLegendable { 689 690 /** 691 * Get the line type 692 * 693 * @return int 694 */ 695 public function getLegendLineStyle(); 696 697 /** 698 * Get the line thickness 699 * 700 * @return int 701 */ 702 public function getLegendLineThickness(); 703 704 /** 705 * Get the color of line 706 * 707 * @return Color 708 */ 709 public function getLegendLineColor(); 710 711 /** 712 * Get the background color or gradient of an element of the component 713 * 714 * @return Color, Gradient 715 */ 716 public function getLegendBackground(); 717 718 /** 719 * Get a Mark object 720 * 721 * @return Mark 722 */ 723 public function getLegendMark(); 724 725 } 726 727 registerInterface('Legendable'); 728 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 14:10:01 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |