Minase Posted June 8, 2008 Share Posted June 8, 2008 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 https://forums.phpfreaks.com/topic/109294-solved-bar-chart/ Share on other sites More sharing options...
rarebit Posted June 8, 2008 Share Posted June 8, 2008 Find the highest value in the set. Divide 200 (maxheight) by the highest value, the result then equals one unit (in pixels)! Now multiply each value by this unit to get each height respectively... Link to comment https://forums.phpfreaks.com/topic/109294-solved-bar-chart/#findComment-560606 Share on other sites More sharing options...
Minase Posted June 8, 2008 Author Share Posted June 8, 2008 the problem is that i need the chart script (to generate the pictures,about formulas or other things i can handle) Link to comment https://forums.phpfreaks.com/topic/109294-solved-bar-chart/#findComment-560619 Share on other sites More sharing options...
Barand Posted June 8, 2008 Share Posted June 8, 2008 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 https://forums.phpfreaks.com/topic/109294-solved-bar-chart/#findComment-560626 Share on other sites More sharing options...
Minase Posted June 8, 2008 Author Share Posted June 8, 2008 lol it was easy (if you dont know how to do it ,its really hard :-\ ) thank you very much Link to comment https://forums.phpfreaks.com/topic/109294-solved-bar-chart/#findComment-560650 Share on other sites More sharing options...
Minase Posted June 8, 2008 Author Share Posted June 8, 2008 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 https://forums.phpfreaks.com/topic/109294-solved-bar-chart/#findComment-560664 Share on other sites More sharing options...
Barand Posted June 8, 2008 Share Posted June 8, 2008 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 https://forums.phpfreaks.com/topic/109294-solved-bar-chart/#findComment-560668 Share on other sites More sharing options...
Minase Posted June 8, 2008 Author Share Posted June 8, 2008 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 https://forums.phpfreaks.com/topic/109294-solved-bar-chart/#findComment-560671 Share on other sites More sharing options...
Barand Posted June 8, 2008 Share Posted June 8, 2008 OK, you know what you need. I've given you a good start Link to comment https://forums.phpfreaks.com/topic/109294-solved-bar-chart/#findComment-560744 Share on other sites More sharing options...
Minase Posted June 8, 2008 Author Share Posted June 8, 2008 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 https://forums.phpfreaks.com/topic/109294-solved-bar-chart/#findComment-560770 Share on other sites More sharing options...
Barand Posted June 8, 2008 Share Posted June 8, 2008 I've shown you how to generate the bars. I've shown you how to calculate the size of the bar. IBM has a good company motto : THINK. I'll be back tomorrow if you haven't worked it out. Link to comment https://forums.phpfreaks.com/topic/109294-solved-bar-chart/#findComment-560773 Share on other sites More sharing options...
Minase Posted June 9, 2008 Author Share Posted June 9, 2008 i did think ,but it was useless ,tryed to remove the background or subtitute it with a 0 value,i did not find a function that remove an image . Link to comment https://forums.phpfreaks.com/topic/109294-solved-bar-chart/#findComment-560878 Share on other sites More sharing options...
Barand Posted June 9, 2008 Share Posted June 9, 2008 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 https://forums.phpfreaks.com/topic/109294-solved-bar-chart/#findComment-560921 Share on other sites More sharing options...
Minase Posted June 9, 2008 Author Share Posted June 9, 2008 thank you very much Link to comment https://forums.phpfreaks.com/topic/109294-solved-bar-chart/#findComment-560966 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.