Jump to content

[SOLVED] Bar chart


Minase

Recommended Posts

i am working on a game and i need a chart (game has skills like ICE/FIRE/EARTH)

i have already the varibles that take the numbers from mysql.

was looking on google for some free scripts to use,but no success at all,it was a trial one ,or it did not fit my needs (i am new to charts)

i have the image center.jpg lets say

and i do have 5 skills (FIRE/ICE/EARH/AIR/WATER) every element has the value in a variable (lets say we asign variables with exact name like elements)

the script should not order them,but to leave theyr order how it is(instead it should first find the highest value and make its width 200px,if they are more than 1 variable with the same value and they are the highest all bars should have 200px.the rest is a normal chart,if value < higher it should have the bar lower than 200px)

i did try to create such a script for weeks no success,it is painfull when you dont know how to do something and try to create it  >:(

Link to comment
Share on other sites

here's my code for simple bar charts.

<?php
$elements = array (
                'FIRE'   => 200,
                'ICE'    => 300,
                'EARTH'  => 500,
                'AIR'    => 100,
                'WATER'  => 80
		);
$max = max ($elements);

echo '<table>';
foreach ($elements as $el => $val)
{
echo "<tr>
		<td>$el</td>
		<td><img src='bar.php?val=$val&max=$max'></td>
		<td>$val</td>
		</tr>";
}
echo '</table>';
?>

 

bar.php (posted before) is

<?php
// set dimensions
     $w = 202;
     $h = 20;
// create image
     $im = imagecreate($w, $h);
// set colours to be used
     $bg = imagecolorallocate($im, 0xE0, 0xE0, 0xE0);
     $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
     $red  = imagecolorallocate($im, 0xFF, 0x00, 0x00);
     $green  = imagecolorallocate($im, 0x50, 0xB6, 0x30);  
// draw border
     imagerectangle($im, 0,0,$w-1,$h-1,$black);
// get value and max value from query string
     $val = isset($_GET['val']) ? $_GET['val'] : 0;
     $max = isset($_GET['max']) ? $_GET['max'] : 100; 
// calculate dimensions of inner bar
     $barw = $max ? floor(($w-2) * $val / $max) : 0;
     $barh = $h - 2;
// draw inner bar
 if ($barw)
     {
        $barcolor = $red;
     	imagefilledrectangle($im, 1, 1, $barw, $barh, $barcolor);
     }
// send image header
     header("content-type: image/png");
// send png image
     imagepng($im);
     imagedestroy($im);
?>

Link to comment
Share on other sites

hmm sorry for bump,but how can i make the image size "dinamic" (the background not to apear,if FIRE is the highest then generate the image 200px max,if water < fire then water == 50px lets say,it should generate an 50 px image not full 200)

hope you understand what i mean ;)

thank you very much

Link to comment
Share on other sites

change this section

// draw border
     imagerectangle($im, 0,0,$w-1,$h-1,$black);

to

// draw border
     imagerectangle($im, 0,0,$w-1,$h-1,$bg);                      // border uses background colur also
     imagecolortransparent($im, $bg);                             // now make bg colour transparent

Link to comment
Share on other sites

the transparent thing wont do it (cause i do have 3 images,just try to imaginate how it will look  ::) )

<LEFT IMAGE><CENTER><RIGHT IMAGE>

 

<CENTER> = this chart ,and if i make the background transparent it will move the right image at the end and i dont want that.

Link to comment
Share on other sites

thats why i asked here cause i dont know how to do it.

i want the image to be generated,but just the RED LINE,the rest that is up to 200px to be removed from image(not transparent or something like that,but removed)

Link to comment
Share on other sites

instead of fixed width image you need to calculate the width before creating the image (the image will now be same size as red bar + 2 px for the border.

 

<?php
// get value and max value from query string
     $val = isset($_GET['val']) ? $_GET['val'] : 0;
     $max = isset($_GET['max']) ? $_GET['max'] : 100;
     $maxw = 202;                                                 // max poss width of image
      
// calculate dimensions of inner bar
     $barw = $max ? floor(($maxw-2) * $val / $max) : 0;
     $barh = $h - 2;
// set dimensions
     $w = $barw+2;                                                // image width = width of bar + 2 (for border)
     $h = 20;
// create image
     $im = imagecreate($w, $h);
// set colours to be used
     $bg = imagecolorallocate($im, 0xFF, 0x00, 0x00);
     $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
// draw border
     imagerectangle($im, 0,0,$w-1,$h-1,$black);                      
// send image header
     header("content-type: image/png");
// send png image
     imagepng($im);
     imagedestroy($im);

?>

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.