Darkmatter5 Posted October 29, 2008 Share Posted October 29, 2008 I have this code that creates a bar graph. If I put the code simply in a php file and display it, it runs fine, but if I run it as a function within another file, it produces the error. "The image "mywebsite" cannot be displayed, because it contains errors." Here's the code: function bar_graph() { $values=array( "Jan" => 110, "Feb" => 130, "Mar" => 215, "Apr" => 81, "May" => 310, "Jun" => 110, "Jul" => 190, "Aug" => 175, "Sep" => 390, "Oct" => 286, "Nov" => 150, "Dec" => 196 ); $img_width=450; $img_height=300; $margins=20; # ---- Find the size of graph by substracting the size of borders $graph_width=$img_width - $margins * 2; $graph_height=$img_height - $margins * 2; $img=imagecreate($img_width,$img_height); $bar_width=20; $total_bars=count($values); $gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1); # ---- Define Colors $bar_color=imagecolorallocate($img,0,64,128); $background_color=imagecolorallocate($img,240,240,255); $border_color=imagecolorallocate($img,200,200,200); $line_color=imagecolorallocate($img,220,220,220); # ---- Create the border around the graph imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color); imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color); # ---- Max value is required to adjust the scale $max_value=max($values); $ratio= $graph_height/$max_value; # ---- Create scale and draw horizontal lines $horizontal_lines=20; $horizontal_gap=$graph_height/$horizontal_lines; for($i=1;$i<=$horizontal_lines;$i++){ $y=$img_height - $margins - $horizontal_gap * $i; imageline($img,$margins,$y,$img_width-$margins,$y,$line_color); $v=intval($horizontal_gap * $i /$ratio); imagestring($img,0,5,$y-5,$v,$bar_color); } # ---- Draw the bars here for($i=0;$i< $total_bars; $i++){ # ---- Extract key and value pair from the current pointer position list($key,$value)=each($values); $x1= $margins + $gap + $i * ($gap+$bar_width); $x2= $x1 + $bar_width; $y1=$margins +$graph_height- intval($value * $ratio); $y2=$img_height-$margins; imagestring($img,0,$x1+3,$y1-10,$value,$bar_color);imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color); imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color); } header("Content-type:image/png"); imagepng($img); $_REQUEST['asdfad']=234234; } Can anyone help me figure this out. This code is from the following tutorial. http://www.tutorialized.com/tutorial/How-to-create-bar-graph-in-PHP-with-dynamic-scaling/37826 Link to comment https://forums.phpfreaks.com/topic/130579-help-with-code-for-creating-a-graph/ Share on other sites More sharing options...
rhodesa Posted October 29, 2008 Share Posted October 29, 2008 can you post the code that is including the file and calling the function? note: there can't be ANY extra characters/whitespace/etc echoed in ANY part of the script except for this part: header("Content-type:image/png"); imagepng($img); Link to comment https://forums.phpfreaks.com/topic/130579-help-with-code-for-creating-a-graph/#findComment-677453 Share on other sites More sharing options...
Darkmatter5 Posted October 29, 2008 Author Share Posted October 29, 2008 Okay I'm sorry for this confusion, but I've chosen to put the code directly in the php file and not called as a function. Here's my new code with an example of my tables used in the code. I see that you said, "note: there can't be ANY extra characters/whitespace/etc echoed in ANY part of the script except for this part:" header("Content-type:image/png"); imagepng($img); In my code there were and still are echos before the: header("Content-type:image/png"); imagepng($img); part. Can you help me figure out how I can get this image to be created and displayed? Here's me new code: <?php include 'library/config.inc.php'; $conn=mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql'); mysql_select_db($dbnamemain); $query1=mysql_query("SELECT folder_id, fol_name FROM folders JOIN cabinets ON(cabinets.cabinet_id=folders.cabinet_id) WHERE cab_name='Bills' ORDER BY fol_name ASC ") or die(mysql_error()); while($row=mysql_fetch_array($query1)) { $query2=mysql_query("SELECT bill_date_due, bill_amount FROM items WHERE folder_id=$row[folder_id] ORDER BY bill_date_due ASC ") or die(mysql_error()); echo "$row[fol_name]<br>"; $dataset=array(); while(list($date,$amt)=mysql_fetch_row($query2)) { $t=strtotime($date); $values=array ("year"=>date('Y',$t), "month"=>date('m',$t), "amount"=>$amt); $dataset[$values[month]]=$values[amount]; } if($dataset!=NULL) { $img_width=450; $img_height=300; $margins=20; # ---- Find the size of graph by substracting the size of borders $graph_width=$img_width - $margins * 2; $graph_height=$img_height - $margins * 2; $img=imagecreate($img_width,$img_height); $bar_width=20; $total_bars=count($dataset); $gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1); # ---- Define Colors $bar_color=imagecolorallocate($img,0,64,128); $background_color=imagecolorallocate($img,240,240,255); $border_color=imagecolorallocate($img,200,200,200); $line_color=imagecolorallocate($img,220,220,220); # ---- Create the border around the graph imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color); imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color); # ---- Max value is required to adjust the scale $max_value=max($dataset); $ratio= $graph_height/$max_value; # ---- Create scale and draw horizontal lines $horizontal_lines=20; $horizontal_gap=$graph_height/$horizontal_lines; for($i=1;$i<=$horizontal_lines;$i++){ $y=$img_height - $margins - $horizontal_gap * $i; imageline($img,$margins,$y,$img_width-$margins,$y,$line_color); $v=intval($horizontal_gap * $i /$ratio); imagestring($img,0,5,$y-5,$v,$bar_color); } # ---- Draw the bars here for($i=0;$i< $total_bars; $i++){ # ---- Extract key and value pair from the current pointer position list($key,$value)=each($dataset); $x1= $margins + $gap + $i * ($gap+$bar_width); $x2= $x1 + $bar_width; $y1=$margins +$graph_height- intval($value * $ratio); $y2=$img_height-$margins; imagestring($img,0,$x1+3,$y1-10,$value,$bar_color); imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color); imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color); } //header("Content-type:image/png"); imagepng($img); //$_REQUEST['asdfad']=234234; } else { echo "<i>No data entered for this bill folder!</i>"; } echo "<p>"; } mysql_close($conn); ?> Example tables: cabinets cabinet_id cab_name 2 bills folders folder_id fol_name cabinet_id 2 telephone 2 10 water and electric 2 15 internet 2 items item_id item_name bill_date_due bill_amount 9 tel01 2008-10-15 100 11 tel02 2008-11-15 120 12 tel03 2008-12-15 200 12 tel03 2008-12-15 200 13 inter01 2008-10-16 50 14 inter02 2008-11-16 50 Did I leave anything out? ??? Link to comment https://forums.phpfreaks.com/topic/130579-help-with-code-for-creating-a-graph/#findComment-677492 Share on other sites More sharing options...
rhodesa Posted October 29, 2008 Share Posted October 29, 2008 looks like you just need to remove the whitespace at the beginning of the file before the PHP tag: <?php include 'library/config.inc.php'; $conn=mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql'); mysql_select_db($dbnamemain); $query1=mysql_query("SELECT folder_id, fol_name FROM folders JOIN cabinets ON(cabinets.cabinet_id=folders.cabinet_id) WHERE cab_name='Bills' ORDER BY fol_name ASC ") or die(mysql_error()); while($row=mysql_fetch_array($query1)) { $query2=mysql_query("SELECT bill_date_due, bill_amount FROM items WHERE folder_id=$row[folder_id] ORDER BY bill_date_due ASC ") or die(mysql_error()); echo "$row[fol_name]<br>"; $dataset=array(); while(list($date,$amt)=mysql_fetch_row($query2)) { $t=strtotime($date); $values=array ("year"=>date('Y',$t), "month"=>date('m',$t), "amount"=>$amt); $dataset[$values[month]]=$values[amount]; } if($dataset!=NULL) { $img_width=450; $img_height=300; $margins=20; # ---- Find the size of graph by substracting the size of borders $graph_width=$img_width - $margins * 2; $graph_height=$img_height - $margins * 2; $img=imagecreate($img_width,$img_height); $bar_width=20; $total_bars=count($dataset); $gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1); # ---- Define Colors $bar_color=imagecolorallocate($img,0,64,128); $background_color=imagecolorallocate($img,240,240,255); $border_color=imagecolorallocate($img,200,200,200); $line_color=imagecolorallocate($img,220,220,220); # ---- Create the border around the graph imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color); imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color); # ---- Max value is required to adjust the scale $max_value=max($dataset); $ratio= $graph_height/$max_value; # ---- Create scale and draw horizontal lines $horizontal_lines=20; $horizontal_gap=$graph_height/$horizontal_lines; for($i=1;$i<=$horizontal_lines;$i++){ $y=$img_height - $margins - $horizontal_gap * $i; imageline($img,$margins,$y,$img_width-$margins,$y,$line_color); $v=intval($horizontal_gap * $i /$ratio); imagestring($img,0,5,$y-5,$v,$bar_color); } # ---- Draw the bars here for($i=0;$i< $total_bars; $i++){ # ---- Extract key and value pair from the current pointer position list($key,$value)=each($dataset); $x1= $margins + $gap + $i * ($gap+$bar_width); $x2= $x1 + $bar_width; $y1=$margins +$graph_height- intval($value * $ratio); $y2=$img_height-$margins; imagestring($img,0,$x1+3,$y1-10,$value,$bar_color); imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color); imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color); } header("Content-type:image/png"); imagepng($img); //$_REQUEST['asdfad']=234234; exit; //This is just to make sure nothing else is echoed out } else { echo "<i>No data entered for this bill folder!</i>"; } echo "<p>"; } mysql_close($conn); ?> Link to comment https://forums.phpfreaks.com/topic/130579-help-with-code-for-creating-a-graph/#findComment-677495 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.