TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-05-2007, 04:00 PM   #1 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default 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($image255255255);
$colorGrey=imagecolorallocate($image192192192);
$colorBlue=imagecolorallocate($image00255);
$colorBlack=imagecolorallocate($image000); 
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*250$i*25$imgWidth$colorGrey);
   
imageline($image0$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($image4$i*25 20,($imgWidth $Array[$i] - 25), "($Array_normal[$i])" ,$colorBlack);

Now we display the final result

PHP Code:
imagepng($image); 
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($image255255255);
$colorGrey=imagecolorallocate($image192192192);
$colorBlue=imagecolorallocate($image00255);
$colorBlack=imagecolorallocate($image000);

    
imagerectangle($image,0,1,$imgWidth-2,$imgHeight-2,$colorBlue);  

    for (
$i=1$i<$count+1$i++)
    {
        
imageline($image$i*250$i*25$imgWidth$colorGrey);
        
imageline($image0$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($image4$i*25 20,($imgWidth $Array[$i] - 25), "($Array_normal[$i])" ,$colorBlack);
    }
    
imagepng($image);

?>
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
The Following User Says Thank You to Rendair For This Useful Post:
Karl (12-05-2007)
Old 12-05-2007, 04:10 PM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

Nice idea to do a series, I'll look forward to reading them
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 08-28-2009, 08:04 AM   #3 (permalink)
The Wanderer
 
Join Date: May 2009
Posts: 6
Thanks: 0
RishikeshJha is on a distinguished road
Default

It's great. i m going to use this in my project.
RishikeshJha is offline  
Reply With Quote
Old 08-28-2009, 08:17 AM   #4 (permalink)
The Wanderer
 
Join Date: May 2009
Posts: 6
Thanks: 0
RishikeshJha is on a distinguished road
Default

one more thing can we use lables in this script???
RishikeshJha is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:21 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design