Jump to content

real time graph plotting


mariam

Recommended Posts

i am trying to plot a graph taking values from a database table in my sql.

follwing is the code

 

<?php

 

$host="localhost"; // Host name

$username="root"; // Mysql username

$password=""; // Mysql password

$db_name="mehreen"; // Database name

 

include "db.php";

mysql_connect($host, $username, $password)or die("cannot connect");

mysql_select_db($db_name)or die("cannot select DB");

 

$qt=mysql_query("select * from gd_graph");

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

 

$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);

 

 

$x1=0;

$y1=0;

$first_one="yes";

while($nt=mysql_fetch_array($qt)){

//echo "$nt[month], $nt[sales]";

$x2=$x1+$x_gap; // Shifting in X axis

$y2=$y_max-$nt[sales]; // Coordinate of Y axis

ImageString($im,2,$x2,$y2,$nt[month],$graph_color);

//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

$y1=$y2;

$first_one="no"; // Now flag is set to allow the drawing

}

 

ImageJPEG ($im);

 

?>

 

 

the error occured is:

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\drawgraph.php:2) in C:\xampp\htdocs\drawgraph.php on line 15

Link to comment
https://forums.phpfreaks.com/topic/239411-real-time-graph-plotting/
Share on other sites

The headers already sent error can be caused having white space (extra blank spaces before or after the opening and closing PHP tags.

(<?php  ?>)

all headers should generally be placed at the top of the page to avoid this. Also if you have included another file that is declaring a header before line 15 then that will cause a problem.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.