PHP Pie Chart

PHP Pie Chart

PHP Pie Chart : -

In this post you learn how to make pie chart in php, Basically we using core php graphics function and gd library function for making pie chart.

create piechart.php and put this code


<?php
$w=600;
$h=360;
$c=imagecreate($w,$h);
$yellow=imagecolorallocate($c,255,255,128);

$black=imagecolorallocate($c,0,0,0);
$red=imagecolorallocate($c,255,0,0);
$green=imagecolorallocate($c,0,255,0);
$orange=imagecolorallocate($c,255,128,0);
$blue=imagecolorallocate($c,0,0,255);
$brown=imagecolorallocate($c,128,64,64);
$rang=array($red,$green,$orange,$blue,$brown);
    /*include variables/functions/code from another file - like a header file*/

//imagefilledarc($c,200,200,300,300,90,180,$red,0);
//imagefiledarc($canvas,$center_x,$center_y,$e_width,$e_height,$start_a,$end_a,$style);


$result=array('vary good party'=>4,'good party'=>1,'bad party'=>1,'don party'=>1,'danger party'=>1);

$ts=0;
foreach($result as $p=>$s)
$ts+=$s;

$dps=360/$ts;
$sa=0;
$i=0;
$x1=400;
imagestring($c,5,200,20,'election result 2017',$black);
foreach($result as $p=>$s)
{
$ea=$sa+$s*$dps;
imagefilledarc($c,200,200,300,300,$sa,$ea,$rang[$i],0);
$y1=100+40*$i;
imagefilledrectangle($c,$x1,$y1,$x1+10,$y1+10,$rang[$i]);
$data="$p ($s)";
imagestring($c,5,$x1+20,$y1,$data,$black);

$i++;
$sa=$ea;
}
imagerectangle($c,0,0,$w-1,$h-1,$red);
header("Content-type:image/png");
imagepng($c);
imagedestroy($c);
?>

Download Here

Previous Post

PHP Ajax Auto Search

Total Coments (0)