[ 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/ -> Pie.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  require_once dirname(__FILE__)."/Component.class.php";
  11   
  12  /* <php4> */
  13  
  14  define("PIE_DARK", 1);
  15  define("PIE_COLORED", 2);
  16  define("PIE_AQUA", 3);
  17  define("PIE_EARTH", 4);
  18  
  19  /* </php4> */
  20   
  21  /**
  22   * Pie
  23   *
  24   * @package Artichow
  25   */
  26  class awPie extends awComponent {
  27  
  28      /**
  29       * A dark theme for pies
  30       *
  31       *
  32       * @var int
  33       */
  34      
  35  
  36      /**
  37       * A colored theme for pies
  38       *
  39       * @var int
  40       */
  41      
  42  
  43      /**
  44       * A water theme for pies
  45       *
  46       * @var int
  47       */
  48      
  49  
  50      /**
  51       * A earth theme for pies
  52       *
  53       * @var int
  54       */
  55      
  56      
  57      /**
  58       * Pie values
  59       *
  60       * @var array
  61       */
  62      var $values;
  63      
  64      /**
  65       * Pie colors
  66       *
  67       * @var array
  68       */
  69      var $colors;
  70      
  71      /**
  72       * Pie legend
  73       * 
  74       * @var array
  75       */
  76      var $legendValues = array();
  77      
  78      /**
  79       * Intensity of the 3D effect
  80       *
  81       * @var int
  82       */
  83      var $size;
  84      
  85      /**
  86       * Border color
  87       *
  88       * @var Color
  89       */
  90      var $border;
  91      
  92      /**
  93       * Pie explode
  94       *
  95       * @var array
  96       */
  97      var $explode = array();
  98      
  99      /**
 100       * Initial angle
 101       *
 102       * @var int
 103       */
 104      var $angle = 0;
 105      
 106      /**
 107       * Labels precision
 108       *
 109       * @var int
 110       */
 111      var $precision;
 112      
 113      /**
 114       * Labels number
 115       *
 116       * @var int
 117       */
 118      var $number;
 119      
 120      /**
 121       * Labels minimum
 122       *
 123       * @var int
 124       */
 125      var $minimum;
 126      
 127      /**
 128       * Labels position
 129       *
 130       * @var int
 131       */
 132      var $position = 15;
 133      
 134      /**
 135       * Labels of your pie
 136       *
 137       * @var Label
 138       */
 139      var $label;
 140      
 141      /**
 142       * Build the plot
 143       *
 144       * @param array $values Pie values
 145       */
 146  	 function awPie($values, $colors = PIE_COLORED) {
 147          
 148          $this->setValues($values);
 149          
 150          if(is_array($colors)) {
 151              $this->colors = $colors;
 152          } else {
 153          
 154              switch($colors) {
 155              
 156                  case PIE_AQUA :
 157                      $this->colors = array(
 158                          new awColor(131, 220, 215),
 159                          new awColor(131, 190, 215),
 160                          new awColor(131, 160, 215),
 161                          new awColor(160, 140, 215),
 162                          new awColor(190, 131, 215),
 163                          new awColor(220, 131, 215)
 164                      );
 165                      break;
 166              
 167                  case PIE_EARTH :
 168                      $this->colors = array(
 169                          new awColor(97, 179, 110),
 170                          new awColor(130, 179, 97),
 171                          new awColor(168, 179, 97),
 172                          new awColor(179, 147, 97),
 173                          new awColor(179, 108, 97),
 174                          new awColor(99, 107, 189),
 175                          new awColor(99, 165, 189)
 176                      );
 177                      break;
 178              
 179                  case PIE_DARK :
 180                      $this->colors = array(
 181                          new awColor(140, 100, 170),
 182                          new awColor(130, 170, 100),
 183                          new awColor(160, 160, 120),
 184                          new awColor(150, 110, 140),
 185                          new awColor(130, 150, 160),
 186                          new awColor(90, 170, 140)
 187                      );
 188                      break;
 189                      
 190                  default :
 191                      $this->colors = array(
 192                          new awColor(187, 213, 151),
 193                          new awColor(223, 177, 151),
 194                          new awColor(111, 186, 132),
 195                          new awColor(197, 160, 230),
 196                          new awColor(165, 169, 63),
 197                          new awColor(218, 177, 89),
 198                          new awColor(116, 205, 121),
 199                          new awColor(200, 201, 78),
 200                          new awColor(127, 205, 177),
 201                          new awColor(205, 160, 160),
 202                          new awColor(190, 190, 190)
 203                      );
 204                      break;
 205              
 206              }
 207          
 208          }
 209      
 210          parent::awComponent();
 211          
 212          $this->label = new awLabel;
 213          $this->label->setCallbackFunction('callbackPerCent');
 214          
 215      }
 216      
 217      /**
 218       * Change legend values
 219       *
 220       * @param array $legend An array of values for each part of the pie
 221       */
 222  	 function setLegend($legend) {
 223      
 224          $this->legendValues = (array)$legend;
 225      
 226      }
 227      
 228      /**
 229       * Set a border all around the pie
 230       *
 231       * @param $color A color for the border
 232       */
 233  	 function setBorder($color) {
 234          $this->border = $color;
 235      }
 236      
 237      /**
 238       * Change 3D effect intensity
 239       *
 240       * @param int $size Effect size
 241       */
 242  	 function set3D($size) {
 243          $this->size = (int)$size;
 244      }
 245      
 246      /**
 247       * Change initial angle
 248       *
 249       * @param int $angle New angle in degrees
 250       */
 251  	 function setStartAngle($angle) {
 252          $this->angle = (int)$angle;
 253      }
 254      
 255      /**
 256       * Change label precision
 257       *
 258       * @param int $precision New precision
 259       */
 260  	 function setLabelPrecision($precision) {
 261          $this->precision = (int)$precision;
 262      }
 263      
 264      /**
 265       * Change label position
 266       *
 267       * @param int $position New position in pixels
 268       */
 269  	 function setLabelPosition($position) {
 270          $this->position = (int)$position;
 271      }
 272      
 273      /**
 274       * Change label number
 275       *
 276       * @param int $number New number
 277       */
 278  	 function setLabelNumber($number) {
 279          $this->number = is_null($number) ? $number : (int)$number;
 280      }
 281      
 282      /**
 283       * Change label minimum
 284       *
 285       * @param int $minimum New minimum
 286       */
 287  	 function setLabelMinimum($minimum) {
 288          $this->minimum = is_null($minimum) ? $minimum : (int)$minimum;
 289      }
 290      
 291      /**
 292       * Change Pie explode
 293       *
 294       * @param array $explode
 295       */
 296  	 function explode($explode) {
 297          $this->explode = (array)$explode;
 298      }
 299      
 300  	 function drawEnvelope($drawer) {
 301      
 302      }
 303      
 304  	 function drawComponent($drawer, $x1, $y1, $x2, $y2, $aliasing) {
 305          
 306          $count = count($this->values);
 307          $sum = array_sum($this->values);
 308          
 309          $width = $x2 - $x1;
 310          $height = $y2 - $y1;
 311          
 312          if($aliasing) {
 313              $x = $width / 2;
 314              $y = $height / 2;
 315          } else {
 316              $x = $width / 2 + $x1;
 317              $y = $height / 2 + $y1;
 318          }
 319          
 320          $position = $this->angle;
 321          $values = array();
 322          $parts = array();
 323          $angles = 0;
 324          
 325          if($aliasing) {
 326              $side = new awSide(0, 0, 0, 0);
 327          }
 328          
 329          foreach($this->values as $key => $value) {
 330          
 331              $angle = ($value / $sum * 360);
 332              
 333              if($key === $count - 1) {
 334                  $angle = 360 - $angles;
 335              }
 336              
 337              $angles += $angle;
 338              
 339              if(array_key_exists($key, $this->explode)) {
 340                  $middle = 360 - ($position + $angle / 2);
 341                  $posX = $this->explode[$key] * cos($middle * M_PI / 180);
 342                  $posY = $this->explode[$key] * sin($middle * M_PI / 180) * -1;
 343                  
 344                  if($aliasing) {
 345                      $explode = new awPoint(
 346                          $posX * 2,
 347                          $posY * 2
 348                      );
 349                      $side->set(
 350                          max($side->left, $posX * -2),
 351                          max($side->right, $posX * 2),
 352                          max($side->top, $posY * -2),
 353                          max($side->bottom, $posY * 2)
 354                      );
 355                  } else {
 356                      $explode = new awPoint(
 357                          $posX,
 358                          $posY
 359                      );
 360                  }
 361                  
 362              } else {
 363                  $explode = new awPoint(0, 0);
 364              }
 365              
 366              $values[$key] = array(
 367                  $position, ($position + $angle), $explode
 368              );
 369              
 370              $color = $this->colors[$key % count($this->colors)];
 371              $parts[$key] = new awPiePart($color);
 372              
 373              // Add part to the legend
 374              $legend = array_key_exists($key, $this->legendValues) ? $this->legendValues[$key] : $key;
 375              $this->legend->add($parts[$key], $legend, LEGEND_BACKGROUND);
 376              
 377              $position += $angle;
 378          
 379          }
 380          
 381          if($aliasing) {
 382          
 383              $mainDrawer = $drawer;
 384              
 385              $x *= 2;
 386              $y *= 2;
 387              $width *= 2;
 388              $height *= 2;
 389              $this->size *= 2;
 390              
 391              $image = new awImage;
 392              $image->border->hide();
 393              $image->setSize(
 394                  $width + $side->left + $side->right,
 395                  $height + $side->top + $side->bottom + $this->size + 1 /* bugs.php.net ! */
 396              );
 397              
 398              $drawer = $image->getDrawer(
 399                  $width / $image->width,
 400                  $height / $image->height,
 401                  ($width / 2 + $side->left) / $image->width,
 402                  ($height / 2 + $side->top) / $image->height
 403              );
 404              
 405          }
 406          
 407          // Draw 3D effect
 408          for($i = $this->size; $i > 0; $i--) {
 409          
 410              foreach($values as $key => $value) {
 411              
 412                  $color = $this->colors[$key % count($this->colors)];
 413                  $color->brightness(-50);
 414                  
 415                  list($from, $to, $explode) = $value;
 416                  
 417                  $drawer->filledArc($color, $explode->move($x, $y + $i), $width, $height, $from, $to);
 418                  
 419                  $color->free();
 420                  unset($color);
 421                  
 422                  if(is_a($this->border, 'awColor')) {
 423                  
 424                      $point = $explode->move($x, $y);
 425                      
 426                      if($i === $this->size) {
 427                  
 428                          $drawer->arc($this->border, $point->move(0, $this->size), $width, $height, $from, $to);
 429                          
 430                      }
 431                  
 432                  }
 433              
 434              }
 435              
 436          }
 437          
 438          foreach($values as $key => $value) {
 439              
 440              $color = $this->colors[$key % count($this->colors)];
 441              
 442              list($from, $to, $explode) = $value;
 443              
 444              $drawer->filledArc($color, $explode->move($x, $y), $width, $height, $from, $to);
 445              
 446              if(is_a($this->border, 'awColor')) {
 447              
 448                  $point = $explode->move($x, $y);
 449                  $drawer->arc($this->border, $point, $width, $height, $from, $to);
 450              }
 451          
 452          }
 453          
 454          if($aliasing) {
 455          
 456              $x = $x / 2 + $x1;
 457              $y = $y / 2 + $y1;
 458              $width /= 2;
 459              $height /= 2;
 460              $this->size /= 2;
 461              
 462              foreach($values as $key => $value) {
 463                  $old = $values[$key][2];
 464                  $values[$key][2] = new awPoint(
 465                      $old->x / 2, $old->y / 2
 466                  );
 467              }
 468              
 469              $mainDrawer->copyResizeImage(
 470                  $image,
 471                  new awPoint($x1 - $side->left / 2, $y1 - $side->top / 2),
 472                  new awPoint($x1 - $side->left / 2 + $image->width / 2, $y1 - $side->top / 2 + $image->height/ 2),
 473                  new awPoint(0, 0),
 474                  new awPoint($image->width, $image->height),
 475                  TRUE
 476              );
 477              
 478              $drawer = $mainDrawer;
 479          
 480          }
 481          
 482          // Get labels values
 483          $pc = array();
 484          foreach($this->values as $key => $value) {
 485              $pc[$key] = round($value / $sum * 100, $this->precision);
 486          }
 487          if($this->label->count() === 0) { // Check that there is no user defined values
 488              $this->label->set($pc);
 489          }
 490          
 491          $position = 0;
 492          
 493          foreach($pc as $key => $value) {
 494          
 495              // Limit number of labels to display
 496              if($position === $this->number) {
 497                  break;
 498              }
 499              
 500              if(is_null($this->minimum) === FALSE and $value < $this->minimum) {
 501                  continue;
 502              }
 503              
 504              $position++;
 505              
 506              list($from, $to, $explode) = $values[$key];
 507              
 508              $angle = $from + ($to - $from) / 2;
 509              $angleRad = (360 - $angle) * M_PI / 180;
 510              
 511              $point = new awPoint(
 512                  $x + $explode->x + cos($angleRad) * ($width / 2 + $this->position),
 513                  $y + $explode->y - sin($angleRad) * ($height / 2 + $this->position)
 514              );
 515              
 516              $angle %= 360;
 517              
 518              // We don't display labels on the 3D effect
 519              if($angle > 0 and $angle < 180) {
 520                  $point = $point->move(0, -1 * sin($angleRad) * $this->size);
 521              }
 522              
 523              if($angle >= 45 and $angle < 135) {
 524                  $this->label->setAlign(LABEL_CENTER, LABEL_BOTTOM);
 525              } else if($angle >= 135 and $angle < 225) {
 526                  $this->label->setAlign(LABEL_RIGHT, LABEL_MIDDLE);
 527              } else if($angle >= 225 and $angle < 315) {
 528                  $this->label->setAlign(LABEL_CENTER, LABEL_TOP);
 529              } else {
 530                  $this->label->setAlign(LABEL_LEFT, LABEL_MIDDLE);
 531              }
 532              
 533              $this->label->draw(
 534                  $drawer,
 535                  $point,
 536                  $key
 537              );
 538              
 539          }
 540          
 541      }
 542      
 543      /**
 544       * Return margins around the component
 545       *
 546       * @return array Left, right, top and bottom margins
 547       */
 548  	 function getMargin() {
 549          
 550          // Get axis informations
 551          
 552          $leftAxis = $this->padding->left;
 553          $rightAxis = $this->padding->right;
 554          $topAxis = $this->padding->top;
 555          $bottomAxis = $this->padding->bottom;
 556          
 557          return array($leftAxis, $rightAxis, $topAxis, $bottomAxis);
 558          
 559      }
 560      
 561      
 562      /**
 563       * Change values of Y axis
 564       * This method ignores not numeric values
 565       *
 566       * @param array $values
 567       */
 568  	 function setValues($values) {
 569      
 570          $this->checkArray($values);
 571          $this->values = $values;
 572          
 573      }
 574      
 575      
 576      /**
 577       * Return values of Y axis
 578       *
 579       * @return array
 580       */
 581  	 function getValues() {
 582          return $this->values;
 583      }
 584      
 585  	 function checkArray(&$array) {
 586      
 587          if(is_array($array) === FALSE) {
 588              trigger_error("You tried to set values that are not an array");
 589          }
 590          
 591          foreach($array as $key => $value) {
 592              if(is_numeric($value) === FALSE) {
 593                  unset($array[$key]);
 594              }
 595          }
 596          
 597          if(count($array) < 1) {
 598              trigger_error("Your graph must have at least 1 value");
 599          }
 600      
 601      }
 602  
 603  }
 604  
 605  registerClass('Pie');
 606  
 607  /**
 608   * Pie
 609   *
 610   * @package Artichow
 611   */
 612  class awPiePart {
 613  
 614      /**
 615       * Pie part color
 616       *
 617       * @var Color
 618       */
 619      var $color;
 620  
 621      /**
 622       * Build a new awPiePart
 623       *
 624       * @param $color Pie part color
 625       */
 626  	 function awPiePart($color) {
 627      
 628          $this->color = $color;
 629      
 630      }
 631  
 632      /**
 633       * Get the background color or gradient of an element of the component
 634       *
 635       * @return Color, Gradient
 636       */
 637  	 function getLegendBackground() {
 638          return $this->color;
 639      }
 640  
 641      /**
 642       * Get the line thickness
 643       *
 644       * @return NULL
 645       */
 646  	 function getLegendLineThickness() {
 647      }
 648  
 649      /**
 650       * Get the line type
 651       *
 652       * @return NULL
 653       */
 654  	 function getLegendLineStyle() {
 655      }
 656  
 657      /**
 658       * Get the color of line
 659       *
 660       * @return NULL
 661       */
 662  	 function getLegendLineColor() {
 663      }
 664  
 665      /**
 666       * Get a mark object
 667       *
 668       * @return NULL
 669       */
 670  	 function getLegendMark() {
 671      }
 672  
 673  }
 674  
 675  registerClass('PiePart');
 676  
 677  function callbackPerCent($value) {
 678      return $value.'%';
 679  }
 680  ?>


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