In this small tutorial I will explain how I created simple bar chart using PHP. Maybe in future I will write more about charts and to expand this library. Tutorial is for beginners. For image manipulation and processing we will use GD library which is by default come's with PHP distributions. You need to enable GD2 extension in PHP.ini. Search for ";extension=php_gd2.dll" and remove ; character.
The code below calculate the heights for each value to be shown in chart, draws that bars and returns image data as variable.
Click here to view bar.php code
<?php
/*
* Gets maximal value from array and
* calculates ratio of chart height and value
*/
function maxRatio($values)
{
// get max value from array
$max = max($values);
// calculate ratio of chart height and max value
$max_ratio = 150/$max;
return $max_ratio;
}
/*
* Draws chart bar items
*/
function drawBarItems($values)
{
// Calculate ratio of height and max value
$max_ratio = maxRatio($values);
// Create image variable with given attributes
$barimg = imagecreate(399, 149);
// Set background to white
imagecolorallocate($barimg, 255, 255, 255);
// Set text color
$text_color = imagecolorallocate($barimg, 54, 255, 0);
// Set bar item color
$bar_color = imagecolorallocate($barimg, 0, 0, 0);
// Draw bars
$bar_width = 0;
foreach($values as $item => $value)
{
// chart height/item value
$item_ratio = 150/$value;
$max_item_ratio = $max_ratio/$item_ratio;
$item_height = 150*$max_item_ratio;
// Draw bar rectangle with given attributes
imagefilledrectangle($barimg,$bar_width,150-$item_height,$bar_width+25,149,$bar_color);
// Draw text with given color, coordinates...
imagestring($barimg, 1, $bar_width, 140, $item, $text_color);
// Change position for next bar item
$bar_width+=25;
}
return $barimg;
}
?>
I have been merged some part of chart functions into other file. Maybe in future I will write other chart tutorials, and combine with others.
Click here to view chart.php code
<?php
include('bar.php');
// Set content type to PNG
header('Content-type: image/png');
// Unserialize the array and call drawchart
drawChart(unserialize($_GET['values']));
/*
* Draw chart
*/
function drawChart($values)
{
// Create image var and set white background
$img = imagecreate(400, 150);
imagecolorallocate($img, 255, 255, 255);
// Draw coordinate lines
$line_color = imagecolorallocate($img, 0, 0, 0);
imageline($img, 0, 0, 0, 150, $line_color);
imageline($img, 0, 149, 400, 149, $line_color);
// Call bar type chart and draw it
$barItems = drawBarItems($values);
// Include bar chart in main image
imagecopy($img, $barItems, 1, 0, 0, 0, 399, 149);
// Output image
imagepng($img);
imagedestroy($img);
}
?>
The code above will be called with HTML <img/> tag. Chart.php applies the array and unserializes.
And here is example PHP file which set array with numbers and call's chart.php.
Click here to view example.php code
<?php
$values = array('First'=>1234, 'Second'=>3214,'Third'=>5321);
$values = serialize($values);
?>
<html>
<head>
<title>Example 1: Bar chart</title>
</head>
<body>
<img src="chart.php?values=<?php echo $values;?>"/>
</body>
</html>