16. august 2006 - 10:05
Der er
27 kommentarer
X og Y axis on the PHP Graph !
Hello friends, Im posting again the same question again .. and asking for help please help me. here is the graof code i made ... <?php header("Content-type: Image/png"); ob_start(); session_start(); $result = mysql_query("SELECT * FROM skema where s_elev =".$_SESSION['userid']); while ($row= mysql_fetch_assoc($result)) { $values[] = $row["skr_totalpoint"]; $columns = count($values); $width = 250; $height = 300; $padding = 10; $column_width = $width / $columns ; $im = imagecreate($width,$height); $gray = imagecolorallocate ($im,0xcc,0xcc,0xcc); $gray_lite = imagecolorallocate ($im,0xee,0xee,0xee); $gray_dark = imagecolorallocate ($im,0x7f,0x7f,0x7f); $white = imagecolorallocate ($im,0xff,0xff,0xff); imagefilledrectangle($im,0,0,$width,$height,$white); $maxv = 0; $maxv = max($values); for($i=0;$i<$columns;$i++) { $column_height = ($height / 100) * (( $values[$i] / $maxv) *100); $x1 = $i*$column_width; $y1 = $height-$column_height; $x2 = (($i+1)*$column_width)-$padding; $y2 = $height; imagefilledrectangle($im,$x1,$y1,$x2,$y2,$gray); imageline($im,$x1,$y1,$x1,$y2,$gray_lite); imageline($im,$x1,$y2,$x2,$y2,$gray_lite); imageline($im,$x2,$y1,$x2,$y2,$gray_dark); } imagepng($im); imagedestroy($im); ?> please help me ... I need the x and Y lines with text who can describe what that graph showing. I have ganerate the by selv havt used any kind of software. Please kindly help me. Thanks in advance.. can contect me on sweetzoha@msn.com.
Annonceindlæg fra Infor
16. august 2006 - 11:04
#1
Try: imagestring()
16. august 2006 - 11:07
#2
<?php header("Content-type: Image/png"); ob_start(); session_start(); $result = mysql_query("SELECT * FROM skema where s_elev =".$_SESSION['userid']); while ($row= mysql_fetch_assoc($result)) { $values[] = $row["skr_totalpoint"]; $columns = count($values); $width = 250; $height = 300; $margin = 100; $padding = 10; $column_width = $width / $columns ; $im = imagecreate($width+$margin,$height+$margin); $gray = imagecolorallocate ($im,0xcc,0xcc,0xcc); $gray_lite = imagecolorallocate ($im,0xee,0xee,0xee); $gray_dark = imagecolorallocate ($im,0x7f,0x7f,0x7f); $white = imagecolorallocate ($im,0xff,0xff,0xff); imagefilledrectangle($im,0,0,$width+$margin,$height+$margin,$white); $maxv = 0; $maxv = max($values); for($i=0;$i<$columns;$i++) { $column_height = ($height / 100) * (( $values[$i] / $maxv) *100); $x1 = $i*$column_width+$margin/2 + 5; $y1 = $height-$column_height + $margin/2; $x2 = (($i+1)*$column_width)-$padding+$margin/2 + 5; $y2 = $height + $margin/2; imagefilledrectangle($im,$x1,$y1,$x2,$y2,$gray); imageline($im,$x1,$y1,$x1,$y2,$gray_lite); imageline($im,$x1,$y2,$x2,$y2,$gray_lite); imageline($im,$x2,$y1,$x2,$y2,$gray_dark); } imageline($im,$margin/2,$margin/2,$margin/2,$margin/2+$height,0); imageline($im,$margin/2,$margin/2 + $height,$width+$margin/2,$margin/2 + $height,0); $parts = 5; $vPrPart = $maxv/$parts; $yPart = $height/$parts; for($i=0;$i<=$parts;$i++){ imagestring($im,1,$margin/4,$height - $i*$yPart - 5 + $margin/2,$i*$vPrPart,0); } imagestring($im,1,$margin/4,$margin/4,"Value",0); imagestring($im,1,$width + $margin - $margin/2,$height + $margin/2 + $margin/4,"Students",0); imagepng($im); imagedestroy($im); ?>
16. august 2006 - 11:37
#3
Haven't you hag an error in line nr. 10 - you have to end the while-loop with { like: while ($row= mysql_fetch_assoc($result)) { $values[] = $row["skr_totalpoint"]; } in my test vith values like: $values[] = 18; $values[] = 28; $values[] = 38; $values[] = 48; $values[] = 58; $values[] = 68; $values[] = 78; $values[] = 88; $values[] = 98; these values makes tho x and y axes work...
16. august 2006 - 12:08
#4
waoo thanks friends it helps a lot ... now i have a question more in same program .. i will write in detail soon. but can i contect u people some other way in that ekperten.. means by on msn or mail??
16. august 2006 - 12:24
#5
Just do, as you allready are doing... give the experts some code, and lets work built out from that - this is a open forum, and other people might learn from it... I can see, that you know much more about generating pictures, than I do, and other users might help you with ex. putting text in the picture etc. and I might learn about it too... Just carry on.
16. august 2006 - 12:44
#6
yes sure but i need a bit description of that code which is added in my code .. so i know that which line doing what in that... im adding code again so plss make a bit text on that for further user too ......... imageline($im,$margin/2,$margin/2,$margin/2,$margin/2+$height,0); imageline($im,$margin/2,$margin/2 + $height,$width+$margin/2,$margin/2 + $height,0); $parts = 5; $vPrPart = $maxv/$parts; $yPart = $height/$parts; for($i=0;$i<=$parts;$i++) { imagestring($im,1,$margin/4,$height - $i*$yPart - 5 + $margin/2,$i*$vPrPart,0); } imagestring($im,1,$margin/4,$margin/4,"Point",0); imagestring($im,1,$width + $margin - $margin/2,$height + $margin/2 + $margin/4,"Students",0); PLus I wana know how to add the SQL in that.. means the Y akse im getting the points from that SQL tabel .. but now i want in X axis the eksame name like eksame 1 or 2 .. 3 from database to show in X axiss How can i do that ??? Please reply .. thanks alot in advance for the great help ..
16. august 2006 - 14:16
#7
You've noticed that flashbordon added the variable '$margin', and this margin is added to both height and width in most af the lines. I think flashbordon can descripe better the other details, but I will try: // creates a horisontal line in the y-axe imageline($im,$margin/2,$margin/2,$margin/2,$margin/2+$height,0); // creates a vertical line in the x-axe imageline($im,$margin/2,$margin/2 + $height,$width+$margin/2,$margin/2 + $height,0); // parts the maxvalue $maxv in 5 parts to displayed values in the y-axe.$parts = 5; $vPrPart = $maxv/$parts; $yPart = $height/$parts; // makes a for-loop adding them to the picture on their right places for($i=0;$i<=$parts;$i++) { imagestring($im,1,$margin/4,$height - $i*$yPart - 5 + $margin/2,$i*$vPrPart,0); } // creates the header "Point" in the y-axe imagestring($im,1,$margin/4,$margin/4,"Point",0); // creates the header "Students" in the x-axe imagestring($im,1,$width + $margin - $margin/2,$height + $margin/2 + $margin/4,"Students",0); In you vant to add names of students you can make a for-loop in the x-axe too, and add them to the picture on their right places, live it is done in the y-axe.
16. august 2006 - 14:50
#8
Thanks fojensen ... But my question is that i have to put the skame name in X axis and that will come from database. so now my whole function look like this... with database. header("Content-type: Image/png"); ob_start(); session_start(); $result = mysql_query("SELECT * from skemaresultat,sporgeskema where sk_id = skr_skema AND skr_elev = ".$_SESSION['userid']); while ($row= mysql_fetch_assoc($result)) { $values[] = $row["skr_totalpoint"]; $eksamename[] = $row["sk_beskrivelse"]; } // Get the total number of columns we are going to plot $columns = count($values); // Get the height and width of the final image $width = 250; $height = 300; $margin = 100; // Set the amount of space between each column $padding = 10; // Get the width of 1 column $column_width = $width / $columns ; // Generate the image variables $im = imagecreate($width+$margin,$height+$margin); $gray = imagecolorallocate ($im,0xcc,0xcc,0xcc); $gray_lite = imagecolorallocate ($im,0xee,0xee,0xee); $gray_dark = imagecolorallocate ($im,0x7f,0x7f,0x7f); $white = imagecolorallocate ($im,0xff,0xff,0xff); // Fill in the background of the image imagefilledrectangle($im,0,0,$width+$margin,$height+$margin,$white); $maxv = 0; // Calculate the maximum value we are going to plot $maxv = max($values); // Now plot each column for($i=0;$i<$columns;$i++) { //echo $values[$i].",".$height.",".$maxv; $column_height = ($height / 100) * (( $values[$i] / $maxv) *100); $x1 = $i*$column_width+$margin/2 + 5; $y1 = $height-$column_height + $margin/2; $x2 = (($i+1)*$column_width)-$padding+$margin/2 + 5; $y2 = $height + $margin/2; // Using imagepng() results in clearer text compared with imagejpeg() imagefilledrectangle($im,$x1,$y1,$x2,$y2,$gray); // This part is just for 3D effect imageline($im,$x1,$y1,$x1,$y2,$gray_lite); imageline($im,$x1,$y2,$x2,$y2,$gray_lite); imageline($im,$x2,$y1,$x2,$y2,$gray_dark); } imageline($im,$margin/2,$margin/2,$margin/2,$margin/2+$height,0); imageline($im,$margin/2,$margin/2 + $height,$width+$margin/2,$margin/2 + $height,0); $points = 10; $vPrPoint = $maxv/$points; $yPoint = $height/$points; for($i=0;$i<=$points;$i++) { imagestring($im,1,$margin/4,$height - $i*$yPoint - 5 + $margin/2,$i*$vPrPoint,0); } imagestring($im,1,$margin/4,$margin/4,"Point",0); imagestring($im,1,$width + $margin/2 - $margin/2,$height + $margin/2 + $margin/4,"Eksames Description",0); imagepng($im); imagedestroy($im); So now u people have to focus on that main SQL Query i have made and the while-loop which wil take out the eksame description which is name out and put on X axis after look at how much colums that user have. Can u please help me on that... please Message $to *flashbordon* to look at script once again. thanks in advance.
16. august 2006 - 15:21
#9
//in the top add $columnPlacement = array(); //after each column x1,y1,x2,y1 calculation add $columnPlacement[] = (x2-x1)/2 + x1; //before imagepng add for($i=0;$i<count($eksamename);$i++){ imagestring($im,1,$columnPlacement[$i],$height + $margin/2 + $margin/4,$eksamename[$i],0); }
16. august 2006 - 15:24
#10
I'v tried to place them in the middle of the column... that might not be good try: imagestring($im,1,$columnPlacement[$i] - strlen($eksamename[$i])*3,$height + $margin/2 + $margin/4,$eksamename[$i],0); instead -flashbordon
16. august 2006 - 19:21
#11
Im getting error in that line... its from the first FOR-LOOP ... and line is that.. $column_height = ($height / 100) * (( $values[$i] / $maxv) *100); Error is : Unsupported operand types in <b>C:\Program Files\Apache Group\Apache2\htdocs\copy\Try\Statistik.php</b> on line <b>54</b><br /> What i have to do now?
17. august 2006 - 08:50
#12
please help i really nead that graf to be finished today ... please friends help me pleaasseee
17. august 2006 - 09:39
#13
is this line 54 $column_height = ($height / 100) * (( $values[$i] / $maxv) *100); There wasn't an error earlier and you didn't change anything.... that makes it a bit difficult post the entire code as it looks right now -flashbordon and please don't beg for help... we all help as much as we can. but we are working fulltime jobs here.... we can't just dedicate ourselves to your problem because you have chosen to do an assignment you don't have the skills to do. sorry if it comes out a bit harsh...
17. august 2006 - 11:31
#14
<?php header("Content-type: Image/png"); ob_start(); session_start(); $result = mysql_query("SELECT * from skemaresultat,sporgeskema where sk_id = skr_skema AND skr_elev = ".$_SESSION['userid']); while ($row= mysql_fetch_assoc($result)) { $values[] = $row["skr_totalpoint"]; } $columns = count($values); $width = 250; $height = 300; $margin = 100; $padding = 10; $column_width = $width / $columns ; $im = imagecreate($width+$margin,$height+$margin); $gray = imagecolorallocate ($im,0xcc,0xcc,0xcc); $gray_lite = imagecolorallocate ($im,0xee,0xee,0xee); $gray_dark = imagecolorallocate ($im,0x7f,0x7f,0x7f); $white = imagecolorallocate ($im,0xff,0xff,0xff); imagefilledrectangle($im,0,0,$width+$margin,$height+$margin,$white); $maxv = 0; $maxv = max($values); for($i=0;$i<$columns;$i++) { $column_height = ($height / 100) * (( $values[$i] / $maxv) *100 ); $x1 = $i*$column_width+$margin/2 + 5; $y1 = $height-$column_height + $margin/2; $x2 = (($i+1)*$column_width)-$padding+$margin/2 + 5; $y2 = $height + $margin/2; imagefilledrectangle($im,$x1,$y1,$x2,$y2,$gray); imageline($im,$x1,$y1,$x1,$y2,$gray_lite); imageline($im,$x1,$y2,$x2,$y2,$gray_lite); imageline($im,$x2,$y1,$x2,$y2,$gray_dark); } imageline($im,$margin/2,$margin/2,$margin/2,$margin/2+$height,0); imageline($im,$margin/2,$margin/2 + $height,$width+$margin/2,$margin/2 + $height,0); $points = 10; $vPrPoint = $maxv/$points; $yPoint = $height/$points; for($i=0;$i<=$points;$i++) { imagestring($im,1,$margin/4,$height - $i*$yPoint - 5 + $margin/2,$i*$vPrPoint,0); } imagestring($im,1,$margin/4,$margin/4,"Point",0); imagestring($im,1,$width + $margin/2 - $margin/2,$height + $margin/2 + $margin/4,"Eksames Description",0); imagepng($im); imagedestroy($im); ?> Well thats the first code we made .. who show the result Y axes and point. After that i want the X axis to made which is cames with the Exame name which called description in database and comes out on X akes. To make that u said to be add several lines ... //in the top add $columnPlacement = array(); //after each column x1,y1,x2,y1 calculation add $columnPlacement[] = (x2-x1)/2 + x1; //before imagepng add for($i=0;$i<count($eksamename);$i++){ imagestring($im,1,$columnPlacement[$i],$height + $margin/2 + $margin/4,$eksamename[$i],0); } try: imagestring($im,1,$columnPlacement[$i] - strlen($eksamename[$i])*3,$height + $margin/2 + $margin/4,$eksamename[$i],0); But after that I made its comes with eroor on that line i told. Sorry I u minde it I was wating for answer.but sorry. If u still intrested to help me then i will a lot thankfull to U.
17. august 2006 - 11:44
#15
i need to see the code after you added the lines
17. august 2006 - 11:48
#16
<?php header("Content-type: Image/png"); ob_start(); session_start(); mysql_connect('localhost', 'user', 'user'); mysql_select_db('pceksamen'); $result = mysql_query("SELECT * from skemaresultat,sporgeskema where sk_id = skr_skema AND skr_elev = ".$_SESSION['userid']); while ($row= mysql_fetch_assoc($result)) { $values[] = $row["skr_totalpoint"]; $eksamename[] = $row["sk_beskrivelse"]; } $columnPlacement = array(); $columns = count($values); $width = 250; $height = 300; $margin = 100; $padding = 10; $column_width = $width / $columns ; $im = imagecreate($width+$margin,$height+$margin); $gray = imagecolorallocate ($im,0xcc,0xcc,0xcc); $gray_lite = imagecolorallocate ($im,0xee,0xee,0xee); $gray_dark = imagecolorallocate ($im,0x7f,0x7f,0x7f); $white = imagecolorallocate ($im,0xff,0xff,0xff); imagefilledrectangle($im,0,0,$width+$margin,$height+$margin,$white); $maxv = 0; $maxv = max($values); for($i=0;$i<$columns;$i++) { $column_height = ($height / 100) * (( $values[$i] / $maxv) *100 ); $x1 = $i*$column_width+$margin/2 + 5; $y1 = $height-$column_height + $margin/2; $x2 = (($i+1)*$column_width)-$padding+$margin/2 + 5; $y2 = $height + $margin/2; $columnPlacement[] = (x2-x1)/2 + x1; imagefilledrectangle($im,$x1,$y1,$x2,$y2,$gray); imageline($im,$x1,$y1,$x1,$y2,$gray_lite); imageline($im,$x1,$y2,$x2,$y2,$gray_lite); imageline($im,$x2,$y1,$x2,$y2,$gray_dark); } imageline($im,$margin/2,$margin/2,$margin/2,$margin/2+$height,0); imageline($im,$margin/2,$margin/2 + $height,$width+$margin/2,$margin/2 + $height,0); $points = 10; $vPrPoint = $maxv/$points; $yPoint = $height/$points; for($i=0;$i<=$points;$i++) { imagestring($im,1,$margin/4,$height - $i*$yPoint - 5 + $margin/2,$i*$vPrPoint,0); } imagestring($im,1,$margin/4,$margin/4,"Point",0); for($i=0;$i<count($eksamename);$i++) { // imagestring($im,1,$columnPlacement[$i],$height + $margin/2 + $margin/4,$eksamename[$i],0); imagestring($im,1,$columnPlacement[$i] - strlen($eksamename[$i])*3,$height + $margin/2 + $margin/4,$eksamename[$i],0); } imagestring($im,1,$width + $margin/2 - $margin/2,$height + $margin/2 + $margin/4,"Eksames Description",0); imagepng($im); imagedestroy($im); ?>
17. august 2006 - 11:48
#17
It should be like this <?php header("Content-type: Image/png"); ob_start(); session_start(); $result = mysql_query("SELECT * from skemaresultat,sporgeskema where sk_id = skr_skema AND skr_elev = ".$_SESSION['userid']); while ($row= mysql_fetch_assoc($result)) { $values[] = $row["skr_totalpoint"]; } $columns = count($values); $columnPlacement = array(); $width = 250; $height = 300; $margin = 100; $padding = 10; $column_width = $width / $columns ; $im = imagecreate($width+$margin,$height+$margin); $gray = imagecolorallocate ($im,0xcc,0xcc,0xcc); $gray_lite = imagecolorallocate ($im,0xee,0xee,0xee); $gray_dark = imagecolorallocate ($im,0x7f,0x7f,0x7f); $white = imagecolorallocate ($im,0xff,0xff,0xff); imagefilledrectangle($im,0,0,$width+$margin,$height+$margin,$white); $maxv = 0; $maxv = max($values); for($i=0;$i<$columns;$i++) { $column_height = ($height / 100) * (( $values[$i] / $maxv) *100 ); $x1 = $i*$column_width+$margin/2 + 5; $y1 = $height-$column_height + $margin/2; $x2 = (($i+1)*$column_width)-$padding+$margin/2 + 5; $y2 = $height + $margin/2; $columnPlacement[] = (x2-x1)/2 + x1; imagefilledrectangle($im,$x1,$y1,$x2,$y2,$gray); imageline($im,$x1,$y1,$x1,$y2,$gray_lite); imageline($im,$x1,$y2,$x2,$y2,$gray_lite); imageline($im,$x2,$y1,$x2,$y2,$gray_dark); } imageline($im,$margin/2,$margin/2,$margin/2,$margin/2+$height,0); imageline($im,$margin/2,$margin/2 + $height,$width+$margin/2,$margin/2 + $height,0); $points = 10; $vPrPoint = $maxv/$points; $yPoint = $height/$points; for($i=0;$i<=$points;$i++) { imagestring($im,1,$margin/4,$height - $i*$yPoint - 5 + $margin/2,$i*$vPrPoint,0); } imagestring($im,1,$margin/4,$margin/4,"Point",0); imagestring($im,1,$width + $margin/2 - $margin/2,$height + $margin/2 + $margin/4,"Eksames Description",0); for($i=0;$i<count($eksamename);$i++){ imagestring($im,1,$columnPlacement[$i] - strlen($eksamename[$i])*3,$height + $margin/2 + $margin/4,$eksamename[$i],0); } imagepng($im); imagedestroy($im); ?>
17. august 2006 - 11:49
#18
here is the code after add these lines and error is on line 48 which is $columnPlacement[] = (x2-x1)/2 + x1;
17. august 2006 - 11:50
#19
I just realised the error. I had forgotten $ <?php header("Content-type: Image/png"); ob_start(); session_start(); $result = mysql_query("SELECT * from skemaresultat,sporgeskema where sk_id = skr_skema AND skr_elev = ".$_SESSION['userid']); while ($row= mysql_fetch_assoc($result)) { $values[] = $row["skr_totalpoint"]; } $columns = count($values); $columnPlacement = array(); $width = 250; $height = 300; $margin = 100; $padding = 10; $column_width = $width / $columns ; $im = imagecreate($width+$margin,$height+$margin); $gray = imagecolorallocate ($im,0xcc,0xcc,0xcc); $gray_lite = imagecolorallocate ($im,0xee,0xee,0xee); $gray_dark = imagecolorallocate ($im,0x7f,0x7f,0x7f); $white = imagecolorallocate ($im,0xff,0xff,0xff); imagefilledrectangle($im,0,0,$width+$margin,$height+$margin,$white); $maxv = 0; $maxv = max($values); for($i=0;$i<$columns;$i++) { $column_height = ($height / 100) * (( $values[$i] / $maxv) *100 ); $x1 = $i*$column_width+$margin/2 + 5; $y1 = $height-$column_height + $margin/2; $x2 = (($i+1)*$column_width)-$padding+$margin/2 + 5; $y2 = $height + $margin/2; $columnPlacement[] = ($x2-$x1)/2 + $x1; imagefilledrectangle($im,$x1,$y1,$x2,$y2,$gray); imageline($im,$x1,$y1,$x1,$y2,$gray_lite); imageline($im,$x1,$y2,$x2,$y2,$gray_lite); imageline($im,$x2,$y1,$x2,$y2,$gray_dark); } imageline($im,$margin/2,$margin/2,$margin/2,$margin/2+$height,0); imageline($im,$margin/2,$margin/2 + $height,$width+$margin/2,$margin/2 + $height,0); $points = 10; $vPrPoint = $maxv/$points; $yPoint = $height/$points; for($i=0;$i<=$points;$i++) { imagestring($im,1,$margin/4,$height - $i*$yPoint - 5 + $margin/2,$i*$vPrPoint,0); } imagestring($im,1,$margin/4,$margin/4,"Point",0); imagestring($im,1,$width + $margin/2 - $margin/2,$height + $margin/2 + $margin/4,"Eksames Description",0); for($i=0;$i<count($eksamename);$i++){ imagestring($im,1,$columnPlacement[$i] - strlen($eksamename[$i])*3,$height + $margin/2 + $margin/4,$eksamename[$i],0); } imagepng($im); imagedestroy($im); ?>
17. august 2006 - 12:26
#21
try this.... it doesn't rotate them but it might help anyway for($i=0;$i<count($eksamename);$i++){ if($i%2 == 0){ $y = $height + $margin/2 + $margin/4; }else{ $y = $height + $margin/2 + $margin/8; } imagestring($im,1,$columnPlacement[$i] - strlen($eksamename[$i])*3,$y,$eksamename[$i],0); }
17. august 2006 - 14:36
#22
well its better the before thanks alot.. can u explan me how i can made that x y lines and the text on that line dark so i can read it???
17. august 2006 - 15:55
#23
the last parameter of imagestring and imageline is the color. i've made them 0 which ought to be black as far as i know, but try 0x000000 instead -flashbordon
17. august 2006 - 15:57
#24
arhhh... i just read the documentation again //after the lines: $gray = imagecolorallocate ($im,0xcc,0xcc,0xcc); $gray_lite = imagecolorallocate ($im,0xee,0xee,0xee); $gray_dark = imagecolorallocate ($im,0x7f,0x7f,0x7f); $white = imagecolorallocate ($im,0xff,0xff,0xff); //add $dark = imagecolorallocate ($im,0,0,0); then change all the imagelines you want to $dark in the last parameter instead of 0 the same with the imagestring -flashbordon
17. august 2006 - 22:58
#25
Thanks alot for ur help im to much thanks full of u for helping me so much. I need some function lines description if u dont minde can i put the lines so u can tell me what they r doing???
22. august 2006 - 14:08
#26
hvsi du poster hele scriptet med de kommentarer du har så skal jeg nok fylde resten ud -flashbordon
31. august 2006 - 14:56
#27
u still need help? if not, do I get some points for helping you? -flashbordon
Vi tilbyder markedets bedste kurser inden for webudvikling