Location: PHPKode > scripts > Graph classes > graph-classes/Piechart.php
<?php

class Piechart extends Graph {

        var $segmentkeys = array();

        var $segmentvals = array();

        var $img_w = 100;

        var $img_h = 100;

        var $radius = 50;

        var $bgcolor;



        function Piechart($rad,$bgcolor='') {

                $this->img_w = $rad*2;

                $this->img_h = $rad*2;

                $this->radius = $rad;

                if (!empty($bgcolor)) {

                        $this->bgcolor = $bgcolor;

                }

                else {

                        $this->bgcolor = new Color(0,0,255);

                }

        }



        function addSegment($val=0,$color) {

                $this->segmentkeys[sizeof($this->segmentkeys)]=$val;

                $this->segmentvals[sizeof($this->segmentvals)]=$color;

        }



        function createImage() {

                $total = 0;

                reset($this->segmentkeys);

                while(list($key,$val)=each($this->segmentkeys)) {

                                                $total += $val;

                }



                $cx = floor($this->img_w/2);

                $cy = floor($this->img_h/2);



                #$kx = $this->img_w/$cnmax;

                #$ky = $this->img_h/(abs($max)+abs($min));



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

                ImageArc($im,$cx,$cy,$this->img_w,$this->img_h,0,360,$blue);

                ImageLine($im,$cx,$cy,$cx+$this->img_w,$cy,$blue);



                reset($this->segmentkeys);

                $prev = 0;

                $sm = 0;

                for ($i=0; $i<sizeof($this->segmentkeys); $i++) {

                                                $key=$this->segmentkeys[$i];

                                                $val=$this->segmentvals[$i];

                        // ÐÒÏÃÅÎÔÎÏÅ ÓÏÏÔÎÏÛÅÎÉÅ

                        $percent = $key/$total*100;

                        // ÕÇÏÌ

                        $angle = (360/100)*$percent;

                        $sm += $angle;

                        // ÕÇÏÌ × ÒÁÄÉÁÎÁÈ

                        $rad = deg2rad($sm);

                        $dx = ($this->radius) * cos($rad);

                        $dy = ($this->radius) * sin($rad);

                        ImageLine($im,$cx,$cy,$cx+$dx,$cy-$dy,$blue);

                        $gamma = $rad - $prev;

                        $delta = $prev + $gamma/2;

                        $dxp = ($this->radius/2) * cos($delta);

                        $dyp = ($this->radius/2) * sin($delta);

                        $color = ImageColorAllocate($im,$val->R,$val->G,$val->B);

                        ImageFill($im,$cx+$dxp,$cy-$dyp,$color);

                        $prev = $rad;

                }

                $this->image = $im;

                return $im;

        }

}

?>

Return current item: Graph classes