includes/graphs.php
changeset 1 fe660c52c48f
child 16 64e0d3d4cf14
equal deleted inserted replaced
0:902822492a68 1:fe660c52c48f
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
       
     5  * Version 1.0 (Banshee)
       
     6  * Copyright (C) 2006-2007 Dan Fuhry
       
     7  *
       
     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.
       
    10  *
       
    11  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    12  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    13  */
       
    14 
       
    15 // BarGraph for PHP
       
    16 // Source: http://www.phpclasses.org/browse/package/1567.html
       
    17 // License: PHP license, see licenses/phplic.html included with this package
       
    18 
       
    19 class GraphMaker {
       
    20   /**
       
    21    * GraphMaker::bar_width
       
    22    * Width of bars
       
    23    */
       
    24   var $bar_width = 32;
       
    25   /**
       
    26    * GraphMaker::bar_height
       
    27    * Height of bars
       
    28    */
       
    29   var $bar_height = 8;
       
    30   /**
       
    31    * GraphMaker::bar_data
       
    32    * Data of all bars
       
    33    */
       
    34   var $bar_data = array('a' => 7, 'b' => 3, 'c' => 6, 'd' => 0, 'e' => 2);
       
    35   /**
       
    36    * GraphMaker::bar_padding
       
    37    * Padding of bars
       
    38    */
       
    39   var $bar_padding = 5;
       
    40   /**
       
    41    * GraphMaker::bar_bordercolor
       
    42    * Border color of bars
       
    43    */
       
    44   var $bar_bordercolor = array(39, 78, 120);
       
    45   /**
       
    46    * GraphMaker::bar_bgcolor
       
    47    * Background color of bars
       
    48    */
       
    49   var $bar_bgcolor = array(69, 129, 194);
       
    50   //---------------------------------------------
       
    51   /**
       
    52    * GraphMaker::graph_areaheight
       
    53    * Height of graphic area
       
    54    */
       
    55   var $graph_areaheight = 100;
       
    56   /**
       
    57    * GraphMaker::graph_padding
       
    58    * Paddings of graph
       
    59    */
       
    60   var $graph_padding = array('left' => 50, 'top' => 20, 'right'  => 20, 'bottom' => 20);
       
    61   /**
       
    62    * GraphMaker::graph_title
       
    63    * Title text of graph
       
    64    */
       
    65   var $graph_title = "";
       
    66   /**
       
    67    * GraphMaker::graph_bgcolor
       
    68    * Background color of graph
       
    69    */
       
    70   var $graph_bgcolor = array(255, 255, 255);
       
    71   /**
       
    72    * GraphMaker::graph_bgtransparent
       
    73    * Boolean for background transparency
       
    74    */
       
    75   var $graph_bgtransparent = 0;
       
    76   /**
       
    77    * GraphMaker::graph_transparencylevel
       
    78    * Transparency level (0=opaque, 127=transparent)
       
    79    */
       
    80   var $graph_transparencylevel = 0;
       
    81   /**
       
    82    * GraphMaker::graph_borderwidth
       
    83    * Width of graph border
       
    84    */
       
    85   var $graph_borderwidth = 1;
       
    86   /**
       
    87    * GraphMaker::graph_bordercolor
       
    88    * Border color of graph
       
    89    */
       
    90   var $graph_bordercolor = array(218, 218, 239);
       
    91   /**
       
    92    * GraphMaker::graph_titlecolor
       
    93    * Color of title text of graph
       
    94    */
       
    95   var $graph_titlecolor = array(99, 88, 78);
       
    96   //---------------------------------------------
       
    97   /**
       
    98    * GraphMaker::axis_step
       
    99    * Scale step of axis
       
   100    */
       
   101   var $axis_step = 2;
       
   102   /**
       
   103    * GraphMaker::axis_bordercolor
       
   104    * Border color of axis
       
   105    */
       
   106   var $axis_bordercolor = array(99, 88, 78);
       
   107   /**
       
   108    * GraphMaker::axis_bgcolor
       
   109    * Background color of axis
       
   110    */
       
   111   var $axis_bgcolor = array(152, 137, 124);
       
   112 
       
   113   /****************************************************************
       
   114                               GRAPH
       
   115   ****************************************************************/
       
   116 
       
   117   /**
       
   118    * GraphMaker::SetGraphAreaHeight()
       
   119    * Sets graph height (not counting top and bottom margins)
       
   120    **/
       
   121   function SetGraphAreaHeight($height) {
       
   122     if ($height > 0) $this->graph_areaheight = $height;
       
   123   }
       
   124 
       
   125   /**
       
   126    * GraphMaker::SetGraphPadding()
       
   127    * Sets graph padding (margins)
       
   128    **/
       
   129   function SetGraphPadding($left, $top, $right, $bottom) {
       
   130     $this->graph_padding = array('left'   => (int) $left,
       
   131                                  'top'    => (int) $top,
       
   132                                  'right'  => (int) $right,
       
   133                                  'bottom' => (int) $bottom);
       
   134   }
       
   135 
       
   136   /**
       
   137    * GraphMaker::SetGraphTitle()
       
   138    * Set title text
       
   139    **/
       
   140   function SetGraphTitle($title) {
       
   141     $this->graph_title = $title;
       
   142   }
       
   143 
       
   144   /**
       
   145    * GraphMaker::SetGraphBorderColor()
       
   146    * Sets border color for graph
       
   147    **/
       
   148   function SetGraphBorderColor($red, $green, $blue) {
       
   149     $this->graph_bordercolor = array($red, $green, $blue);
       
   150   }
       
   151 
       
   152   /**
       
   153    * GraphMaker::SetGraphBorderWidth()
       
   154    * Set width of border. 0 disables border
       
   155    **/
       
   156   function SetGraphBorderWidth($width = 0) {
       
   157     $this->graph_borderwidth = $width;
       
   158   }
       
   159 
       
   160   /**
       
   161    * GraphMaker::SetGraphBackgroundColor()
       
   162    * Sets background color for graph
       
   163    **/
       
   164   function SetGraphBackgroundColor($red, $green, $blue) {
       
   165     $this->graph_bgcolor = array($red, $green, $blue);
       
   166   }
       
   167 
       
   168   /**
       
   169    * GraphMaker::SetGraphBackgroundTransparent()
       
   170    * Sets background color for graph (and set it transparent)
       
   171    **/
       
   172   function SetGraphBackgroundTransparent($red, $green, $blue, $addtransparency = 1) {
       
   173     $this->graph_bgcolor = array($red, $green, $blue);
       
   174     $this->graph_bgtransparent = ($addtransparency ? 1 : 0);
       
   175   }
       
   176 
       
   177   /**
       
   178    * GraphMaker::SetGraphTitleColor()
       
   179    * Sets title color for graph
       
   180    **/
       
   181   function SetGraphTitleColor($red, $green, $blue) {
       
   182     $this->graph_titlecolor = array($red, $green, $blue);
       
   183   }
       
   184 
       
   185   /**
       
   186    * GraphMaker::SetGraphTransparency()
       
   187    * Sets transparency for graph
       
   188    **/
       
   189   function SetGraphTransparency($percent) {
       
   190     if ($percent < 0) $percent = 0;
       
   191     elseif ($percent > 100) $percent = 127;
       
   192     else $percent = $percent * 1.27;
       
   193     $this->graph_transparencylevel = $percent;
       
   194   }
       
   195 
       
   196   /****************************************************************
       
   197                                BAR
       
   198   ****************************************************************/
       
   199 
       
   200   /**
       
   201    * GraphMaker::SetBarBorderColor()
       
   202    * Sets border color for bars
       
   203    **/
       
   204   function SetBarBorderColor($red, $green, $blue) {
       
   205     $this->bar_bordercolor = array($red, $green, $blue);
       
   206   }
       
   207 
       
   208   /**
       
   209    * GraphMaker::SetBarBackgroundColor()
       
   210    * Sets background color for bars
       
   211    **/
       
   212   function SetBarBackgroundColor($red, $green, $blue) {
       
   213     $this->bar_bgcolor = array($red, $green, $blue);
       
   214   }
       
   215 
       
   216   /**
       
   217    * GraphMaker::SetBarData()
       
   218    * Sets data of graph (parameter should be an array with key
       
   219    * being the name of the bar and the value the value of the bar.
       
   220    **/
       
   221   function SetBarData($data) {
       
   222     if (is_array($data)) $this->bar_data = $data;
       
   223   }
       
   224 
       
   225   /**
       
   226    * GraphMaker::SetBarDimensions()
       
   227    * Sets with and height of each bar
       
   228    **/
       
   229   function SetBarDimensions($width, $height) {
       
   230     if ($width > 0) $this->bar_width = $width;
       
   231     if ($height > 0) $this->bar_height = $height;
       
   232   }
       
   233 
       
   234   /**
       
   235    * GraphMaker::SetBarPadding()
       
   236    * Sets padding (border) around each bar
       
   237    **/
       
   238   function SetBarPadding($padding) {
       
   239     if ($padding > 0) $this->bar_padding = $padding;
       
   240   }
       
   241 
       
   242   /****************************************************************
       
   243                                AXIS
       
   244   ****************************************************************/
       
   245 
       
   246   /**
       
   247    * GraphMaker::SetAxisBorderColor()
       
   248    * Sets border color for axis
       
   249    **/
       
   250   function SetAxisBorderColor($red, $green, $blue) {
       
   251     $this->axis_bordercolor = array($red, $green, $blue);
       
   252   }
       
   253 
       
   254   /**
       
   255    * GraphMaker::SetAxisBackgroundColor()
       
   256    * Sets background color for axis
       
   257    **/
       
   258   function SetAxisBackgroundColor($red, $green, $blue) {
       
   259     $this->axis_bgcolor = array($red, $green, $blue);
       
   260   }
       
   261 
       
   262   /**
       
   263    * GraphMaker::SetAxisStep()
       
   264    * Sets axis scale step
       
   265    **/
       
   266   function SetAxisStep($step) {
       
   267     if ($step > 0) $this->axis_step = $step;
       
   268   }
       
   269 
       
   270   /**
       
   271    * GraphMaker::GetFinalGraphDimensions()
       
   272    * From the values already setted, it calculates image
       
   273    * width and height
       
   274    **/
       
   275   function GetFinalGraphDimensions() {
       
   276     $w = $this->graph_padding['left'] +
       
   277          (count($this->bar_data) * ($this->bar_width + ($this->bar_padding * 2))) +
       
   278          $this->graph_padding['right'];
       
   279     $h = $this->graph_padding['top'] +
       
   280          $this->graph_areaheight +
       
   281          $this->graph_padding['bottom'];
       
   282     return array($w, $h);
       
   283   }
       
   284 
       
   285   /**
       
   286    * GraphMaker::LoadGraph()
       
   287    * Loads definitions from a file
       
   288    **/
       
   289   function LoadGraph($path) {
       
   290     if (($fp = @fopen($path, "r")) !== false) {
       
   291       $content = "";
       
   292       while (!feof($fp)) {              // I do not use filesize() here
       
   293         $content .= fread($fp, 4096);   // because of remote files. If
       
   294       }                                 // there is no problem with them
       
   295       fclose($fp);                      // please let me know
       
   296       $this->__LoadGraphDefinitions($content);
       
   297       return true;
       
   298     } else return false;
       
   299   }
       
   300 
       
   301   /**
       
   302    * GraphMaker::DrawGraph()
       
   303    * Draw all the graph: bg, axis, bars, text.. and output it
       
   304    * Optional file parameter turns output to file, and bool on success
       
   305    **/
       
   306   function DrawGraph($file = "") {
       
   307     list($w, $h) = $this->GetFinalGraphDimensions();
       
   308     $this->graph_width = $w;
       
   309     $this->graph_height = $h;
       
   310 
       
   311     $this->im = imagecreatetruecolor($w, $h);
       
   312     if ($this->graph_transparencylevel) {
       
   313       imagealphablending($this->im, true);
       
   314     }
       
   315 
       
   316     $this->__PaintBackground();
       
   317     $this->__DrawAxis();
       
   318 
       
   319     $p = 0;
       
   320     foreach ($this->bar_data as $name => $value) {
       
   321       $p++;
       
   322       $this->__DrawBarText($p, $name);
       
   323       $this->__DrawBar($p, $value);
       
   324     }
       
   325 
       
   326     if (strlen($this->graph_title)) {
       
   327       $this->__AllocateColor("im_graph_titlecolor",
       
   328                              $this->graph_titlecolor,
       
   329                              $this->graph_transparencylevel);
       
   330       $this->__DrawText($this->graph_title,
       
   331                         floor($this->graph_width / 2),
       
   332                         $this->graph_borderwidth + 2,
       
   333                         $this->im_graph_titlecolor,
       
   334                         2,
       
   335                         1);
       
   336     }
       
   337 
       
   338     if (strlen($file)) {
       
   339       $ret = imagepng($this->im, $file);
       
   340     } else {
       
   341       header('Content-Type: image/png');
       
   342       imagepng($this->im);
       
   343       $ret = true;
       
   344     }
       
   345     imagedestroy($this->im);
       
   346     return $ret;
       
   347   }
       
   348 
       
   349   /**
       
   350    * GraphMaker::PaintBackground()
       
   351    * Draw all the graph: bg, axis, bars, text.. and output it
       
   352    * Optional file parameter turns output to file, and bool on success
       
   353    **/
       
   354   function __PaintBackground() {
       
   355     $this->__AllocateColor("im_graph_bgcolor",
       
   356                            $this->graph_bgcolor,
       
   357                            0);
       
   358     imagefilledrectangle($this->im,
       
   359                          0,
       
   360                          0,
       
   361                          $this->graph_width,
       
   362                          $this->graph_height,
       
   363                          $this->im_graph_bgcolor);
       
   364     if ($this->graph_bgtransparent) {
       
   365       imagecolortransparent($this->im, $this->im_graph_bgcolor);
       
   366     }
       
   367     if ($this->graph_borderwidth) {
       
   368       $this->__AllocateColor("im_graph_bordercolor",
       
   369                              $this->graph_bordercolor,
       
   370                              $this->graph_transparencylevel);
       
   371       for ($i = 0; $i < $this->graph_borderwidth; $i++) {
       
   372         imagerectangle($this->im,
       
   373                        $i,
       
   374                        $i,
       
   375                        $this->graph_width - 1 - $i,
       
   376                        $this->graph_height - 1 - $i,
       
   377                        $this->im_graph_bordercolor);
       
   378       }
       
   379     }
       
   380   }
       
   381 
       
   382   /**
       
   383    * GraphMaker::__DrawAxis()
       
   384    * Draws all the axis stuff (and scale steps)
       
   385    **/
       
   386   function __DrawAxis() {
       
   387     $this->__AllocateColor("im_axis_bordercolor",
       
   388                            $this->axis_bordercolor,
       
   389                            $this->graph_transparencylevel);
       
   390     $this->__AllocateColor("im_axis_bgcolor",
       
   391                            $this->axis_bgcolor,
       
   392                            $this->graph_transparencylevel);
       
   393     $this->__DrawPolygon($this->graph_padding['left'], $this->graph_height - $this->graph_padding['bottom'],
       
   394                          $this->graph_padding['left'], $this->graph_padding['top'],
       
   395                          $this->graph_padding['left'] + $this->bar_height - 1, $this->graph_padding['top'] - $this->bar_height + 1,
       
   396                          $this->graph_padding['left'] + $this->bar_height - 1, $this->graph_height - $this->graph_padding['bottom'] - $this->bar_height + 1,
       
   397                          $this->im_axis_bgcolor, true);
       
   398     $this->__DrawPolygon($this->graph_padding['left'], $this->graph_height - $this->graph_padding['bottom'],
       
   399                          $this->graph_padding['left'], $this->graph_padding['top'],
       
   400                          $this->graph_padding['left'] + $this->bar_height - 1, $this->graph_padding['top'] - $this->bar_height + 1,
       
   401                          $this->graph_padding['left'] + $this->bar_height - 1, $this->graph_height - $this->graph_padding['bottom'] - $this->bar_height + 1,
       
   402                          $this->im_axis_bordercolor);
       
   403 
       
   404     $this->__DrawPolygon($this->graph_padding['left'], $this->graph_height - $this->graph_padding['bottom'],
       
   405                          $this->graph_padding['left'] + $this->bar_height - 1, $this->graph_height - $this->graph_padding['bottom'] - $this->bar_height + 1,
       
   406                          $this->graph_width - $this->graph_padding['right'] + $this->bar_height - 1, $this->graph_height - $this->graph_padding['bottom'] - $this->bar_height + 1,
       
   407                          $this->graph_width - $this->graph_padding['right'], $this->graph_height - $this->graph_padding['bottom'],
       
   408                          $this->im_axis_bgcolor, true);
       
   409     $this->__DrawPolygon($this->graph_padding['left'], $this->graph_height - $this->graph_padding['bottom'],
       
   410                          $this->graph_padding['left'] + $this->bar_height - 1, $this->graph_height - $this->graph_padding['bottom'] - $this->bar_height + 1,
       
   411                          $this->graph_width - $this->graph_padding['right'] + $this->bar_height - 1, $this->graph_height - $this->graph_padding['bottom'] - $this->bar_height + 1,
       
   412                          $this->graph_width - $this->graph_padding['right'], $this->graph_height - $this->graph_padding['bottom'],
       
   413                          $this->im_axis_bordercolor);
       
   414 
       
   415     // draw lines that separate bars
       
   416     $total_bars = count($this->bar_data);
       
   417     for ($i = 1; $i < $total_bars; $i++) {
       
   418       $offset = $this->graph_padding['left'] +
       
   419                 (($this->bar_width + ($this->bar_padding * 2)) * $i);
       
   420       imageline($this->im,
       
   421                 $offset,
       
   422                 $this->graph_height - $this->graph_padding['bottom'],
       
   423                 $offset + $this->bar_height - 1,
       
   424                 $this->graph_height - $this->graph_padding['bottom'] - $this->bar_height + 1,
       
   425                 $this->im_axis_bordercolor);
       
   426     }
       
   427 
       
   428     // draw scale steps
       
   429     $max_value = $this->__GetMaxGraphValue();
       
   430     if (($max_value % 10) > 0) {
       
   431       $max_value = $max_value + (10 - ($max_value % 10));
       
   432     }
       
   433     $this->axis_max = $max_value;
       
   434     $y = 0;
       
   435     $style = array($this->im_axis_bordercolor, $this->im_graph_bgcolor);
       
   436     imagesetstyle($this->im, $style);
       
   437     while ($y <= $max_value) {
       
   438       if ($max_value == 0) { $max_value=1; } // corrected by Marcelo Trenkenchu
       
   439       $offset = floor($this->graph_height - $this->graph_padding['bottom'] -
       
   440                 ($y * $this->graph_areaheight / $max_value));
       
   441       imageline($this->im,
       
   442                 $this->graph_padding['left'],
       
   443                 $offset,
       
   444                 $this->graph_padding['left'] + $this->bar_height - 1,
       
   445                 $offset - $this->bar_height + 1,
       
   446                 $this->im_axis_bordercolor);
       
   447       $this->__DrawText($y,
       
   448                         $this->graph_padding['left'],
       
   449                         $offset,
       
   450                         $this->im_axis_bordercolor,
       
   451                         1,
       
   452                         2,
       
   453                         1);
       
   454       // gridline
       
   455       if ($y > 0) {
       
   456         imageline($this->im,
       
   457                   $this->graph_padding['left'] + $this->bar_height,
       
   458                   $offset - $this->bar_height + 1,
       
   459                   $this->graph_width - $this->graph_padding['right'] + $this->bar_height - 1,
       
   460                   $offset - $this->bar_height + 1,
       
   461                   IMG_COLOR_STYLED);
       
   462       }
       
   463       $y += $this->axis_step;
       
   464     }
       
   465 
       
   466     imageline($this->im,
       
   467               $this->graph_width - $this->graph_padding['right'] + $this->bar_height - 1,
       
   468               $this->graph_padding['top'] - $this->bar_height + 1,
       
   469               $this->graph_width - $this->graph_padding['right'] + $this->bar_height - 1,
       
   470               $this->graph_height - $this->graph_padding['bottom'] - $this->bar_height,
       
   471               IMG_COLOR_STYLED);
       
   472   }
       
   473 
       
   474   /**
       
   475    * GraphMaker::__DrawText()
       
   476    * Draws text on image with color, size and alignment options
       
   477    **/
       
   478   function __DrawText($text, $x, $y, $color, $size = 1, $align = 0, $valign = 0) {
       
   479     /*
       
   480      * Align: 0=left | 1=center | 2=right
       
   481      */
       
   482     if ($align == 1) $x -= floor(strlen($text) * imagefontwidth($size) / 2);
       
   483     elseif ($align == 2) $x -= (strlen($text) * imagefontwidth($size));
       
   484     if ($valign == 1) $y -= floor(imagefontheight($size) / 2);
       
   485     elseif ($valign == 2) $y -= imagefontheight($size);
       
   486     imagestring($this->im,
       
   487                 $size,
       
   488                 $x,
       
   489                 $y,
       
   490                 $text,
       
   491                 $color);
       
   492   }
       
   493 
       
   494   /**
       
   495    * GraphMaker::__GetMaxGraphValue()
       
   496    * Returns max bar value
       
   497    **/
       
   498   function __GetMaxGraphValue() {
       
   499     $max_value = 0;
       
   500     foreach ($this->bar_data as $name => $value) {
       
   501       if ($value > $max_value) $max_value = $value;
       
   502     }
       
   503     return $max_value;
       
   504   }
       
   505 
       
   506   /**
       
   507    * GraphMaker::__DrawBarText()
       
   508    * Determines top and left to draw text to a choosen bar
       
   509    **/
       
   510   function __DrawBarText($bar, $text) {
       
   511     $this->__DrawText($text,
       
   512                       $this->graph_padding['left'] + (($this->bar_width + ($this->bar_padding * 2)) * ($bar - 0.5)),
       
   513                       $this->graph_height - $this->graph_padding['bottom'] + 1,
       
   514                       $this->axis_bordercolor,
       
   515                       1,
       
   516                       1);
       
   517   }
       
   518 
       
   519   /**
       
   520    * GraphMaker::__DrawBar()
       
   521    * Draws a choosen bar with it's value
       
   522    **/
       
   523   function __DrawBar($bar, $value) {
       
   524     $x = $this->graph_padding['left'] +
       
   525          (($this->bar_width + ($this->bar_padding * 2)) * ($bar - 1)) +
       
   526          $this->bar_padding;
       
   527     if ($this->axis_max == 0) { $this->axis_max = 1; } // corrected by Marcelo Trenkenchu
       
   528     $y = $value * $this->graph_areaheight / $this->axis_max;
       
   529     $this->____DrawBar($x,
       
   530                        $this->graph_height - $this->graph_padding['bottom'] - $y,
       
   531                        $x + $this->bar_width,
       
   532                        $this->graph_height - $this->graph_padding['bottom']);
       
   533   }
       
   534 
       
   535   /**
       
   536    * GraphMaker::____DrawBar()
       
   537    * Draws the actual rectangles that form a bar
       
   538    **/
       
   539   function ____DrawBar($x1, $y1, $x2, $y2) {
       
   540     $this->__AllocateColor("im_bar_bordercolor",
       
   541                            $this->bar_bordercolor,
       
   542                            $this->graph_transparencylevel);
       
   543     $this->__AllocateColor("im_bar_bgcolor",
       
   544                            $this->bar_bgcolor,
       
   545                            $this->graph_transparencylevel);
       
   546     $this->__DrawPolygon($x1,                         $y1,
       
   547                          $x2,                         $y1,
       
   548                          $x2,                         $y2,
       
   549                          $x1,                         $y2,
       
   550                          $this->im_bar_bgcolor,       true);
       
   551     $this->__DrawPolygon($x1,                         $y1,
       
   552                          $x2,                         $y1,
       
   553                          $x2,                         $y2,
       
   554                          $x1,                         $y2,
       
   555                          $this->im_bar_bordercolor);
       
   556     $this->__DrawPolygon($x1,                         $y1,
       
   557                          $x2,                         $y1,
       
   558                          $x2 + $this->bar_height - 1, $y1 - $this->bar_height + 1,
       
   559                          $x1 + $this->bar_height - 1, $y1 - $this->bar_height + 1,
       
   560                          $this->im_bar_bgcolor,       true);
       
   561     $this->__DrawPolygon($x1,                         $y1,
       
   562                          $x2,                         $y1,
       
   563                          $x2 + $this->bar_height - 1, $y1 - $this->bar_height + 1,
       
   564                          $x1 + $this->bar_height - 1, $y1 - $this->bar_height + 1,
       
   565                          $this->im_bar_bordercolor);
       
   566     $this->__DrawPolygon($x2,                         $y2,
       
   567                          $x2,                         $y1,
       
   568                          $x2 + $this->bar_height - 1, $y1 - $this->bar_height + 1,
       
   569                          $x2 + $this->bar_height - 1, $y2 - $this->bar_height + 1,
       
   570                          $this->im_bar_bgcolor,       true);
       
   571     $this->__DrawPolygon($x2,                         $y2,
       
   572                          $x2,                         $y1,
       
   573                          $x2 + $this->bar_height - 1, $y1 - $this->bar_height + 1,
       
   574                          $x2 + $this->bar_height - 1, $y2 - $this->bar_height + 1,
       
   575                          $this->im_bar_bordercolor);
       
   576   }
       
   577 
       
   578   /**
       
   579    * GraphMaker::__DrawPolygon()
       
   580    * Draws a (filled) (ir)regular polygon
       
   581    **/
       
   582   function __DrawPolygon($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4, $color, $filled = false) {
       
   583     if ($filled) {
       
   584       imagefilledpolygon($this->im, array($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4), 4, $color);
       
   585     } else {
       
   586       imagepolygon($this->im, array($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4), 4, $color);
       
   587     }
       
   588   }
       
   589 
       
   590   /**
       
   591    * GraphMaker::__LoadGraphDefinitions()
       
   592    * Loads definitions to a graph from text lines (normaly
       
   593    * they come from a file). This function is called by
       
   594    * GraphMaker::LoadGraph()
       
   595    **/
       
   596   function __LoadGraphDefinitions($text) {
       
   597     $text = preg_split("/\r?\n/", $text);
       
   598     $data = array();
       
   599     $section = '';
       
   600     for ($i = 0; $i < count($text); $i++) {
       
   601       if (preg_match("/^\s*#/", $text[$i])) {
       
   602         //ignore.. it's just a comment
       
   603       } elseif (preg_match("/^\s*\}\s*/", $text[$i])) {
       
   604         $section = '';
       
   605       } elseif (preg_match("/^\s*(\w+)\s*\{\s*$/", $text[$i], $r)) {
       
   606         $section = $r[1];
       
   607       } else {
       
   608         $p = strpos($text[$i], "=");
       
   609         if ($p !== false) {
       
   610           $data[$section][trim(substr($text[$i], 0, $p))] = trim(substr($text[$i], $p + 1));
       
   611         }
       
   612       }
       
   613     }
       
   614     if (is_array($data['graph'])) {
       
   615       $this->__LoadGraphValues($data['graph']);
       
   616     }
       
   617     if (is_array($data['bar'])) {
       
   618       $this->__LoadBarValues($data['bar']);
       
   619     }
       
   620     if (is_array($data['axis'])) {
       
   621       $this->__LoadAxisValues($data['axis']);
       
   622     }
       
   623     if (is_array($data['data'])) {
       
   624       $this->bar_data = $data['data'];
       
   625     }
       
   626   }
       
   627 
       
   628   /**
       
   629    * GraphMaker::__LoadGraphValues()
       
   630    * Loads definitions to main graph settings
       
   631    **/
       
   632   function __LoadGraphValues($data) {
       
   633     foreach ($data as $name => $value) {
       
   634       $name = strtolower($name);
       
   635       switch ($name) {
       
   636         case 'background-color':
       
   637           $this->__SetColorToValue("graph_bgcolor", $value);
       
   638           break;
       
   639         case 'border-color':
       
   640           $this->__SetColorToValue("graph_bordercolor", $value);
       
   641           break;
       
   642         case 'title-color':
       
   643           $this->__SetColorToValue("graph_titlecolor", $value);
       
   644           break;
       
   645         case 'background-transparent':
       
   646           $this->graph_bgtransparent = ($value == 1 || $value == 'yes' ? 1 : 0);
       
   647           break;
       
   648         case 'transparency':
       
   649           $this->SetGraphTransparency(str_replace('%', '', $value));
       
   650           break;
       
   651         case 'title':
       
   652           $this->graph_title = $value;
       
   653           break;
       
   654         case 'border-width':
       
   655           $this->graph_borderwidth = (int) $value;
       
   656           break;
       
   657         case 'area-height':
       
   658           $this->graph_areaheight = (int) $value;
       
   659           break;
       
   660         default:
       
   661           if (substr($name, 0, 8) == 'padding-' && strlen($name) > 8) {
       
   662             $this->graph_padding[substr($name, 8)] = $value;
       
   663           }
       
   664       }
       
   665     }
       
   666   }
       
   667 
       
   668   /**
       
   669    * GraphMaker::__LoadBarValues()
       
   670    * Loads definitions to bar settings
       
   671    **/
       
   672   function __LoadBarValues($data) {
       
   673     foreach ($data as $name => $value) {
       
   674       $name = strtolower($name);
       
   675       switch ($name) {
       
   676         case 'background-color':
       
   677           $this->__SetColorToValue("bar_bgcolor", $value);
       
   678           break;
       
   679         case 'border-color':
       
   680           $this->__SetColorToValue("bar_bordercolor", $value);
       
   681           break;
       
   682         case 'padding':
       
   683           $this->bar_padding = $value;
       
   684           break;
       
   685         case 'width':
       
   686           $this->bar_width = (int) $value;
       
   687           break;
       
   688         case 'height':
       
   689           $this->bar_height = (int) $value;
       
   690           break;
       
   691       }
       
   692     }
       
   693   }
       
   694 
       
   695   /**
       
   696    * GraphMaker::__LoadAxisValues()
       
   697    * Loads definitions to axis settings
       
   698    **/
       
   699   function __LoadAxisValues($data) {
       
   700     foreach ($data as $name => $value) {
       
   701       switch (strtolower($name)) {
       
   702         case 'step':
       
   703           $this->SetAxisStep($value);
       
   704           break;
       
   705         case 'background-color':
       
   706           $this->__SetColorToValue("axis_bgcolor", $value);
       
   707           break;
       
   708         case 'border-color':
       
   709           $this->__SetColorToValue("axis_bordercolor", $value);
       
   710       }
       
   711     }
       
   712   }
       
   713 
       
   714   /**
       
   715    * GraphMaker::__SetColorToValue()
       
   716    * Sets a color (rgb or in html format) to a variable
       
   717    **/
       
   718   function __SetColorToValue($varname, $color) {
       
   719     if ($color[0] == "#") { // if it's hex (html format), change to rgb array
       
   720       if (strlen($color) == 4) {
       
   721         // if only 3 hex values (I assume it's a shade of grey: #ddd)
       
   722         $color .= substr($color, -3);
       
   723       }
       
   724       $color = array(hexdec($color[1].$color[2]),
       
   725                      hexdec($color[3].$color[4]),
       
   726                      hexdec($color[5].$color[6]));
       
   727     }
       
   728     $this->$varname = $color;
       
   729   }
       
   730 
       
   731   function __AllocateColor($varname, $color, $alpha) {
       
   732     $this->$varname = imagecolorallocatealpha($this->im,
       
   733                                               $color[0],
       
   734                                               $color[1],
       
   735                                               $color[2],
       
   736                                               $alpha);
       
   737   }
       
   738 }
       
   739 
       
   740 // Graph Generator for PHP
       
   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
       
   743 // License unknown
       
   744 
       
   745 class GraphMaker_compat {
       
   746  var $_values;
       
   747  var $_ShowLabels;
       
   748  var $_ShowCounts;
       
   749  var $_ShowCountsMode;
       
   750 
       
   751  var $_BarWidth;
       
   752  var $_GraphWidth;
       
   753  var $_BarImg;
       
   754  var $_BarBorderWidth;
       
   755  var $_BarBorderColor;
       
   756  var $_RowSortMode;
       
   757  var $_TDClassHead;
       
   758  var $_TDClassLabel;
       
   759  var $_TDClassCount;
       
   760  var $_GraphTitle;
       
   761 
       
   762  function __construct() {
       
   763   $this->_values = array();
       
   764   $this->_ShowLabels = true;
       
   765   $this->_BarWidth = 16;
       
   766   $this->_GraphWidth = 360;
       
   767   $this->_BarImg = "NULL";
       
   768   $this->_BarBorderWidth = 0;
       
   769   $this->_BarBorderColor = "red";
       
   770   $this->_ShowCountsMode = 2;
       
   771   $this->_RowSortMode = 1;
       
   772   $this->_TDClassHead = "grphh";
       
   773   $this->_TDClassLabel = "grph";
       
   774   $this->_TDClassCount = "grphc";
       
   775   $this->_GraphTitle="Graph title";
       
   776  }
       
   777  
       
   778  function GraphMaker_compat() {
       
   779   $this->__construct();
       
   780  }
       
   781 
       
   782  function SetBarBorderWidth($width) {
       
   783   $this->_BarBorderWidth = $width;
       
   784  }
       
   785  function SetBorderColor($color) {
       
   786   $this->_BarBorderColor = $color;
       
   787  }
       
   788 
       
   789 //  mode = 1 labels asc, 2 label desc
       
   790  function SetSortMode($mode) {
       
   791   switch ($mode) {
       
   792    case 1:
       
   793     asort($this->_values);
       
   794     break;
       
   795    case 2:
       
   796     arsort($this->_values);
       
   797     break;
       
   798    default:
       
   799     break;
       
   800    }
       
   801 
       
   802  }
       
   803 
       
   804  function AddValue($labelName, $theValue) {
       
   805   array_push($this->_values, array("label" => $labelName, "value" => $theValue));
       
   806 
       
   807  }
       
   808  function SetBarWidth($width) {
       
   809   $this->_BarWidth = $width;
       
   810  }
       
   811  function SetBarImg($img) {
       
   812   $this->_BarImg = $img;
       
   813  }
       
   814  function SetShowLabels($lables) {
       
   815   $this->_ShowLabels = $labels;
       
   816  }
       
   817  function SetGraphWidth($width) {
       
   818   $this->_GraphWidth = $width;
       
   819  }
       
   820  function SetGraphTitle($title) {
       
   821   $this->_GraphTitle = $title;
       
   822  }
       
   823  //mode = percentage or counts
       
   824  function SetShowCountsMode($mode) {
       
   825   $this->_ShowCountsMode = $mode;
       
   826  }
       
   827  //mode = none(0) label(1) or count(2)
       
   828  function SetRowSortMode($sortmode) {
       
   829   $this->_RowSortMode = $sortmode;
       
   830  }
       
   831 
       
   832  function SetTDClassHead($class) {
       
   833   $this->_TDClassHead = $class;
       
   834  }
       
   835  function SetTDClassLabel($class) {
       
   836   $this->_TDClassLabel = $class;
       
   837  }
       
   838  function SetTDClassCount($class) {
       
   839   $this->_TDClassCount = $class;
       
   840  }
       
   841  function GetMaxVal() {
       
   842   $maxval = 0;
       
   843   foreach($this->_values as $value) if($maxval<$value["value"]) $maxval = $value["value"];
       
   844   return $maxval;
       
   845  }
       
   846  function BarGraphVert() {
       
   847   $maxval = $this->GetMaxVal();
       
   848   foreach($this->_values as $value) $sumval += $value["value"];
       
   849   $this->SetSortMode($this->_RowSortMode);
       
   850   echo "<table>";
       
   851   if (strlen($this->_GraphTitle)>0)  echo "<tr><td colspan=".count($this->_values)." class=\"".$this->_TDClassHead."\">".$this->_GraphTitle."</td></tr>";
       
   852    echo "<tr>";
       
   853    foreach($this->_values as $value) {
       
   854     echo "<td valign=bottom align=center>";
       
   855     $height = $this->_BarWidth;
       
   856     $width=ceil($value["value"]*$this->_GraphWidth/$maxval);
       
   857     echo "<div ";
       
   858     echo "  style=\"background-color: #666666; border: ".$this->_BarBorderWidth."px solid ".$this->_BarBorderColor."\"";
       
   859     echo ">";
       
   860     echo "</td>";
       
   861    }
       
   862    echo "</tr>";
       
   863    if ($this->_ShowCountsMode>0) {
       
   864    	echo "<tr>";
       
   865     foreach($this->_values as $value) {
       
   866      switch ($this->_ShowCountsMode) {
       
   867      case 1:
       
   868       $count = round(100*$value["value"]/$sumval)."%";
       
   869       break;
       
   870      case 2:
       
   871       $count = $value["value"];
       
   872       break;  /* Exit the switch and the while. */
       
   873      default:
       
   874       break;
       
   875      }
       
   876      echo "<td align=center class=".$this->_TDClassCount.">$count</td>";
       
   877    	}
       
   878 	echo "</tr>";
       
   879    }
       
   880 
       
   881    if ($this->_ShowLabels) {
       
   882     echo "<tr>";
       
   883     foreach($this->_values as $value) {
       
   884      echo "<td align=center class=".$this->_TDClassLabel;
       
   885 	 echo ">".$value["label"]."</td>";
       
   886 	}
       
   887 	echo "</tr>";
       
   888    }	
       
   889 
       
   890   echo "</table>";
       
   891  }
       
   892 
       
   893 
       
   894 
       
   895  function BarGraphHoriz() {
       
   896   $maxval = $this->GetMaxVal();
       
   897   foreach($this->_values as $value) $sumval += $value["value"];
       
   898   $this->SetSortMode($this->_RowSortMode);
       
   899   echo "<table border=0>";
       
   900   if (strlen($this->_GraphTitle)>0)  {
       
   901     echo "<tr><td ";
       
   902    if ($this->_ShowCountsMode>0) echo " colspan=2";
       
   903     echo " class=\"".$this->_TDClassHead."\">".$this->_GraphTitle."</TD></TR>";
       
   904   }
       
   905   foreach($this->_values as $value) {
       
   906    if ($this->_ShowLabels) {
       
   907     echo "<tr>";
       
   908     echo "<td class=".$this->_TDClassLabel;
       
   909     if ($this->_ShowCountsMode>0) echo " colspan=2";
       
   910 	echo ">".$value["label"]."</TD></TR>";
       
   911    }	
       
   912    echo "<tr>";
       
   913    if ($this->_ShowCountsMode>0) {
       
   914     switch ($this->_ShowCountsMode) {
       
   915     case 1:
       
   916      $count = round(100*$value["value"]/$sumval)."%";
       
   917      break;
       
   918     case 2:
       
   919      $count = $value["value"];
       
   920      break;  /* Exit the switch and the while. */
       
   921     default:
       
   922      break;
       
   923     }
       
   924    echo "<td class=".$this->_TDClassCount.">$count</TD>";
       
   925    }
       
   926    echo "<td>";
       
   927    $height = $this->_BarWidth;
       
   928     $width=ceil($value["value"]*$this->_GraphWidth/$maxval);
       
   929    echo "<img SRC=\"".$this->_BarImg."\" height=$height width=$width ";
       
   930    echo "  style=\"border: ".$this->_BarBorderWidth."px solid ".$this->_BarBorderColor."\"";
       
   931    echo ">";
       
   932    echo "</TD></TR>";
       
   933   }
       
   934   echo "</TABLE>";
       
   935  }
       
   936 }
       
   937