Location: PHPKode > scripts > Graph classes > graph-classes/Linechart.php
<?php
class Linechart extends Graph {
        var $arrays = array();
        var $colors = array();
        var $img_w = 100;
        var $img_h = 100;
        var $bgcolor='';
        var $arr_counter = 0;

        function Linechart($img_w=100,$img_h=100,$bgcolor='') {
                $this->img_w = $img_w;
                $this->img_h = $img_h;
                if (!empty($bgcolor)) {
                        $this->bgcolor = $bgcolor;
                }
                else {
                        $this->bgcolor = new Color(0,0,255);
                }
        }

        function addArray($val=array(),$color) {
                $this->arr_counter++;
                $this->arrays[$this->arr_counter] = array();
                $this->arrays[$this->arr_counter] = $val;
                $this->colors[$this->arr_counter] = $color;
        }

        function createImage() {
                //ÉÝÅÍ ËÏÌÉÞÅÓÔ×Ï ÔÏÞÅË × ÍÁÓÓÉ×ÁÈ
                reset($this->arrays);
                error_reporting(63);
                $cnmax = 0;
                $ar = $this->arrays[$this->arr_counter];
                $min = $ar[0];
                $max = $ar[0];
                while(list($key,$val)=each($this->arrays)) {
                        $tt = count($val);
                        if ($tt > $cnmax) { $cnmax = $tt; }
                        $armax = $val[0];
                        $armin = $val[0];
                        while (list($key1,$val1)=each($val)) {
                                if ($val1 > $armax) { $armax = $val1; }
                                if ($val1 < $armin) { $armin = $val1; }
                        }
                        if ($armax > $max) { $max = $armax; }
                        if ($armin < $min) { $min = $armin; }
                }

                $kx = $this->img_w/$cnmax;
                $ky = $this->img_h/(abs($max)+abs($min));

                $cx = floor($this->img_w/2);
                $cy = floor($this->img_h/2);
                $im = ImageCreate($this->img_w,$this->img_h);

                $blue = ImageColorAllocate($im,0,0,255);
                $white = ImageColorAllocate($im,255,255,255);
                $bgc = ImageColorAllocate($im,$this->bgcolor->R,$this->bgcolor->G,$this->bgcolor->B);

                ImageColorTransparent($im,$bgc);
                ImageFill($im,$cx,$cy,$bgc);
                ImageRectangle($im,0,0,$this->img_w-1,$this->img_h-1,$blue);

                $ay = abs($max);
                // ÏÓØ x
                ImageLine($im,0,$ay*$ky,$this->img_w-1,$ay*$ky,$blue);

                //ÒÉÓÕÅÍ ÔÏÞËÉ
                reset($this->arrays);
                while(list($key,$val)=each($this->arrays)) {
                        $i = 0;
                        reset($val);
                        $prx = 0;
                        $pry = $ay*$ky;
                        $val_cl = $this->colors[$key];
                        while (list($key1,$val1)=each($val)) {
                                $i++;
                                $px = floor($i*$kx);
                                $py = floor($ky*($ay-$val1));
                                $color = ImageColorAllocate($im,$val_cl->R,$val_cl->G,$val_cl->B);
                                ImageSetPixel($im,$px,$py,$blue);
                                ImageLine($im,$prx,$pry,$px,$py,$color);
                                #$i++;
                                $prx = $px;
                                $pry = $py;
                        }
                }               

                $this->image = $im;
                return $im;
        }
}
?>
Return current item: Graph classes