Jump to content

help ammending a graph script


Gazz1982

Recommended Posts

hi, I hope some one can be of help?

 

I want to make a graph using php and GD with data from MySQL, so far success. Now I want to ammend the script (see end of message) to display text on each bar, e.g red, green, blue.

 

Also currently I loop through when setting the bar colours, in this case grey, how can I get it so there are 3 different colours? There will only ever be 3 bars.

 

Thank you for any  help

 

Gary

 

<?php
$dbhost = 'host';
$dbuser = 'user';
$dbpass = 'password';
$dbname = 'db';
$tblname = 'tbl';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

mysql_select_db($dbname);

// Make a MySQL Connection
$query = sql goes here!
$result = mysql_query($query) or die(mysql_error());
$values = array();
// Print out result
while($row = mysql_fetch_array($result)){   
array_push($values,$row['COUNT(Answer)']);}





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


// 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,$colour[$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);

?> 

Link to comment
https://forums.phpfreaks.com/topic/225765-help-ammending-a-graph-script/
Share on other sites

Just a hunch...

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;
if($i==0){ $color = "red";}
if($i==1) {$color = "green";}
if($i==2) { $color = "blue";}

         imagefilledrectangle($im,$x1,$y1,$x2,$y2,$color);
        // imagefilledrectangle($im,$x1,$y1,$x2,$y2,$colour[$i]); 

Create an array of three colours...

 

$colours = array(
                     imagecolorallocate ($im,0xff,0x00,0xcc),
                     imagecolorallocate ($im,0xcc,0xff,0x00),
                     imagecolorallocate ($im,0x00,0xcc,0xff)
                );

 

Then, in your for loop, instead of using $gray, use $colours[$i].

 

To set a label for the bars, look into the PHP function imagestring() --- http://www.php.net/manual/en/function.imagestring.php

Thanks for that, looks exactly like what I need, although it does seem to work... have I done something silly, I have double and double checked it but I can't see whats wrong:

<?php

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

?> 

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.