note 102337 modified in function.imagefilledarc by thiago

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
A simple pie chart generation script. 
Displays the percentage at center arc and displays the Legends with random colors. 

<?php
class simplepie
{
    function __construct($width, $height, $dataArr)
    {
        $font = './verdana.ttf'; /** get it from c:/windows/fonts dir */
        $this->image = imagecreate($width,$height);
        $piewidth = $width * 0.70;/* pie area */
        $x = round($piewidth/2);
        $y = round($height/2);
        $total = array_sum($dataArr);
        $angle_start = 0;
        $ylegend = 2;
        imagefilledrectangle($this->image, 0, 0, $width, $piewidth, imagecolorallocate($this->image, 128, 128, 128));
        foreach($dataArr as $label=>$value) {
            $angle_done    = ($value/$total) * 360; /** angle calculated for 360 degrees */
            $perc          = round(($value/$total) * 100, 1); /** percentage calculated */
            $color         = imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
            imagefilledarc($this->image, $x, $y, $piewidth, $height, $angle_start, $angle_done+= $angle_start, $color, IMG_ARC_PIE);
            $xtext = $x + (cos(deg2rad(($angle_start+$angle_done)/2))*($piewidth/4));
            $ytext = $y + (sin(deg2rad(($angle_start+$angle_done)/2))*($height/4)); 
            imagettftext($this->image, 6, 0, $xtext, $ytext, imagecolorallocate($this->image, 0, 0, 0), $font, "$perc %");
            imagefilledrectangle($this->image, $piewidth+2, $ylegend, $piewidth+20, $ylegend+=20, $color); 
            imagettftext($this->image, 8, 0, $piewidth+22, $ylegend, imagecolorallocate($this->image, 0, 0, 0), $font, $label);
            $ylegend += 4;
            $angle_start = $angle_done;
        }
    }
    function render()
    {
        header('Content-type: image/png');
        imagepng($this->image);
    }
}
/** usage */
$dataArr = array(2001=>10, 2002=>30, 2003=>50, 2004=>10);
$width=600;
$height=480;
$pie = new simplepie($width, $height, $dataArr);
$pie->render();
?>

--was--
A simple pie chart generation script. 
Displays the percentage at center arc and displays the Legends with random colors. 

class simplepie
{
    function __construct($width, $height, $dataArr)
    {
        $font = './verdana.ttf'; /** get it from c:/windows/fonts dir */
        $this->image = imagecreate($width,$height);
        $piewidth = $width * 0.70;/* pie area */
        $x = round($piewidth/2);
        $y = round($height/2);
        $total = array_sum($dataArr);
        $angle_start = 0;
        $ylegend = 2;
        imagefilledrectangle($this->image, 0, 0, $width, $piewidth, imagecolorallocate($this->image, 128, 128, 128));
        foreach($dataArr as $label=>$value) {
            $angle_done    = ($value/$total) * 360; /** angle calculated for 360 degrees */
            $perc          = round(($value/$total) * 100, 1); /** percentage calculated */
            $color         = imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
            imagefilledarc($this->image, $x, $y, $piewidth, $height, $angle_start, $angle_done+= $angle_start, $color, IMG_ARC_PIE);
            $xtext = $x + (cos(deg2rad(($angle_start+$angle_done)/2))*($piewidth/4));
            $ytext = $y + (sin(deg2rad(($angle_start+$angle_done)/2))*($height/4)); 
            imagettftext($this->image, 6, 0, $xtext, $ytext, imagecolorallocate($this->image, 0, 0, 0), $font, "$perc %");
            imagefilledrectangle($this->image, $piewidth+2, $ylegend, $piewidth+20, $ylegend+=20, $color); 
            imagettftext($this->image, 8, 0, $piewidth+22, $ylegend, imagecolorallocate($this->image, 0, 0, 0), $font, $label);
            $ylegend += 4;
            $angle_start = $angle_done;
        }
    }
    function render()
    {
        header('Content-type: image/png');
        imagepng($this->image);
    }
}
/** usage */
$dataArr = array(2001=>10, 2002=>30, 2003=>50, 2004=>10);
$width=600;
$height=480;
$pie = new simplepie($width, $height, $dataArr);
$pie->render();

http://php.net/manual/en/function.imagefilledarc.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.