[ Index ]
 

Code source de Cr@wltr@ck 2.2.1

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/graphs/artichow/php4/ -> Graph.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__)."/Image.class.php";
  11   
  12  /* <php4> */
  13  
  14  define("GRAPH_DRAW_RETURN", 1);
  15  define("GRAPH_DRAW_DISPLAY", 2);
  16  
  17  /* </php4> */
  18  
  19  /**
  20   * A graph 
  21   *
  22   * @package Artichow
  23   */
  24  class awGraph extends awImage {
  25  
  26      /**
  27       * Graph name
  28       *
  29       * @var string
  30       */
  31      var $name;
  32  
  33      /**
  34       * Cache timeout
  35       *
  36       * @var int
  37       */
  38      var $timeout = 0;
  39      
  40      /**
  41       * Graph timing ?
  42       *
  43       * @var bool
  44       */
  45      var $timing;
  46      
  47      /**
  48       * Components
  49       *
  50       * @var array
  51       */
  52      var $components = array();
  53      
  54      /**
  55       * Some labels to add to the component
  56       *
  57       * @var array
  58       */
  59      var $labels = array();
  60      
  61      /**
  62       * Graph title
  63       *
  64       * @var Label
  65       */
  66      var $title;
  67      
  68      /**
  69       * File cache location
  70       *
  71       * @var string
  72       */
  73      var $fileCache;
  74      
  75      /**
  76       * Time file cache location
  77       *
  78       * @var string
  79       */
  80      var $fileCacheTime;
  81      
  82      /** 
  83       * Drawing mode to return the graph
  84       *
  85       * @var int
  86       */
  87      
  88      
  89      /** 
  90       * Drawing mode to display the graph
  91       *
  92       * @var int
  93       */
  94      
  95      
  96      /**
  97       * Construct a new graph
  98       *
  99       * @param int $width Graph width
 100       * @param int $height Graph height
 101       * @param string $name Graph name for the cache (must be unique). Let it null to not use the cache.
 102       * @param int $timeout Cache timeout (unix timestamp)
 103       */
 104  	 function awGraph($width = NULL, $height = NULL, $name = NULL, $timeout = 0) {
 105          
 106          parent::awImage();
 107      
 108          $this->setSize($width, $height);
 109  
 110          if(ARTICHOW_CACHE) {
 111      
 112              $this->name = $name;
 113              $this->timeout = $timeout;
 114              
 115              // Clean sometimes all the cache
 116              if(mt_rand(0, 5000) ===  0) {
 117                  awGraph::cleanCache();
 118              }
 119          
 120              // Take the graph from the cache if possible
 121              if($this->name !== NULL) {
 122              
 123                  $this->fileCache = ARTICHOW_CACHE_DIRECTORY."/".$this->name;
 124                  $this->fileCacheTime = $this->fileCache."-time";
 125                  
 126                  if(is_file($this->fileCache)) {
 127                  
 128                      $type = awGraph::cleanGraphCache($this->fileCacheTime);
 129                      
 130                      if($type === NULL) {
 131                          awGraph::deleteFromCache($this->name);
 132                      } else {
 133                          header("Content-Type: image/".$type);
 134                          echo file_get_contents($this->fileCache);
 135                          exit;
 136                      }
 137                      
 138                  }
 139              
 140              }
 141          
 142          }
 143          
 144          $this->title = new awLabel(
 145              NULL,
 146              new awTuffy(16),
 147              NULL,
 148              0
 149          );
 150          $this->title->setAlign(LABEL_CENTER, LABEL_BOTTOM);
 151      
 152      }
 153      
 154      /**
 155       * Delete a graph from the cache
 156       *
 157       * @param string $name Graph name
 158       * @return bool TRUE on success, FALSE on failure
 159       */
 160  	  function deleteFromCache($name) {
 161  
 162          if(ARTICHOW_CACHE) {
 163          
 164              if(is_file(ARTICHOW_CACHE_DIRECTORY."/".$name."-time")) {
 165                  unlink(ARTICHOW_CACHE_DIRECTORY."/".$name."");
 166                  unlink(ARTICHOW_CACHE_DIRECTORY."/".$name."-time");
 167              }
 168              
 169          }
 170          
 171      }
 172      
 173      /**
 174       * Delete all graphs from the cache
 175       */
 176  	  function deleteAllCache() {
 177  
 178          if(ARTICHOW_CACHE) {
 179      
 180              $dp = opendir(ARTICHOW_CACHE_DIRECTORY);
 181              
 182              while($file = readdir($dp)) {
 183                  if($file !== '.' and $file != '..') {
 184                      unlink(ARTICHOW_CACHE_DIRECTORY."/".$file);
 185                  }
 186              }
 187              
 188          }
 189      
 190      }
 191      
 192      /**
 193       * Clean cache
 194       */
 195  	  function cleanCache() {
 196  
 197          if(ARTICHOW_CACHE) {
 198      
 199              $glob = glob(ARTICHOW_CACHE_DIRECTORY."/*-time");
 200              
 201              foreach($glob as $file) {
 202                  
 203                  $type = awGraph::cleanGraphCache($file);
 204                  
 205                  if($type === NULL) {
 206                      $name = ereg_replace(".*/(.*)\-time", "\\1", $file);
 207                      awGraph::deleteFromCache($name);
 208                  }
 209              
 210              }
 211              
 212          }
 213          
 214      }
 215      
 216      /**
 217       * Enable/Disable Graph timing
 218       *
 219       * @param bool $timing
 220       */
 221  	 function setTiming($timing) {
 222          $this->timing = (bool)$timing;
 223      }
 224       
 225      /**
 226       * Add a component to the graph
 227       *
 228       * @param &$component
 229       */
 230  	 function add(&$component) {
 231      
 232          $this->components[] = $component;
 233      
 234      }
 235      
 236      /**
 237       * Add a label to the component
 238       *
 239       * @param &$label
 240       * @param int $x Position on X axis of the center of the text
 241       * @param int $y Position on Y axis of the center of the text
 242       */
 243  	 function addLabel(&$label, $x, $y) {
 244      
 245          $this->labels[] = array(
 246              $label, $x, $y
 247          );
 248          
 249      }
 250      
 251      /**
 252       * Add a label to the component with aboslute position
 253       *
 254       * @param &$label
 255       * @param $point Text position
 256       */
 257  	 function addAbsLabel(&$label, $point) {
 258      
 259          $this->labels[] = array(
 260              $label, $point
 261          );
 262          
 263      }
 264      
 265      /**
 266       * Build the graph and draw component on it
 267       *
 268       * @param string $mode Display mode (can be a file name)
 269       */
 270  	 function draw($mode = GRAPH_DRAW_DISPLAY) {
 271          
 272          if($this->timing) {
 273              $time = microtimeFloat();
 274          }
 275      
 276          $this->create();
 277          
 278          foreach($this->components as $component) {
 279          
 280              $this->drawComponent($component);
 281          
 282          }
 283          
 284          $this->drawTitle();
 285          $this->drawShadow();
 286          $this->drawLabels();
 287          
 288          if($this->timing) {
 289              $this->drawTiming(microtimeFloat() - $time);
 290          }
 291          
 292          // Create graph
 293          $data = $this->send(TRUE);
 294          
 295          // Put the graph in the cache if needed
 296          if(ARTICHOW_CACHE and $this->name !== NULL) {
 297              
 298              if(is_writable(ARTICHOW_CACHE_DIRECTORY) === FALSE) {
 299                  awImage::drawError("Class Graph: Cache directory is not writable.");
 300              }
 301          
 302              file_put_contents($this->fileCache, $data);
 303              file_put_contents($this->fileCacheTime, $this->timeout."\n".$this->getFormat());
 304              
 305          }
 306          
 307          switch($mode) {
 308          
 309              case GRAPH_DRAW_DISPLAY :
 310                  echo $data;
 311                  break;
 312          
 313              case GRAPH_DRAW_RETURN :
 314                  return $data;
 315              
 316              default :
 317                  if(is_string($mode)) {
 318                      file_put_contents($mode, $data);
 319                  } else {
 320                      awImage::drawError("Class Graph: Unable to draw the graph.");
 321                  }
 322          
 323          }
 324          
 325      }
 326      
 327  	 function drawLabels() {
 328      
 329          $drawer = $this->getDrawer();
 330      
 331          foreach($this->labels as $array) {
 332          
 333              if(count($array) === 3) {
 334              
 335                  // Text in relative position
 336                  list($label, $x, $y) = $array;
 337                  
 338                  $point = new awPoint(
 339                      $x * $this->width,
 340                      $y * $this->height
 341                  );
 342                  
 343              } else {
 344              
 345                  // Text in absolute position
 346                  list($label, $point) = $array;
 347              
 348              }
 349                  
 350              $label->draw($drawer, $point);
 351          
 352          }
 353          
 354      }
 355          
 356  	 function drawTitle() {
 357      
 358          $drawer = $this->getDrawer();
 359      
 360          $point = new awPoint(
 361              $this->width / 2,
 362              10
 363          );
 364          
 365          $this->title->draw($drawer, $point);
 366      
 367      }
 368      
 369  	 function drawTiming($time) {
 370      
 371          $drawer = $this->getDrawer();
 372          
 373          $label = new awLabel;
 374          $label->set("(".sprintf("%.3f", $time)." s)");
 375          $label->setAlign(LABEL_LEFT, LABEL_TOP);
 376          $label->border->show();
 377          $label->setPadding(1, 0, 0, 0);
 378          $label->setBackgroundColor(new awColor(230, 230, 230, 25));
 379          
 380          $label->draw($drawer, new awPoint(5, $drawer->height - 5));
 381      
 382      }
 383      
 384  	  function cleanGraphCache($file) {
 385      
 386          list(
 387              $time,
 388              $type
 389          ) = explode("\n", file_get_contents($file));
 390          
 391          $time = (int)$time;
 392          
 393          if($time !== 0 and $time < time()) {
 394              return NULL;
 395          } else {
 396              return $type;
 397          }
 398          
 399          
 400      }
 401  
 402  }
 403  
 404  registerClass('Graph');
 405  
 406  /*
 407   * To preserve PHP 4 compatibility
 408   */
 409  function microtimeFloat() { 
 410      list($usec, $sec) = explode(" ", microtime()); 
 411      return (float)$usec + (float)$sec; 
 412  }
 413  ?>


Généré le : Thu Sep 6 14:14:11 2007 par Balluche grâce à PHPXref 0.7