Forum Moderators: coopster

Message Too Old, No Replies

Report in graph

         

hsceeus

10:43 am on Mar 23, 2006 (gmt 0)

10+ Year Member



What is the method can i used to generated a report in graph by using PHP?

tomda

10:56 am on Mar 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are planning to do advance graph, then you should definitely use the GD extension. I am not an advance PHP master, but you can find some nice tutorials on the web ("GD PHP graph") or the web or in books.

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!

hsceeus

11:00 am on Mar 23, 2006 (gmt 0)

10+ Year Member



i not really plan to use the GD in PHP.. Your method quite interesting, can you teach me how to did it.. thanks.

tomda

11:34 am on Mar 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, I can. I hope you are familiar with PHP


// 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!

scriptmasterdel

12:22 pm on Mar 23, 2006 (gmt 0)

10+ Year Member



I think your graph script is great, but this is how i would do it ... with styles instead of images.

<?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,

tomda

12:59 pm on Mar 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for updating the script Scriptmasterdel!
Much appreciated.

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]

hsceeus

1:00 pm on Mar 23, 2006 (gmt 0)

10+ Year Member



thank for your script tomda..

tomda

1:16 pm on Mar 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Scriptmasterdel, I have just try your script in IE.
You get a problem with the height with IE!

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!

scriptmasterdel

1:28 pm on Mar 23, 2006 (gmt 0)

10+ Year Member



Here is a little something for you, try setting the font-size in the div style (shown below) ...

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

maccas

1:35 pm on Mar 23, 2006 (gmt 0)

10+ Year Member



Check out JpGraph. <edited> sorry I thought you wanted to use GD.