includes/graphs.php
changeset 262 a82133ce2d76
parent 261 5f1cd51bf1be
equal deleted inserted replaced
261:5f1cd51bf1be 262:a82133ce2d76
     1 <?php
     1 <?php
     2 
     2 
     3 /*
     3 /*
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
     5  * Version 1.0.3 (Dyrad)
     5  * Version 1.1.1
     6  * Copyright (C) 2006-2007 Dan Fuhry
     6  * Copyright (C) 2006-2007 Dan Fuhry
     7  *
     7  *
     8  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
     8  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
     9  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
     9  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
    10  *
    10  *
   738 }
   738 }
   739 
   739 
   740 // Graph Generator for PHP
   740 // Graph Generator for PHP
   741 // Originally located at http://szewo.com/php/graph, but link was broken, so this file was retrieved from:
   741 // Originally located at http://szewo.com/php/graph, but link was broken, so this file was retrieved from:
   742 // http://web.archive.org/web/20030130065944/szewo.com/php/graph/graph.class.php3.txt
   742 // http://web.archive.org/web/20030130065944/szewo.com/php/graph/graph.class.php3.txt
   743 // License unknown
   743 // License unknown, however sources on the web have shown this to be either GPL or public domain.
   744 
   744 
   745 class GraphMaker_compat {
   745 class GraphMaker_compat {
   746  var $_values;
   746   var $_values;
   747  var $_ShowLabels;
   747   var $_ShowLabels;
   748  var $_ShowCounts;
   748   var $_ShowCounts;
   749  var $_ShowCountsMode;
   749   var $_ShowCountsMode;
   750 
   750 
   751  var $_BarWidth;
   751   var $_BarWidth;
   752  var $_GraphWidth;
   752   var $_GraphWidth;
   753  var $_BarImg;
   753   var $_BarImg;
   754  var $_BarBorderWidth;
   754   var $_BarBorderWidth;
   755  var $_BarBorderColor;
   755   var $_BarBorderColor;
   756  var $_RowSortMode;
   756   var $_BarBackgroundColor;
   757  var $_TDClassHead;
   757   var $_RowSortMode;
   758  var $_TDClassLabel;
   758   var $_TDClassHead;
   759  var $_TDClassCount;
   759   var $_TDClassLabel;
   760  var $_GraphTitle;
   760   var $_TDClassCount;
   761 
   761   var $_GraphTitle;
   762  function __construct() {
   762 
   763   $this->_values = array();
   763   function __construct() {
   764   $this->_ShowLabels = true;
   764     $this->_values = array();
   765   $this->_BarWidth = 16;
   765     $this->_ShowLabels = true;
   766   $this->_GraphWidth = 360;
   766     $this->_BarWidth = 32;
   767   $this->_BarImg = "NULL";
   767     $this->_GraphWidth = 360;
   768   $this->_BarBorderWidth = 0;
   768     $this->_BarImg = scriptPath . "/images/graphbit.png";
   769   $this->_BarBorderColor = "red";
   769     $this->_BarBorderWidth = 0;
   770   $this->_ShowCountsMode = 2;
   770     $this->_BarBorderColor = "red";
   771   $this->_RowSortMode = 1;
   771     $this->_ShowCountsMode = 2;
   772   $this->_TDClassHead = "grphh";
   772     $this->_RowSortMode = 1;
   773   $this->_TDClassLabel = "grph";
   773     $this->_TDClassHead = "graph-title";
   774   $this->_TDClassCount = "grphc";
   774     $this->_TDClassLabel = "graph-label";
   775   $this->_GraphTitle="Graph title";
   775     $this->_TDClassCount = "graph-count";
   776  }
   776     $this->_GraphTitle="Graph title";
   777  
   777     $this->_BarBackgroundColor = "#456798";
   778  function GraphMaker_compat() {
   778   }
   779   $this->__construct();
   779 
   780  }
   780   function GraphMaker_compat() {
   781 
   781     $this->__construct();
   782  function SetBarBorderWidth($width) {
   782   }
   783   $this->_BarBorderWidth = $width;
   783 
   784  }
   784   function SetBarBorderWidth($width) {
   785  function SetBorderColor($color) {
   785     $this->_BarBorderWidth = $width;
   786   $this->_BarBorderColor = $color;
   786   }
   787  }
   787   function SetBorderColor($color) {
       
   788     $this->_BarBorderColor = $color;
       
   789   }
       
   790   
       
   791   function SetBarBackgroundColor($color)
       
   792   {
       
   793     $this->_BarBackgroundColor = $color;
       
   794   }
   788 
   795 
   789 //  mode = 1 labels asc, 2 label desc
   796 //  mode = 1 labels asc, 2 label desc
   790  function SetSortMode($mode) {
   797   function SetSortMode($mode) {
   791   switch ($mode) {
   798     switch ($mode) {
   792    case 1:
   799       case 1:
   793     asort($this->_values);
   800         asort($this->_values);
   794     break;
   801         break;
   795    case 2:
   802       case 2:
   796     arsort($this->_values);
   803         arsort($this->_values);
   797     break;
   804         break;
   798    default:
   805       default:
   799     break;
   806         break;
   800    }
   807       }
   801 
   808 
   802  }
   809   }
   803 
   810 
   804  function AddValue($labelName, $theValue) {
   811   function AddValue($labelName, $theValue) {
   805   array_push($this->_values, array("label" => $labelName, "value" => $theValue));
   812     array_push($this->_values, array("label" => $labelName, "value" => $theValue));
   806 
   813   }
   807  }
   814 
   808  function SetBarWidth($width) {
   815   function SetBarData($data)
   809   $this->_BarWidth = $width;
   816   {
   810  }
   817       foreach ( $data as $name => $value )
   811  function SetBarImg($img) {
   818       {
   812   $this->_BarImg = $img;
   819           $this->AddValue($name, $value);
   813  }
   820       }
   814  function SetShowLabels($lables) {
   821   }
   815   $this->_ShowLabels = $labels;
   822   function DrawGraph()
   816  }
   823   {
   817  function SetGraphWidth($width) {
   824       $this->BarGraphVert();
   818   $this->_GraphWidth = $width;
   825   }
   819  }
   826   function SetBarWidth($width)
   820  function SetGraphTitle($title) {
   827   {
   821   $this->_GraphTitle = $title;
   828     $this->_BarWidth = $width;
   822  }
   829   }
   823  //mode = percentage or counts
   830   function SetBarImg($img)
   824  function SetShowCountsMode($mode) {
   831   {
   825   $this->_ShowCountsMode = $mode;
   832     $this->_BarImg = $img;
   826  }
   833   }
   827  //mode = none(0) label(1) or count(2)
   834   function SetShowLabels($lables)
   828  function SetRowSortMode($sortmode) {
   835   {
   829   $this->_RowSortMode = $sortmode;
   836     $this->_ShowLabels = $labels;
   830  }
   837   }
   831 
   838   function SetGraphWidth($width)
   832  function SetTDClassHead($class) {
   839   {
   833   $this->_TDClassHead = $class;
   840     $this->_GraphWidth = $width;
   834  }
   841   }
   835  function SetTDClassLabel($class) {
   842   function SetGraphTitle($title)
   836   $this->_TDClassLabel = $class;
   843   {
   837  }
   844     $this->_GraphTitle = $title;
   838  function SetTDClassCount($class) {
   845   }
   839   $this->_TDClassCount = $class;
   846   //mode = percentage or counts
   840  }
   847   function SetShowCountsMode($mode)
   841  function GetMaxVal() {
   848   {
   842   $maxval = 0;
   849     $this->_ShowCountsMode = $mode;
   843   foreach($this->_values as $value) if($maxval<$value["value"]) $maxval = $value["value"];
   850   }
   844   return $maxval;
   851   //mode = none(0) label(1) or count(2)
   845  }
   852   function SetRowSortMode($sortmode)
   846  function BarGraphVert() {
   853   {
   847   $maxval = $this->GetMaxVal();
   854     $this->_RowSortMode = $sortmode;
   848   foreach($this->_values as $value) $sumval += $value["value"];
   855   }
   849   $this->SetSortMode($this->_RowSortMode);
   856 
   850   echo "<table>";
   857   function SetTDClassHead($class)
   851   if (strlen($this->_GraphTitle)>0)  echo "<tr><td colspan=".count($this->_values)." class=\"".$this->_TDClassHead."\">".$this->_GraphTitle."</td></tr>";
   858   {
   852    echo "<tr>";
   859     $this->_TDClassHead = $class;
   853    foreach($this->_values as $value) {
   860   }
   854     echo "<td valign=bottom align=center>";
   861   function SetTDClassLabel($class)
   855     $height = $this->_BarWidth;
   862   {
   856     $width=ceil($value["value"]*$this->_GraphWidth/$maxval);
   863     $this->_TDClassLabel = $class;
   857     echo "<div ";
   864   }
   858     echo "  style=\"background-color: #666666; border: ".$this->_BarBorderWidth."px solid ".$this->_BarBorderColor."\"";
   865   function SetTDClassCount($class)
   859     echo ">";
   866   {
   860     echo "</td>";
   867     $this->_TDClassCount = $class;
   861    }
   868   }
   862    echo "</tr>";
   869   function GetMaxVal()
   863    if ($this->_ShowCountsMode>0) {
   870   {
   864    	echo "<tr>";
   871     $maxval = 0;
   865     foreach($this->_values as $value) {
   872     foreach ( $this->_values as $value )
   866      switch ($this->_ShowCountsMode) {
   873     {
   867      case 1:
   874       if ( $maxval < $value["value"] )
   868       $count = round(100*$value["value"]/$sumval)."%";
   875       {
   869       break;
   876         $maxval = $value["value"];
   870      case 2:
   877       }
   871       $count = $value["value"];
   878     }
   872       break;  /* Exit the switch and the while. */
   879     return $maxval;
   873      default:
   880   }
   874       break;
   881   function BarGraphVert()
   875      }
   882   {
   876      echo "<td align=center class=".$this->_TDClassCount.">$count</td>";
   883     $maxval = $this->GetMaxVal();
   877    	}
   884     foreach($this->_values as $value)
   878 	echo "</tr>";
   885     {
   879    }
   886       $sumval += $value["value"];
   880 
   887     }
   881    if ($this->_ShowLabels) {
   888     
   882     echo "<tr>";
   889     $this->SetSortMode($this->_RowSortMode);
   883     foreach($this->_values as $value) {
   890     
   884      echo "<td align=center class=".$this->_TDClassLabel;
   891     echo "\n<!-- ----------------------------------------- -->\n<div class=\"tblholder\" style=\"width: 100%; clip: rect(0px,auto,auto,0px); overflow: auto;\">\n<table border=\"0\" cellspacing=\"1\" cellpadding=\"4\">\n  ";
   885 	 echo ">".$value["label"]."</td>";
   892     
   886 	}
   893     if ( strlen($this->_GraphTitle) > 0 )
   887 	echo "</tr>";
   894     {
   888    }	
   895       echo "<tr>\n    <th colspan=\"".count($this->_values)."\" class=\"".$this->_TDClassHead."\">".$this->_GraphTitle."</th>\n  </tr>\n  ";
   889 
   896     }
   890   echo "</table>";
   897     
   891  }
   898     echo "<tr>\n  ";
   892 
   899     $css_class = 'row1';
   893 
   900     
   894 
   901     foreach($this->_values as $value)
   895  function BarGraphHoriz() {
   902     {
   896   $maxval = $this->GetMaxVal();
   903       $css_class = ( $css_class == 'row1' ) ? 'row3' : 'row1';
   897   foreach($this->_values as $value) $sumval += $value["value"];
   904       echo "  <td valign=\"bottom\" align=\"center\" class=\"$css_class\">\n      ";
   898   $this->SetSortMode($this->_RowSortMode);
   905       $width = $this->_BarWidth;
   899   echo "<table border=0>";
   906       $height = ceil( $value["value"] * $this->_GraphWidth / $maxval );
   900   if (strlen($this->_GraphTitle)>0)  {
   907 
   901     echo "<tr><td ";
   908       echo "<div style=\"width: {$width}px; height: {$height}px; background-color: {$this->_BarBackgroundColor}; border: ".$this->_BarBorderWidth."px solid ".$this->_BarBorderColor."\">\n      ";
   902    if ($this->_ShowCountsMode>0) echo " colspan=2";
   909       echo "</div>\n    ";
   903     echo " class=\"".$this->_TDClassHead."\">".$this->_GraphTitle."</TD></TR>";
   910       
   904   }
   911       // echo "<img src=\"".$this->_BarImg."\" height=\"$width\" width=\"$height\" ";
   905   foreach($this->_values as $value) {
   912       // echo "  style=\"border: ".$this->_BarBorderWidth."px solid ".$this->_BarBorderColor."\"";
   906    if ($this->_ShowLabels) {
   913       // echo ">";
   907     echo "<tr>";
   914 
   908     echo "<td class=".$this->_TDClassLabel;
   915       echo "</td>\n  ";
   909     if ($this->_ShowCountsMode>0) echo " colspan=2";
   916     }
   910 	echo ">".$value["label"]."</TD></TR>";
   917     echo "</tr>\n  ";
   911    }	
   918     if ( $this->_ShowCountsMode > 0 )
   912    echo "<tr>";
   919     {
   913    if ($this->_ShowCountsMode>0) {
   920       $css_class = 'row1';
   914     switch ($this->_ShowCountsMode) {
   921       echo "<tr>\n  ";
   915     case 1:
   922       foreach($this->_values as $value)
   916      $count = round(100*$value["value"]/$sumval)."%";
   923       {
   917      break;
   924         $css_class = ( $css_class == 'row1' ) ? 'row3' : 'row1';
   918     case 2:
   925         switch ($this->_ShowCountsMode)
   919      $count = $value["value"];
   926         {
   920      break;  /* Exit the switch and the while. */
   927           case 1:
   921     default:
   928             $count = round ( 100 * $value["value"] / $sumval ) . "%";
   922      break;
   929             break;
   923     }
   930           case 2:
   924    echo "<td class=".$this->_TDClassCount.">$count</TD>";
   931             $count = $value["value"];
   925    }
   932             break;
   926    echo "<td>";
   933           default:
   927    $height = $this->_BarWidth;
   934             break;
   928     $width=ceil($value["value"]*$this->_GraphWidth/$maxval);
   935         }
   929    echo "<img SRC=\"".$this->_BarImg."\" height=$height width=$width ";
   936         echo "  <td align=\"center\" class=\"$css_class ".$this->_TDClassCount."\">$count</td>\n  ";
   930    echo "  style=\"border: ".$this->_BarBorderWidth."px solid ".$this->_BarBorderColor."\"";
   937       }
   931    echo ">";
   938       echo "</tr>\n";
   932    echo "</TD></TR>";
   939     }
   933   }
   940 
   934   echo "</TABLE>";
   941     if ($this->_ShowLabels)
   935  }
   942     {
       
   943       $css_class = 'row1';
       
   944       echo "  <tr>\n  ";
       
   945       foreach($this->_values as $value)
       
   946       {
       
   947         $css_class = ( $css_class == 'row1' ) ? 'row3' : 'row1';
       
   948         echo "  <td align=\"center\" class=\"$css_class ".$this->_TDClassLabel."\"";
       
   949         echo ">".$value["label"]."</td>\n  ";
       
   950       }
       
   951       echo "</tr>\n";
       
   952     }
       
   953 
       
   954     echo "</table>";
       
   955   }
       
   956 
       
   957   function BarGraphHoriz()
       
   958   {
       
   959     $maxval = $this->GetMaxVal();
       
   960     
       
   961     foreach($this->_values as $value)
       
   962     {
       
   963       $sumval += $value["value"];
       
   964     }
       
   965     
       
   966     $this->SetSortMode($this->_RowSortMode);
       
   967     
       
   968     echo "<table border=\"0\">";
       
   969     
       
   970     if ( strlen($this->_GraphTitle) > 0 )
       
   971     {
       
   972       echo "<tr><td ";
       
   973       if ( $this->_ShowCountsMode > 0 )
       
   974       {
       
   975         echo " colspan=\"2\"";
       
   976       }
       
   977       echo " class=\"".$this->_TDClassHead."\">".$this->_GraphTitle."</td></tr>";
       
   978     }
       
   979     foreach($this->_values as $value)
       
   980     {
       
   981       if ($this->_ShowLabels)
       
   982       {
       
   983         echo "<tr>";
       
   984         echo "<td class=\"".$this->_TDClassLabel."\"";
       
   985         if ( $this->_ShowCountsMode > 0 )
       
   986         {
       
   987           echo " colspan=\"2\"";
       
   988         }
       
   989         echo ">".$value["label"]."</td></tr>";
       
   990       }
       
   991       echo "<tr>";
       
   992       if ( $this->_ShowCountsMode > 0 )
       
   993       {
       
   994         switch ($this->_ShowCountsMode)
       
   995         {
       
   996           case 1:
       
   997             $count = round(100 * $value["value"] / $sumval )."%";
       
   998             break;
       
   999           case 2:
       
  1000             $count = $value["value"];
       
  1001             break;  /* Exit the switch and the while. */
       
  1002           default:
       
  1003             break;
       
  1004         }
       
  1005         echo "<td class=\"".$this->_TDClassCount."\">$count</TD>";
       
  1006       }
       
  1007       echo "<td>";
       
  1008       $height = $this->_BarWidth;
       
  1009       $width = ceil( $value["value"] * $this->_GraphWidth / $maxval );
       
  1010       echo "<div style=\"width: {$width}px; height: {$height}px; background-color: #456798; border: ".$this->_BarBorderWidth."px solid ".$this->_BarBorderColor."\">\n      ";
       
  1011       echo "</div>\n    ";
       
  1012       //echo "<img SRC=\"".$this->_BarImg."\" height=$height width=$width ";
       
  1013       //echo "  style=\"border: ".$this->_BarBorderWidth."px solid ".$this->_BarBorderColor."\"";
       
  1014       //echo ">";
       
  1015       echo "</td></tr>";
       
  1016     }
       
  1017     echo "</table>";
       
  1018   }
       
  1019   /**
       
  1020    * Dummy functions for compatibility with the GD version of the class
       
  1021    */
       
  1022   
       
  1023   function SetGraphPadding($a, $b, $c, $d)
       
  1024   {
       
  1025     return true;
       
  1026   }
       
  1027   function SetBarPadding($a)
       
  1028   {
       
  1029     return true;
       
  1030   }
       
  1031   function SetAxisStep($a)
       
  1032   {
       
  1033     return true;
       
  1034   }
       
  1035   function SetGraphBackgroundTransparent($r, $g, $b, $a)
       
  1036   {
       
  1037     return true;
       
  1038   }
       
  1039   function SetGraphTransparency($a)
       
  1040   {
       
  1041     return true;
       
  1042   }
       
  1043   function SetGraphAreaHeight($a)
       
  1044   {
       
  1045     return true;
       
  1046   }
   936 }
  1047 }
   937 
  1048 
       
  1049