graphing/class.linegraph.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 LineGraph extends CGraph {
       
     5     var $line_color = array(39, 78, 120);
       
     6     var $line_bgcolor = array(69, 129, 194);
       
     7 
       
     8     function LineGraph() {
       
     9       $this->graph_width = $this->graph_padding['left'] + $this->graph_areawidth + $this->graph_padding['right'];
       
    10       $this->graph_height = $this->graph_padding['top'] + $this->graph_areaheight + $this->graph_padding['bottom'];
       
    11     }
       
    12     
       
    13     /**
       
    14      * Graph::SetLineColor()
       
    15      * Sets line color
       
    16      **/
       
    17     function SetLineColor($red, $green, $blue) {
       
    18       $this->line_color = array($red, $green, $blue);
       
    19     }
       
    20 
       
    21     /**
       
    22      * Graph::AddLineColor()
       
    23      * Sets line color
       
    24      **/
       
    25     function AddLineColor($red, $green, $blue) {
       
    26       if (!is_array($this->line_color[0])) {
       
    27         $this->line_color = array($this->line_color);
       
    28       }
       
    29       $this->line_color[] = array($red, $green, $blue);
       
    30     }
       
    31 
       
    32     /**
       
    33      * Graph::SetLineBackgroundColor()
       
    34      * Sets background color for line (when 3D)
       
    35      **/
       
    36     function SetLineBackgroundColor($red, $green, $blue) {
       
    37       $this->line_bgcolor = array($red, $green, $blue);
       
    38     }
       
    39     
       
    40     /**
       
    41      * Graph::AddLineBackgroundColor()
       
    42      * Sets line background color
       
    43      **/
       
    44     function AddLineBackgroundColor($red, $green, $blue) {
       
    45       if (!is_array($this->line_bgcolor[0])) {
       
    46         $this->line_bgcolor = array($this->line_bgcolor);
       
    47       }
       
    48       $this->line_bgcolor[] = array($red, $green, $blue);
       
    49     }
       
    50     
       
    51     /**
       
    52      * Graph::DrawGraph()
       
    53      * Draw all the graph: bg, axis, bars, text.. and output it
       
    54      * Optional file parameter turns output to file, and bool on success
       
    55      **/
       
    56     function DrawGraph($file = "") {
       
    57       $this->im = imagecreatetruecolor($this->graph_width, $this->graph_height);
       
    58       
       
    59       CGraph::DrawGraph();
       
    60       
       
    61       $this->__Draw_LeftBottom_Axis();
       
    62       
       
    63       $arrki = array_keys($this->data);
       
    64       if (is_array($this->data[$arrki[0]])) { // more than 1 line
       
    65         if (!is_array($this->line_color)) {
       
    66           $this->line_color = array($this->line_color);
       
    67         }
       
    68         if (!is_array($this->line_bgcolor)) {
       
    69           $this->line_bgcolor = array($this->line_bgcolor);
       
    70         }
       
    71         for ($i = 0; $i < count($arrki); $i++) {
       
    72           $this->__AllocateColor("im_line_color",
       
    73                                  $this->line_color[$i],
       
    74                                  $this->graph_transparencylevel,
       
    75                                  $i);
       
    76           if ($this->axis_deepness > 0) {
       
    77             $this->__AllocateColor("im_line_bgcolor",
       
    78                                    $this->line_bgcolor[$i],
       
    79                                    $this->graph_transparencylevel,
       
    80                                    $i);
       
    81           }
       
    82           $arrkj = array_keys($this->data[$arrki[$i]]);
       
    83           for ($j = 1; $j < count($arrkj); $j++) {
       
    84             $this->__DrawLine(array($arrkj[$j - 1],
       
    85                                     $arrkj[$j],
       
    86                                     $this->data[$arrki[$i]][$arrkj[$j - 1]],
       
    87                                     $this->data[$arrki[$i]][$arrkj[$j]]),
       
    88                               $this->im_line_color[$i],
       
    89                               $this->im_line_bgcolor[$i]);
       
    90           }
       
    91         }
       
    92       } else {
       
    93         $this->__AllocateColor("im_line_color",
       
    94                                $this->line_color,
       
    95                                $this->graph_transparencylevel);
       
    96         $this->__AllocateColor("im_line_bgcolor",
       
    97                                $this->line_bgcolor,
       
    98                                $this->graph_transparencylevel);
       
    99         for ($i = 1; $i < count($arrki); $i++) {
       
   100           $this->__DrawLine(array($i - 1, // x1
       
   101                                   $i,     // x2
       
   102                                   $this->data[$arrki[$i - 1]], // y1
       
   103                                   $this->data[$arrki[$i]]),    // y2
       
   104                             $this->im_line_color,
       
   105                             $this->im_line_bgcolor);
       
   106         }
       
   107         // exit;
       
   108       }
       
   109       
       
   110       $this->__Draw_TopRight_Axis();
       
   111       
       
   112       CGraph::DrawGraph2();
       
   113 
       
   114       if (strlen($file)) {
       
   115         $ret = imagepng($this->im, $file);
       
   116       } else {
       
   117         header("Content-Type: image/png"); // thanks to Marcin G. :)
       
   118         imagepng($this->im);
       
   119         $ret = true;
       
   120       }
       
   121       imagedestroy($this->im);
       
   122       return $ret;
       
   123     }
       
   124 
       
   125     /**
       
   126      * Graph::__DrawLine()
       
   127      * Draws a line between 2 points
       
   128      **/
       
   129     function __DrawLine($points, $color, $bgcolor) {
       
   130       if (!isset($this->line_unitX) || !isset($this->line_unitY)) {
       
   131         $this->line_unitX = ($this->graph_width - $this->graph_padding['left'] - $this->graph_padding['right']) / ($this->axis_maxX - $this->axis_minX);
       
   132         $this->line_unitY = $this->graph_areaheight / ($this->axis_maxY - $this->axis_minY);
       
   133       }
       
   134       $x1 = $this->graph_padding['left'] + floor(($points[0] - $this->axis_minX) * $this->line_unitX) + $this->line_unitX;
       
   135       $x2 = $this->graph_padding['left'] + floor(($points[1] - $this->axis_minX) * $this->line_unitX) + $this->line_unitX;
       
   136       $y1 = $this->graph_height - $this->graph_padding['bottom'] - floor(($points[2] - $this->axis_minY) * $this->line_unitY);
       
   137       $y2 = $this->graph_height - $this->graph_padding['bottom'] - floor(($points[3] - $this->axis_minY) * $this->line_unitY);
       
   138       // echo "drawing line from ($x1, $y1) to ($x2, $y2)<br />";
       
   139       if ($this->axis_deepness > 0) {
       
   140         $this->__DrawPolygon(array($x1, $y1,
       
   141                                    $x1 + $this->axis_deepness, $y1 - $this->axis_deepness,
       
   142                                    $x2 + $this->axis_deepness, $y2 - $this->axis_deepness,
       
   143                                    $x2, $y2),
       
   144                              $bgcolor,
       
   145                              true);
       
   146         $this->__DrawPolygon(array($x1, $y1,
       
   147                                    $x1 + $this->axis_deepness, $y1 - $this->axis_deepness,
       
   148                                    $x2 + $this->axis_deepness, $y2 - $this->axis_deepness,
       
   149                                    $x2, $y2),
       
   150                              $color);
       
   151       } else {
       
   152         imageline($this->im, $x1, $y1, $x2, $y2, $color);
       
   153       }
       
   154     }
       
   155 
       
   156     /**
       
   157      * Graph::__LoadLineValues()
       
   158      * Loads definitions to line settings
       
   159      **/
       
   160     function __LoadLineValues($data) {
       
   161       foreach ($data as $name => $value) {
       
   162         $name = strtolower($name);
       
   163         switch ($name) {
       
   164           case 'background-color':
       
   165             $this->__SetColorToValue("line_bgcolor", $value);
       
   166             break;
       
   167           case 'color':
       
   168             $this->__SetColorToValue("line_color", $value);
       
   169             break;
       
   170         }
       
   171       }
       
   172     }
       
   173   }
       
   174 ?>