Jump to content

rotating text and putting on a bar chart


Gazz1982

Recommended Posts

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




?> 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.