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