Login   Register  
PHP Classes
elePHPant
Icontem

File: simplepie.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of LogaMurugan  >  Very simple pie chart  >  simplepie.php  >  Download  
File: simplepie.php
Role: Class source
Content type: text/plain
Description: simple pie chart
Class: Very simple pie chart
Render a pie chart image from a list of values
Author: By
Last change:
Date: 3 years ago
Size: 1,750 bytes
 

Contents

Class file image Download
<?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->image00$width$piewidthimagecolorallocate($this->image128128128));
        foreach(
$dataArr as $label=>$value) {
            
$angle_done    = ($value/$total) * 360/** angle calculated for 360 degrees */
            
$perc          round(($value/$total) * 1001); /** percentage calculated */
            
$color         imagecolorallocate($this->imagerand(0255), rand(0255), rand(0255));
            
imagefilledarc($this->image$x$y$piewidth$height$angle_start$angle_done+= $angle_start$colorIMG_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->image60$xtext$ytextimagecolorallocate($this->image000), $font"$perc %");
            
imagefilledrectangle($this->image$piewidth+2$ylegend$piewidth+20$ylegend+=20$color);
            
imagettftext($this->image80$piewidth+22$ylegendimagecolorallocate($this->image000), $font$label);
            
$ylegend += 4;
            
$angle_start $angle_done;
        }
    }
    function 
render()
    {
        
header('Content-type: image/png');
        
imagepng($this->image);
    }

?>