PHP Bar chart

PHP Bar chart

PHP Bar Chart : -

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

create piechart.php and put this code


<?php
//define--fetch data from FORM
$result=array('maths'=>100,'physics'=>50,'chem'=>90,'eng'=>60,'hindi'=>80);

$w=600;
$h=480;
$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);

imageline($c,40,40,40,$h-40,$black);
imageline($c,40,$h-40,$w-40,$h-40,$black);
$i=0;
$bw=40;
$bg=60;
//add string to graphics - typography
//imagestring($c,$fontsize_font,$x1,$y1,$string,$color);
//fontsize is 1 to 5
//font refers gdf fonts
imagestring($c,5,230,20,"Pariksha Fal 2020",$black);
foreach($result as $s=>$m)
{
$x1=40+$bg+($bw+$bg)*$i;
$x2=$x1+$bw;

/*
pixel per mark=4

100marks        y1=40
99marks        y1=44
98marks        y1=48
90marks        y1=40+(100-90)*4
n marks        y1=40+(100-n)*4

*/

$y1=40+(100-$m)*4;    //changed as per marks
$y2=$h-40;
imagefilledrectangle($c,$x1,$y1,$x2,$y2,$rang[$i]);
imagestring($c,5,$x1+10,$y1-14,$m,$black);
imagestring($c,5,$x1,$y2+4,$s,$black);

$i++;
}
$n=100;
for($i=0;$i<=400;$i+=4)
{
if($i%40==0){
imagestring($c,5,10,32+$i,$n,$black);
$n-=10;
    }
else    {
imagestring($c,5,32,32+$i,'-',$black);
    }
}


header("Content-type:image/jpg");
imagejpeg($c);
imagedestroy($c);
?>

 

smileysmileysmileysmileysmiley

Download Here

Previous Post

PHP Pie Chart

Total Coments (0)