graphing/class.3dbargraph.php
changeset 43 ce2e9caf2dfa
equal deleted inserted replaced
42:c24a2311f232 43:ce2e9caf2dfa
       
     1 <?php
       
     2   require_once dirname(__FILE__) . "/class.graph.php";
       
     3 
       
     4   class BarGraph extends CGraph {
       
     5     var $bar_width = 32;
       
     6     var $bar_height = 8;
       
     7     var $bar_padding = 5;
       
     8     var $bar_bordercolor = array(39, 78, 120);
       
     9     var $bar_bgcolor = array(69, 129, 194);
       
    10     var $bar_textfont = 2;
       
    11 
       
    12     function BarGraph() {
       
    13       $this->axis_deepness = $this->bar_height;
       
    14       $this->graph_height = $this->graph_padding['top'] + $this->graph_areaheight + $this->graph_padding['bottom'];
       
    15     }
       
    16 
       
    17     /**
       
    18      * Graph::SetBarBorderColor()
       
    19      * Sets border color for bars
       
    20      **/
       
    21     function SetBarBorderColor($red, $green, $blue) {
       
    22       $this->bar_bordercolor = array($red, $green, $blue);
       
    23     }
       
    24 
       
    25     /**
       
    26      * Graph::SetBarBackgroundColor()
       
    27      * Sets background color for bars
       
    28      **/
       
    29     function SetBarBackgroundColor($red, $green, $blue) {
       
    30       $this->bar_bgcolor = array($red, $green, $blue);
       
    31     }
       
    32 
       
    33     /**
       
    34      * Graph::SetBarDimensions()
       
    35      * Sets with and height of each bar
       
    36      **/
       
    37     function SetBarDimensions($width, $height) {
       
    38       if ($width > 0) $this->bar_width = $width;
       
    39       if ($height >= 0) {
       
    40         $this->bar_height = $height;
       
    41         $this->axis_deepness = $height;
       
    42       }
       
    43     }
       
    44 
       
    45     /**
       
    46      * Graph::SetBarPadding()
       
    47      * Sets padding (border) around each bar
       
    48      **/
       
    49     function SetBarPadding($padding) {
       
    50       if ($padding > 0) $this->bar_padding = $padding;
       
    51     }
       
    52     function SetBarFont($font) {
       
    53       $this->bar_textfont = 0;
       
    54       switch ($font) {
       
    55         case 'x-large': $this->bar_textfont++;
       
    56         case 'large':   $this->bar_textfont++;
       
    57         case 'medium':  $this->bar_textfont++;
       
    58         case 'small':   $this->bar_textfont++;
       
    59         case 'x-small': $this->bar_textfont++; break;
       
    60         default:
       
    61           $this->bar_textfont = $font;
       
    62       }
       
    63     }
       
    64     function DrawGraph($file = "") {
       
    65       $this->graph_width = $this->graph_padding['left'] +
       
    66                            (count($this->data) * ($this->bar_width + ($this->bar_padding * 2))) +
       
    67                            $this->graph_padding['right'];
       
    68       $this->axis_deepness = $this->bar_height;
       
    69 
       
    70       $this->im = imagecreatetruecolor($this->graph_width, $this->graph_height);
       
    71 
       
    72       $this->axis_frontgridlines = 0;
       
    73       $this->axis_xscalevisible = 0;
       
    74       $this->axis_positions = array($this->axis_positions[0], 0, 0, 0);
       
    75 
       
    76       CGraph::DrawGraph();
       
    77       
       
    78       if ($this->axis_minY > 0) $this->axis_minY = 0;
       
    79       if ($this->axis_maxY < 0) $this->axis_maxY = 0;
       
    80 
       
    81       $this->__Draw_LeftBottom_Axis();
       
    82       
       
    83       $p = 0;
       
    84       foreach ($this->data as $name => $value) {
       
    85         $p++;
       
    86         $this->__DrawBarText($p, $name);
       
    87         $this->__DrawBar($p, $value);
       
    88       }
       
    89       
       
    90       $this->__Draw_TopRight_Axis();
       
    91 
       
    92       CGraph::DrawGraph2();
       
    93 
       
    94       if (strlen($file)) {
       
    95         $ret = imagepng($this->im, $file);
       
    96       } else {
       
    97         header("Content-Type: image/png"); // thanks to Marcin G. :)
       
    98         imagepng($this->im);
       
    99         $ret = true;
       
   100       }
       
   101       imagedestroy($this->im);
       
   102       return $ret;
       
   103     }
       
   104 
       
   105     /**
       
   106      * Graph::__DrawBarText()
       
   107      * Determines top and left to draw text to a choosen bar
       
   108      **/
       
   109     function __DrawBarText($bar, $text) {
       
   110       $this->__DrawText($text,
       
   111                         $this->graph_padding['left'] + (($this->bar_width + ($this->bar_padding * 2)) * ($bar - 0.5)),
       
   112                         $this->graph_height - $this->graph_padding['bottom'] + 1,
       
   113                         $this->im_axis_scalecolor,
       
   114                         $this->bar_textfont,
       
   115                         1);
       
   116     }
       
   117 
       
   118     /**
       
   119      * Graph::__DrawBar()
       
   120      * Draws a choosen bar with it's value
       
   121      **/
       
   122     function __DrawBar($bar, $value) {
       
   123       $x = $this->graph_padding['left'] +
       
   124            (($this->bar_width + ($this->bar_padding * 2)) * ($bar - 1)) +
       
   125            $this->bar_padding;
       
   126       $y = $this->graph_areaheight / ($this->axis_maxY - $this->axis_minY);
       
   127       if ($value >= 0) {
       
   128         $this->____DrawBar($x,
       
   129                            $this->graph_height - $this->graph_padding['bottom'] - abs(floor($this->axis_minY * $y)) - floor($y * $value),
       
   130                            $x + $this->bar_width,
       
   131                            $this->graph_height - $this->graph_padding['bottom'] - abs(floor($this->axis_minY * $y)));
       
   132       } else {
       
   133         $this->____DrawBar($x,
       
   134                            $this->graph_height - $this->graph_padding['bottom'] - abs(floor($this->axis_minY * $y)),
       
   135                            $x + $this->bar_width,
       
   136                            $this->graph_height - $this->graph_padding['bottom'] - floor($this->axis_minY * -$y) + floor($y * -$value));
       
   137       }
       
   138     }
       
   139     
       
   140     /**
       
   141      * Graph::____DrawBar()
       
   142      * Draws the actual rectangles that form a bar
       
   143      **/
       
   144     function ____DrawBar($x1, $y1, $x2, $y2) {
       
   145       $this->__AllocateColor("im_bar_bordercolor",
       
   146                              $this->bar_bordercolor,
       
   147                              ( $this->graph_transparencylevel * 1.5));
       
   148       $this->__AllocateColor("im_bar_bgcolor",
       
   149                              $this->bar_bgcolor,
       
   150                              ( $this->graph_transparencylevel * 1.5));
       
   151                              
       
   152       // base square
       
   153       $this->__DrawPolygon(array($x1, $y2, $x2, $y2, $x2 + $this->bar_height,
       
   154                                  $y2 - $this->bar_height, $x1 + $this->bar_height,
       
   155                                  $y2 - $this->bar_height),
       
   156                            $this->im_bar_bgcolor,
       
   157                            true);
       
   158       $this->__DrawPolygon(array($x1, $y2, $x2, $y2, $x2 + $this->bar_height,
       
   159                                  $y2 - $this->bar_height, $x1 + $this->bar_height,
       
   160                                  $y2 - $this->bar_height),
       
   161                            $this->im_bar_bordercolor);
       
   162 
       
   163       // back square
       
   164       $this->__DrawPolygon(array($x1 + $this->bar_height, $y1 - $this->bar_height,
       
   165                                  $x2 + $this->bar_height, $y1 - $this->bar_height,
       
   166                                  $x2 + $this->bar_height, $y2 - $this->bar_height,
       
   167                                  $x1 + $this->bar_height, $y2 - $this->bar_height),
       
   168                            $this->im_bar_bgcolor,
       
   169                            true);
       
   170       $this->__DrawPolygon(array($x1 + $this->bar_height, $y1 - $this->bar_height,
       
   171                                  $x2 + $this->bar_height, $y1 - $this->bar_height,
       
   172                                  $x2 + $this->bar_height, $y2 - $this->bar_height,
       
   173                                  $x1 + $this->bar_height, $y2 - $this->bar_height),
       
   174                            $this->im_bar_bordercolor);
       
   175 
       
   176       // left square
       
   177       $this->__DrawPolygon(array($x1, $y1, $x1 + $this->bar_height, $y1 - $this->bar_height,
       
   178                                  $x1 + $this->bar_height, $y2 - $this->bar_height,
       
   179                                  $x1, $y2),
       
   180                            $this->im_bar_bgcolor,
       
   181                            true);
       
   182       $this->__DrawPolygon(array($x1, $y1, $x2, $y1, $x2, $y2, $x1, $y2),
       
   183                            $this->im_bar_bordercolor);
       
   184 
       
   185       // front square
       
   186       $this->__DrawPolygon(array($x1, $y1, $x2, $y1, $x2, $y2, $x1, $y2),
       
   187                            $this->im_bar_bgcolor,
       
   188                            true);
       
   189       $this->__DrawPolygon(array($x1, $y1, $x2, $y1, $x2, $y2, $x1, $y2),
       
   190                            $this->im_bar_bordercolor);
       
   191                            
       
   192       // right square
       
   193       $this->__DrawPolygon(array($x2, $y2, $x2, $y1, $x2 + $this->bar_height,
       
   194                                  $y1 - $this->bar_height, $x2 + $this->bar_height,
       
   195                                  $y2 - $this->bar_height),
       
   196                            $this->im_bar_bgcolor,
       
   197                            true);
       
   198       $this->__DrawPolygon(array($x2, $y2, $x2, $y1, $x2 + $this->bar_height,
       
   199                                  $y1 - $this->bar_height, $x2 + $this->bar_height,
       
   200                                  $y2 - $this->bar_height),
       
   201                            $this->im_bar_bordercolor);
       
   202 
       
   203       // top square
       
   204       $this->__DrawPolygon(array($x1, $y1, $x2, $y1, $x2 + $this->bar_height,
       
   205                                  $y1 - $this->bar_height, $x1 + $this->bar_height,
       
   206                                  $y1 - $this->bar_height),
       
   207                            $this->im_bar_bgcolor,
       
   208                            true);
       
   209       $this->__DrawPolygon(array($x1, $y1, $x2, $y1, $x2 + $this->bar_height,
       
   210                                  $y1 - $this->bar_height, $x1 + $this->bar_height,
       
   211                                  $y1 - $this->bar_height),
       
   212                            $this->im_bar_bordercolor);
       
   213     }
       
   214     
       
   215     /**
       
   216      * Graph::__Load3dbarValues()
       
   217      * Loads definitions to 3d bar settings
       
   218      **/
       
   219     function __Load3dbarValues($data) {
       
   220       foreach ($data as $name => $value) {
       
   221         $name = strtolower($name);
       
   222         switch ($name) {
       
   223           case 'background-color':
       
   224             $this->__SetColorToValue("bar_bgcolor", $value);
       
   225             break;
       
   226           case 'border-color':
       
   227             $this->__SetColorToValue("bar_bordercolor", $value);
       
   228             break;
       
   229           case 'padding':
       
   230             $this->bar_padding = $value;
       
   231             break;
       
   232           case 'width':
       
   233             $this->bar_width = (int) $value;
       
   234             break;
       
   235           case 'height':
       
   236             $this->bar_height = (int) $value;
       
   237             $this->axis_deepness = (int) $value;
       
   238             break;
       
   239         }
       
   240       }
       
   241     }
       
   242   }
       
   243 ?>