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