graphing/class.piegraph.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 PieGraph extends CGraph {
       
     5     var $pie_color = array(array(39, 78, 120));
       
     6     var $pie_deepnesscolor = array(array(9, 48, 90));
       
     7     var $pie_bgcolor = array(array(69, 129, 194));
       
     8     var $pie_deepness = 10;
       
     9     var $pie_total = 0;
       
    10     var $pie_startoffset = 0;
       
    11 
       
    12     function Graph() {
       
    13       $this->graph_width = $this->graph_padding['left'] + $this->graph_areawidth + $this->graph_padding['right'];
       
    14       $this->graph_height = $this->graph_padding['top'] + $this->graph_areaheight + $this->graph_padding['bottom'];
       
    15     }
       
    16     
       
    17     function SetPieColor($red, $green, $blue) {
       
    18       $this->pie_color = array($red, $green, $blue);
       
    19     }
       
    20     function AddPieColor($red, $green, $blue) {
       
    21       if (!is_array($this->pie_color[0])) {
       
    22         $this->pie_color = array($this->pie_color);
       
    23       }
       
    24       $this->pie_color[] = array($red, $green, $blue);
       
    25     }
       
    26     function SetPieBackgroundColor($red, $green, $blue) {
       
    27       $this->pie_bgcolor = array($red, $green, $blue);
       
    28     }
       
    29     function AddPieBackgroundColor($red, $green, $blue) {
       
    30       if (!is_array($this->pie_bgcolor[0])) {
       
    31         $this->pie_bgcolor = array($this->pie_bgcolor);
       
    32       }
       
    33       $this->pie_bgcolor[] = array($red, $green, $blue);
       
    34     }
       
    35     function SetPieDeepnessColor($red, $green, $blue) {
       
    36       $this->pie_deepnesscolor = array($red, $green, $blue);
       
    37     }
       
    38     function AddPieDeepnessColor($red, $green, $blue) {
       
    39       if (!is_array($this->pie_deepnesscolor[0])) {
       
    40         $this->pie_deepnesscolor = array($this->pie_deepnesscolor);
       
    41       }
       
    42       $this->pie_deepnesscolor[] = array($red, $green, $blue);
       
    43     }
       
    44     function SetPieTotalValue($total) {
       
    45       $this->pie_total = $total;
       
    46     }
       
    47     function SetPieStartOffset($offset) {
       
    48       if ($offset < 0 || $offset > 359) $offset = 0;
       
    49       $this->pie_startoffset = $offset;
       
    50     }
       
    51     function SetPieData($data) {
       
    52       CGraph::SetData($data);
       
    53     }
       
    54     function DrawGraph($file = "") {
       
    55       $this->im = imagecreatetruecolor($this->graph_width, $this->graph_height);
       
    56 
       
    57       $this->axis_positions = array(0, 0, 0, 0);
       
    58       $this->axis_xscalevisible = 0;
       
    59       $this->axis_yscalevisible = 0;
       
    60       $this->axis_gridlines = 0;
       
    61       
       
    62       CGraph::DrawGraph();
       
    63       
       
    64       if ($this->pie_total == 0) {
       
    65         foreach ($this->data as $name => $value) {
       
    66           $this->pie_total += $value;
       
    67         }
       
    68       }
       
    69       // deepness
       
    70       for ($i = $this->pie_deepness; $i > 0; $i--) {
       
    71         $offset = 0;
       
    72         $p = 0;
       
    73         foreach ($this->data as $n => $value) {
       
    74           if (!$this->pie_deepnesscolor[$p]) {
       
    75             $this->__AllocateColor("im_pie_deepnesscolor", $this->pie_deepnesscolor[$p], 0, $p);
       
    76           }
       
    77           $from = round($this->pie_startoffset - ($offset * 360 / $this->pie_total));
       
    78           $to = round($this->pie_startoffset - (($value + $offset) * 360 / $this->pie_total));
       
    79           if ($from < 0) $from += 360;
       
    80           if ($to < 0) $to += 360;
       
    81           imagefilledarc($this->im, round($this->graph_width / 2), round($this->graph_height / 2) + $i,
       
    82                                     $this->graph_areawidth, $this->graph_areaheight,
       
    83                                     $to, $from, $this->pie_deepnesscolor[$p], IMG_ARC_PIE);
       
    84           $offset += $value;
       
    85           $p++;
       
    86         }
       
    87       }
       
    88       $offset = 0;
       
    89       $p = 0;
       
    90       foreach ($this->data as $n => $value) {
       
    91         $this->__AllocateColor("im_pie_color", $this->pie_color[$p], 0, $p);
       
    92 
       
    93         $from = round($this->pie_startoffset - ($offset * 360 / $this->pie_total));
       
    94         $to = round($this->pie_startoffset - (($value + $offset) * 360 / $this->pie_total));
       
    95         if ($from < 0) $from += 360;
       
    96         if ($to < 0) $to += 360;
       
    97         imagefilledarc($this->im, round($this->graph_width / 2), round($this->graph_height / 2),
       
    98                                   $this->graph_areawidth, $this->graph_areaheight,
       
    99                                   $to, $from, $this->im_pie_color[$p], IMG_ARC_PIE);
       
   100         $offset += $value;
       
   101         $p++;
       
   102       }
       
   103 
       
   104       CGraph::DrawGraph2();
       
   105 
       
   106       if (strlen($file)) {
       
   107         $ret = imagepng($this->im, $file);
       
   108       } else {
       
   109         header("Content-Type: image/png"); // thanks to Marcin G. :)
       
   110         imagepng($this->im);
       
   111         $ret = true;
       
   112       }
       
   113       imagedestroy($this->im);
       
   114       return $ret;
       
   115     }
       
   116     
       
   117     function __DrawPieSlice($frompercent, $topercent, $color, $deepcolor) {
       
   118       $from = round(270 - ($frompercent * 3.6));
       
   119       $to = round(270 - ($topercent * 3.6));
       
   120       if ($from < 0) $from += 360;
       
   121       if ($to < 0) $to += 360;
       
   122       //echo "FROM:" . $from . " TO:" . $to . "<br>\n";
       
   123       for ($i = $this->pie_deepness; $i > 0; $i--) {
       
   124         imagefilledarc($this->im, round($this->graph_width / 2), round($this->graph_height / 2) + $i,
       
   125                                   round($this->graph_areawidth / 2), round($this->graph_areaheight / 2),
       
   126                                   $to, $from, $deepcolor, IMG_ARC_PIE);
       
   127       }
       
   128       imagefilledarc($this->im, round($this->graph_width / 2), round($this->graph_height / 2),
       
   129                                 round($this->graph_areawidth / 2), round($this->graph_areaheight / 2),
       
   130                                 $to, $from, $color, IMG_ARC_PIE);
       
   131     }
       
   132 
       
   133     /**
       
   134      * Graph::__LoadPieValues()
       
   135      * Loads definitions to pie settings
       
   136      **/
       
   137     function __LoadPieValues($data) {
       
   138       foreach ($data as $name => $value) {
       
   139         $name = strtolower($name);
       
   140         switch ($name) {
       
   141           case 'background-color':
       
   142             $this->__SetColorToValue("pie_bgcolor", $value);
       
   143             break;
       
   144           case 'color':
       
   145             $this->__SetColorToValue("pie_color", $value);
       
   146             break;
       
   147           case 'deepness-color':
       
   148             $this->__SetColorToValue("pie_depnesscolor", $value);
       
   149             break;
       
   150           case 'offset':
       
   151             $this->SetPieStartOffset($value);
       
   152             break;
       
   153           case 'total':
       
   154             $this->SetPieTotalValue($value);
       
   155         }
       
   156       }
       
   157     }
       
   158   }
       
   159 ?>