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

        function Barchart($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);
                // ËÏÅÆÆÉÃÉÅÎÔ ÒÁÓÔÑÇÁ
                $k = 3;
                // ÒÁÓÓÔÏÑÎÉÅ ÍÅÖÄÕ ÓÔÏÌÂÃÁÍÉ
                $dbs = 4;

                $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; }
                }

                // ÛÉÒÉÎÁ ÓÔÏÌÂÃÁ
                $bw = ($this->img_w-$dbs*($this->arr_counter-1)*$cnmax)/($k*($cnmax+1)+$this->arr_counter*$cnmax);

                $kx = ($this->img_w-$this->arr_counter*($dbs+$k*$bw))/($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);
                $db=0;
                while(list($key,$val)=each($this->arrays)) {
                        $i = 0;
                        reset($val);
                        $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);
                                ImageRectangle($im,$px+$db,$ay*$ky,$px+$bw+$db,$py,$color);
                                ImageFill($im,floor($px+$db+$bw/2),floor($ky*($ay-$val1/2)),$color);
                                //ImageSetPixel($im,$px,$py,$color);
                        }
                        $db += ($bw+$dbs);
                }               

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