Thursday, April 28, 2011

Use charts in PHP


I was struggling to use the charts in my PHP script so finally when I found out an easy way I thought of sharing it very everyone.

I am using the GD library available in php. Only thing that needs to be done is to configure the php.ini file.
Go to the php.ini file and enable extension=php_gd2.dll

Now to create the charts I was following the method as specified at http://www.qualitycodes.com/tutorial.php?articleid=20&title=How-to-create-bar-graph-in-PHP-with-dynamic-scaling

But the problem was that I was not able to write any text before or after the chart.

so

text
chart
text

the above kind of output is what I wanted and all I could get was a

Chart


So after doing some experiment I finally found a way

1. Create a file called imageGeneration.php
Create a funcction createImage($values,$imageName)
where $values is an associative array while $imageName is the name of the chart file.
The function will take the values to be plotted in the form of an associtive array
like
$values=array(
"Jan" => 110,
"Feb" => 130,
"Mar" => 215,
"Apr" => 81,
"May" => 310,
"Jun" => 110,
"Jul" => 190,
"Aug" => 175,
"Sep" => 390,
"Oct" => 286,
"Nov" => 150,
"Dec" => 196
);


So that the x axis is the name of the months and the y-axis is the values


function createImage($values,$imageName)
{

$img_width=450;
$img_height=300;
$margins=20;


# ---- Find the size of graph by substracting the size of borders
$graph_width=$img_width - $margins * 2;
$graph_height=$img_height - $margins * 2;
$img=imagecreate($img_width,$img_height);


$bar_width=20;
$total_bars=count($values);
$gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1);


# ------- Define Colors ----------------
$bar_color=imagecolorallocate($img,0,64,128);
$background_color=imagecolorallocate($img,240,240,255);
$border_color=imagecolorallocate($img,200,200,200);
$line_color=imagecolorallocate($img,220,220,220);

# ------ Create the border around the graph ------

imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color);
imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color);


# ------- Max value is required to adjust the scale -------
$max_value=max($values);
$ratio= $graph_height/$max_value;


# -------- Create scale and draw horizontal lines --------
$horizontal_lines=20;
$horizontal_gap=$graph_height/$horizontal_lines;

for($i=1;$i<=$horizontal_lines;$i++){
$y=$img_height - $margins - $horizontal_gap * $i ;
imageline($img,$margins,$y,$img_width-$margins,$y,$line_color);
$v=intval($horizontal_gap * $i /$ratio);
imagestring($img,0,5,$y-5,$v,$bar_color);

}


# ----------- Draw the bars here ------
for($i=0;$i< $total_bars; $i++){
# ------ Extract key and value pair from the current pointer position
list($key,$value)=each($values);
$x1= $margins + $gap + $i * ($gap+$bar_width) ;
$x2= $x1 + $bar_width;
$y1=$margins +$graph_height- intval($value * $ratio) ;
$y2=$img_height-$margins;
imagestring($img,0,$x1+3,$y1-10,$value,$bar_color);
imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color);
imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
}
$temp_chart_file_name = $imageName;//"s/chart2.png";

imagepng($img, $temp_chart_file_name,0);
return $imageName;
}

one important difference from the http://www.qualitycodes.com/tutorial.php?articleid=20&title=How-to-create-bar-graph-in-PHP-with-dynamic-scaling
is that I am not using the header("Content-type:image/png"); since my intention is not to print the chart from this function. The intention is to get the chart and save it in the file.



2. Now create another file called test.php that will use this function


include 'imageGeneration';

echo “ this is a sample test application for the chart”;

// supply the parameters to the chart
$values=array(
"Jan" => 510,
"Feb" => 130,
"Mar" => 215,
"Apr" => 81,
"May" => 310,
"Jun" => 110,
"Jul" => 190,
"Aug" => 175,
"Sep" => 390,
"Oct" => 286,
"Nov" => 150,
"Dec" => 196
);

$imageName="Chart.png";
createImage($values,$imageName);
?>

<img src=" <?php echo $imageName; ?>" alt="some_text"/>


echo “ the chart is placed”;
?>



You will get the output as
this is a sample test application for the chart
Image of the chart
the chart is placed

No comments: