Forum Moderators: coopster
Some more advance users will may be provide you links of some PHP module or any other stuff that can help you to make graph with no (or few) efforts.
Lastly, I have myself tried some of the scripts I have found in PHP books, but since I only needed a simple horizontal-bar graph, I have decided no to use GD but just a 1x1px gif image and modify its size accordingly. Works great and faster!
// This is the function which assigned the color to the bar depending of their value (create 1x1px gif image)
function graph_bar_color($var) {
if ($var>="90") {return "dot_red";}
elseif ($var<"90" AND $var>="75") {return "dot_orange";}
elseif ($var<"75" AND $var>="50") {return "dot_orange2";}
elseif ($var<"50" AND $var>="30") {return "dot_green";}
elseif ($var<"30" AND $var>="10") {return "dot_blue";}
elseif ($var<"10" AND $var>"1") {return "dot_pink";}
else {return "images/dot_grey";}
}// Some fixed variable
$graphWidthMax="244";
$barHeight="8";// Your variable - do the necessary modification here
$percent="50";
$barWidth=intval($graphWidthMax * $percent/100);// And show the horizontal bar
echo '<img src="images/'.graph_bar_color($percent).'.gif" width="'.$barWidth.'" height="'.$barHeight.'" alt="">';
Enjoy!
<?php
// oringinal code by Tomda
function graph_bar_color($var)
{
if ($var>="90") {return "silver";}
elseif ($var<"90" AND $var>="75") {return "orange";}
elseif ($var<"75" AND $var>="50") {return "red";}
elseif ($var<"50" AND $var>="30") {return "green";}
elseif ($var<"30" AND $var>="10") {return "blue";}
elseif ($var<"10" AND $var>"1") {return "pink";}
else {return "gray";}
}
# defaults for the height and the max height, as they will be consistant
$graphWidthMax="244";
$barHeight="8";
# and a 1
$percent="20";
$barWidth=intval($graphWidthMax * $percent/100);
# and a 2
$percent_value2="50";
$barWidth2=intval($graphWidthMax * $percent_value2/100);
# and a 3
$percent_value3="100";
$barWidth3=intval($graphWidthMax * $percent_value3/100);
echo '<div style="background:'.graph_bar_color($percent).';width:'.$barWidth.';height:'.$barHeight.'">';
echo '<br>';
echo '<div style="background:'.graph_bar_color($percent_value2).';width:'.$barWidth2.';height:'.$barHeight.'">';
echo '<br>';
echo '<div style="background:'.graph_bar_color($percent_value3).';width:'.$barWidth3.';height:'.$barHeight.'">';
?>
This would be also great to intergrate with a db!
Thanks for Reading,
Using CSS is indeed much better (I though about it but had not the time yet to modify it on my project) and I guess much faster also.
For all CSS color name, see [w3schools.com...]
Hsceeus, as said above, it works great with db. I have a picture gallery with more than 8000 keywords, providing stats for each keywords, it runs like a charm!
[edited by: tomda at 1:03 pm (utc) on Mar. 23, 2006]
Also note that <div> must be closed
<?php
// oringinal code by Tomda
function graph_bar_color($var){
if ($var>="90") {return "red";}
elseif ($var<"90" AND $var>="75") {return "#FF6347";}
elseif ($var<"75" AND $var>="50") {return "orange";}
elseif ($var<"50" AND $var>="30") {return "green";}
elseif ($var<"30" AND $var>="10") {return "blue";}
elseif ($var<"10" AND $var>"1") {return "pink";}
else {return "gray";}
}# defaults for the height and the max height, as they will be consistant
$graphWidthMax="244";
$barHeight="8";# and a 1
$percent="60";
$barWidth=intval($graphWidthMax * $percent/100);# and a 2
$percent_value2="80";
$barWidth2=intval($graphWidthMax * $percent_value2/100);# and a 3
$percent_value3="100";
$barWidth3=intval($graphWidthMax * $percent_value3/100);echo '<div style="background:'.graph_bar_color($percent).'; width:'.$barWidth.'px; height:'.$barHeight.'px; margin-bottom:'.$barHeight.'px;"></div>
<div style="background:'.graph_bar_color($percent_value2).'; width:'.$barWidth2.'px; height:'.$barHeight.'px; margin-bottom:'.$barHeight.'px;"></div>
<div style="background:'.graph_bar_color($percent_value3).'; width:'.$barWidth3.'px; height:'.$barHeight.'px; margin-bottom:'.$barHeight.'px;"></div>';
?>
Any idea how to resolve the height problem in IE?
I guess that the problem using CSS, at least with image you are sure to get the same in all browser and can use unsafe color!
echo '<div style="font-size:0px;background:'.graph_bar_color($percent).'; width:'.$barWidth.'px; height:'.$barHeight.'px; margin-bottom:'.$barHeight.'px;"></div>
<div style="font-size:0px;background:'.graph_bar_color($percent_value2).'; width:'.$barWidth2.'px; height:'.$barHeight.'px; margin-bottom:'.$barHeight.'px;"></div>
<div style="font-size:0px;background:'.graph_bar_color($percent_value3).'; width:'.$barWidth3.'px; height:'.$barHeight.'px; margin-bottom:'.$barHeight.'px;"></div>';
... thanks for noticing the non-closure of the div!
Del