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


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