linea21-externals
[ class tree: linea21-externals ] [ index: linea21-externals ] [ all elements ]

Source for file Graph.class.php

Documentation is available at Graph.class.php

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

Documentation generated on Fri, 16 Oct 2009 09:33:17 +0200 by phpDocumentor 1.4.1