By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Use PHP to do Line Graph

P: 24
Anyone have any idea on how to do a simple line graph using PHP codes?
Dec 26 '07 #1
Share this Question
Share on Google+
3 Replies

Expert 5K+
P: 6,050
Try the jpgraph library.

Hope that helps.

Regards, markus.
Dec 26 '07 #2

P: 24
How can i use something like this to do a line graph??? Anyone can give me any clue on that???

include_once 'db.php';

$qt=mysql_query("select * from gd_graph1 order by sales");

header ("Content-type: image/gif");

$x_gap=40; // The gap between each point in y axis

$x_max=$x_gap*13; // Maximum width of the graph or horizontal axis
$y_max=250; // Maximum hight of the graph or vertical axis
// Above two variables will be used to create a canvas of the image//

$im = @ImageCreate ($x_max, $y_max)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 234, 234, 234);
$text_color = ImageColorAllocate ($im, 233, 14, 91);
$graph_color = ImageColorAllocate ($im,25,25,25);

//echo "$nt[month], $nt[sales]";
echo $nt['month'];
echo $nt['sales'];
$x2=$x1+$x_gap; // Shifting in X axis
$y2=$y_max-$nt[sales]; // Coordinate of Y axis
//Line above is to print month names on the graph
if($first_one=="no"){ // this is to prevent from starting $x1= and $y1=0
imageline ($im,$x1, $y1,$x2,$y2,$text_color); // Drawing the line between two points
$x1=$x2; // Storing the value for next draw
$first_one="no"; // Now flag is set to allow the drawing

ImageJPEG ($im);

Dec 27 '07 #3

P: 11
Jpgraph is indeed created just to do this. The script you pasted is a Jpgraph script also.

Jpgraph is a sort of script/library that uses GD library (needs to be installed in php) to create graphs. It comes with a massive load of examples, which you can alter to create into something you like.
Jan 7 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.