Bar chart using php

by Mohan 2014-03-25 13:06:57


Bar chart using simple gd functions php


Simple gd function to create bar charts.
create a php file and paste the below code



class BarGraph
{
var $barWidth;
var $imgHeight=400;
var $imgWidth=600;
var $bgColor,$barColor;
var $barPadding;
var $data,$rangeMax=10;
var $im;

function init() /* initializes the image */
{
$this->im=imagecreate($this->imgWidth,$this->imgHeight);
}

function setHeightWidth($h,$w) /** sets the hieght and with of the image **/
{
$this->imgHeight=$h;
$this->imgWidth=$w;
}

function setBarWidth($width) /* sets the bar width */
{
$this->barWidth=$width;
}

function setBarPadding($padding) /* sets the bar padding */
{
$this->barPadding=$padding;
}

function setMax($max) /* sets the maximum posible value in the data set */
{
$this->rangeMax=$max;
}

function loadData($data) /* load data, the input shud be an array */
{
$this->data=$data;
}

function setBgColor($r,$g,$b) /* sets the background color of the image */
{
$this->bgColor=imagecolorallocate($this->im,$r,$g,$b);
}

function setBarColor($r,$g,$b) /* sets the bar color of the image */
{
$this->barColor=imagecolorallocate($this->im,$r,$g,$b);
}

function drawGraph($flag=0) /* to draw graphs on the image */
{
if($flag) /* flag set to 1 to draw the second bar **/
{
$t=$this->barWidth+$this->barPadding;
}
else /* else draws the first bar set */
{
imagefilledrectangle($this->im,0,0,$this->imgWidth,$this->imgHeight,$this->bgColor);
$t=0;
}

for ( $mon = 0 ; $mon < count($this->data) ; $mon ++ )
{

$X = (($this->imgWidth/count($this->data))*$mon) + $this->barPadding + $t;
$Y = (10 - $this->data[$mon])*($this->imgHeight/$this->rangeMax);
$X1 = ($X + $this->barWidth);
$Y1 = $this->imgHeight;

imagefilledrectangle($this->im,$X,$Y,$X1,$Y1,$this->barColor);
}
}

function renderImage() /* creates the image & sends in to the browser */
{
header("Content-Type: image/png");
imagepng($this->im);
}
} ?>


Create another file and include the above class file.

include("Bargraph.php");

$pr=array(3,6,7,4,5,9,4,5,4,2,1,8 );
$p=$pr;
shuffle($p);

$g=new BarGraph;
$g->setHeightWidth(400,600);
$g->init();
$g->setMax(10); //maximum data possible in the data set
$g->setBarWidth(10);
$g->setBarPadding(10);
$g->setBarColor(255,130,130);
$g->setBgColor(204,204,204);
$g->loadData($pr);
$g->drawGraph();

$g->loadData($p);
$g->setBarColor(130,130,255);
$g->drawGraph(1);

$g->renderImage();
?>


sample.png
1158
like
0
dislike
0
mail
flag

You must LOGIN to add comments