| Rendair |
12-05-2007 04:00 PM |
Line Chart in PHP
Hey all
Because i did a tutorial on how to make pie charts in PHP. I thought i would make a series of them lol. Next is how to make a simple line chart.
This line chart is very basic and may seem detailed less, but this will help you get a feel on how to start. I will later be creating more advance ones of each of these different type of graphs, so stay tuned :-)
You can view a demo HERE
Firstly i want to set up some arrays to hold our values. We are going to make 2 sets of arrays with the same results. This is because we will later change one set and keep the others the same for later use.
PHP Code:
$Array= array(0,80,23,1100,190,245,50,80,111,240,55);
$Array_normal = array(0,80,23,1100,190,245,50,80,111,240,55);
//We want to count how many arrays we have
$count = count($Array);
//Add all the values together
$arraySum = array_sum($Array);
Currently with this script we can only make the graph using 11 different figures. I will show you later how we can change this in a more advance tutorial.
Now we have done that, we want to set the image width and height
PHP Code:
$imgWidth=250; // Do not change
$imgHeight=250; //Do not change
For the purpose of this tutorial, do not change the values as it will cause problems. In the advance tutorial will go through this.
Now what we need to do is change the first set of values. These will be used to plot the graph. If we did not do this process we would have a problem. If we used the values we have in the array at the moment. One major error will occur. That is if any of the values are over 250 will mean the plot will appear outside of the actual image and because we have a value over a 1000 it will appear WAY outside the image.
We need to make these first set of numbers easier to plot and stay within out boundaries. We can do this by using the following code.
PHP Code:
for ($i=0; $i<=$count; $i++)
{
$Array[$i] = $Array[$i] / $arraySum * $imgHeight;
}
the code above will set the array values to a smaller number, but of course in comparisons these values would still plot the same as the normal ones would, but the new ones will stay in the image boundaries.
We need to now set the header as a image type
PHP Code:
header("Content-type: image/png");
And now we can start actually creating the colours we are going to use and also create the actual image the graph will show on.
PHP Code:
$image=imagecreate($imgWidth, $imgHeight);
$colorWhite=imagecolorallocate($image, 255, 255, 255);
$colorGrey=imagecolorallocate($image, 192, 192, 192);
$colorBlue=imagecolorallocate($image, 0, 0, 255);
$colorBlack=imagecolorallocate($image, 0, 0, 0);
We now create a border around the image to give it a nice effect.
PHP Code:
imagerectangle($image,0,1,$imgWidth-2,$imgHeight-2,$colorBlue);
Now its time to create the grid you see. We are going to use a for loop to create this grid.
PHP Code:
for ($i=1; $i<$count+1; $i++)
{
imageline($image, $i*25, 0, $i*25, $imgWidth, $colorGrey);
imageline($image, 0, $i*25, $imgHeight, $i*25, $colorGrey);
}
PHP Code:
imageline ( resource image,x-coordinate first point,y-coordinate first point,x-coordinate second point,y-coordinate second point,colour )
Now we have our grid, we can now plot the lines and place a text message that displayed the value of that point
PHP Code:
for ($i=0; $i<$count; $i++)
{
imageline($image, $i*25, ($imgWidth - $Array[$i]), ($i+1)*25, ($imgHeight - $Array[$i+1]), $colorBlue);
imagestring($image, 4, $i*25 - 20,($imgWidth - $Array[$i] - 25), "($Array_normal[$i])" ,$colorBlack);
}
Now we display the final result
This is a very basic looking line chart, but i will improve it in the next one. More advance :-)
Full Code:
PHP Code:
<?php
$Array= array(0,80,23,1100,190,245,50,80,111,240,55);
$Array_normal = array(0,80,23,1100,190,245,50,80,111,240,55);
$count = count($Array);
$arraySum = array_sum($Array);
$imgWidth=250;
$imgHeight=250;
for ($i=0; $i<=$count; $i++)
{
$Array[$i] = $Array[$i] / $arraySum * $imgHeight;
}
header("Content-type: image/png");
$image=imagecreate($imgWidth, $imgHeight);
$colorWhite=imagecolorallocate($image, 255, 255, 255);
$colorGrey=imagecolorallocate($image, 192, 192, 192);
$colorBlue=imagecolorallocate($image, 0, 0, 255);
$colorBlack=imagecolorallocate($image, 0, 0, 0);
imagerectangle($image,0,1,$imgWidth-2,$imgHeight-2,$colorBlue);
for ($i=1; $i<$count+1; $i++)
{
imageline($image, $i*25, 0, $i*25, $imgWidth, $colorGrey);
imageline($image, 0, $i*25, $imgHeight, $i*25, $colorGrey);
}
for ($i=0; $i<$count; $i++)
{
imageline($image, $i*25, ($imgWidth - $Array[$i]), ($i+1)*25, ($imgHeight - $Array[$i+1]), $colorBlue);
imagestring($image, 4, $i*25 - 20,($imgWidth - $Array[$i] - 25), "($Array_normal[$i])" ,$colorBlack);
}
imagepng($image);
?>
|