Gazz1982 Posted January 26, 2011 Share Posted January 26, 2011 Hi again, I have a bar chart, standard 3 verticle bars coloured differently, but now i want to put some text on them, say: Bar A, Bar B, Bar C. Also this text has to be rotated 90 degrees So here is the code to make the bars, I guess I need to use: $red = imagecolorallocate($im, 255, 0, 0); imagestring($im, 5, 50, 50, "Bar A", $red); In some kind of loop so: $text = array( imagestring($im, 5, 50, 50, "Bar A", $red); imagestring($im, 5, 50, 50, "Bar B", $red); imagestring($im, 5, 50, 50, "Bar C", $red); ); can anyone help? 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; //SQL connect stuff // This array of values is just here for the example. //$values = array("5","6","7"); // Get the total number of columns we are going to plot $columns = count($values); // Get the height and width of the final image $width = 150; $height = 200; // Set the amount of space between each column $padding = 15; // Get the width of 1 column $column_width = $width / $columns ; // Generate the image variables $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); $colours = array( imagecolorallocate ($im,0xff,0x00,0xcc), imagecolorallocate ($im,0xcc,0xff,0x00), imagecolorallocate ($im,0x00,0xcc,0xff) ); // Fill in the background of the image imagefilledrectangle($im,0,0,$width,$height,$white); $maxv = 0; // Calculate the maximum value we are going to plot for($i=0;$i<$columns;$i++)$maxv = max($values[$i],$maxv); // Now plot each column 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); imagefilledrectangle($im,$x1,$y1,$x2,$y2,$colours[$i]); // 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); } // Send the PNG header information. Replace for JPEG or GIF or whatever header ("Content-type: image/png"); imagepng($im); imagedestroy($im); ?> Quote Link to comment https://forums.phpfreaks.com/topic/225785-rotating-text-and-putting-on-a-bar-chart/ Share on other sites More sharing options...
Gazz1982 Posted January 26, 2011 Author Share Posted January 26, 2011 solved it myself!!!! used needed to add: imagestringup($im, 5, $y1, 190, "Hello World!", $red); // Now plot each column 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); imagefilledrectangle($im,$x1,$y1,$x2,$y2,$colours[$i]); imagestringup($im, 5, $y1, 190, "Hello World!", $red); Quote Link to comment https://forums.phpfreaks.com/topic/225785-rotating-text-and-putting-on-a-bar-chart/#findComment-1165659 Share on other sites More sharing options...
Gazz1982 Posted January 26, 2011 Author Share Posted January 26, 2011 Well nearly sloved, I've narrowed it down to this code snippet, for some reason the array doesnt work, when I have $text = "some text"; is works but plots the same text on each bar. //text array $text = array("A","B","C"); $red = imagecolorallocate($im, 255, 0, 0); // Now plot each column 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,$colours[$i]); imagestringup($im, 5,$x1,$x2,$text,$red); Quote Link to comment https://forums.phpfreaks.com/topic/225785-rotating-text-and-putting-on-a-bar-chart/#findComment-1165689 Share on other sites More sharing options...
BlueSkyIS Posted January 26, 2011 Share Posted January 26, 2011 ... should probably be imagestringup($im, 5,$x1,$x2,$text[$i],$red); Quote Link to comment https://forums.phpfreaks.com/topic/225785-rotating-text-and-putting-on-a-bar-chart/#findComment-1165694 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.