HTML Grafy
Grafy vygenerované priamo do HTML.
<? /* # # # $Locker: pdavis $ # $Date: 1998/11/05 06:15:52 $ # $Source: C:\\My\040Documents\\RCS\\H\\Webguys\\pdavis\\Programs\\html_graphs\\HTML_Graphs.php3,v $ # $State: Exp $ # $Log: HTML_Graphs.php3,v $ # Added error_reporting setting per Jean-Pierre Arneodo's request. # # Add double_vertical_graph from Jan Diepens. # Added code to increase values of zero to one. # Combined all source into one zip. # Revision 1.3 1998/06/17 23:37:19 pdavis # # Fixed Background problem, added mixed color codes and graphics. # Revision 1.1 1998/06/17 15:52:41 pdavis # ####################################################################### # * # * # * # * http://www.pobox.com/~pdavis/ # # Phil Davis # # and/or modify it under the terms of the GNU General # Software Foundation. # This program is distributed in the hope that it will # implied warranty of MERCHANTABILITY or FITNESS FOR A # for more details. # Released under GNU Public License v2.0, available # warranties relating to this software, express or implied, # merchantability, quality performance, or fitness for a # shall not be liable for any special, incidental, # of data, even if an agent of the author has been found # author's liability for any damages ever exceed the price # form of the claim. The person using the software bears all # # # written and disk-based notes and specifications, and all # renditions, and text files, are the property of the # # material found in this document is both useful and # the author does not make any guarantee concerning the # to which it may be put. ####################################################################### # About: # The following PHP3 code provides a nice class interface for # interface for creating HTML based graphs. The idea behind # arrays and pass these to html_graph() which then takes # with the outcome of this interface. The HTML that must be # very clean and flexible. I think that once you generate # the same. The arrays that must be set up consist of: # * A names array containing column/row identifiers ($names) # values to the column/row names ($values & $dvalues) # array. The values in these arrays are URLS to graphics # generate the graph bar. Color codes and graphics may # be used on Vertical charts. ($bars & $dbars) # array isn't created then html_graphs will use all # include font styles & colors, backgrounds, graphics, # and scaling factor. ($vals) ####################################################################### # Known Bugs: # * Currently the $vals["background"] tag element doesn't # # # # optional. Currently it has to at least be an empty array. ####################################################################### # Contributors: # Jan Diepens - Eindhoven University of Technologie # # # # please send them to pdavis@pobox.com so that they may be wrapped into # # # # */ /* # # # print data, and close table. # Arguments: # $values - Array of corresponding values for elements. # or color codes (begining with a #) for elements. # $dvalues - Array of corresponding values for elements. # $dbars - Array of corresponding graphic image names # This set is required only in the double graph. # $vals - array("vlabel"=>"", # "type"=>"", # "cellspacing"=>"", # "width"=>"", # "vfcolor"=>"", # "vbgcolor"=>"", # "vfstyle"=>"", # "noshowvals"=>"", # "namebgcolor"=>"", # "namefcolor"=>"", # "namefstyle"=>"", # "doublefcolor"=>"") # Where: # vlabel - Vertical Label to apply # hlabel - Horizontal Label to apply # type - Type of graph # 1 = vertical # 3 = double vertical # cellpadding - Padding for the overall table # cellspacing - Space for the overall table # border - Border size for the overall table # width - Width of the overall table # background - Background image for the overall table # codes will be added to table elements. # vfcolor - Vertical label font color # hfcolor - Horizontal label font color # vbgcolor - Vertical label background color # default is #FFFFFF # Not used if background is set # vfstyle - Vertical label font style # hfstyle - Horizontal label font style # noshowvals - Don't show numeric value at end of graphic # scale - Scale values by some number. # namebgcolor - Color code for element name cells # default is "#000000" # Not used if background is set # namefcolor - Color code for font of name element # valuefcolor - Color code for font of value element # namefstyle - Style code for font of name element # valuefstyle - Style code for font of value element # doublefcolor - Color code for font of second element value # */ function html_graph($names, $values, $bars, $vals, $dvalues=0, $dbars=0) { // Set the error level on entry and exit so as not to interfear // with anyone elses error checking. $er = error_reporting(1); // Set the values that the user didn't $vals = hv_graph_defaults($vals); start_graph($vals, $names); if ($vals["type"] == 0) { horizontal_graph($names, $values, $bars, $vals); } elseif ($vals["type"] == 1) { vertical_graph($names, $values, $bars, $vals); } elseif ($vals["type"] == 2) { double_horizontal_graph($names, $values, $bars, $vals, $dvalues, $dbars); } elseif ($vals["type"] == 3) { double_vertical_graph($names, $values, $bars, $vals, $dvalues, $dbars); } end_graph(); // Set the error level back to where it was. error_reporting($er); } /* # # # null. Used to avoid warnings from error_reporting being # are woried about using uninitialized variables. # Returns: The initialized $vals array ####################################################################### */ function html_graph_init() { $vals = array("vlabel"=>"", "hlabel"=>"", "type"=>"", "cellpadding"=>"", "cellspacing"=>"", "border"=>"", "width"=>"", "background"=>"", "vfcolor"=>"", "hfcolor"=>"", "vbgcolor"=>"", "hbgcolor"=>"", "vfstyle"=>"", "hfstyle"=>"", "noshowvals"=>"", "scale"=>"", "namebgcolor"=>"", "valuebgcolor"=>"", "namefcolor"=>"", "valuefcolor"=>"", "namefstyle"=>"", "valuefstyle"=>"", "doublefcolor"=>""); return($vals); } /* # # # */ function start_graph($vals, $names) { print '<TABLE'; print ' CELLPADDING="' . $vals["cellpadding"] . '"'; print ' CELLSPACING="' . $vals["cellspacing"] . '"'; print ' BORDER="' . $vals["border"] . '"'; if ($vals["width"] != 0) { print ' WIDTH="' . $vals["width"] . '"'; } if ($vals["background"]) { print ' BACKGROUND="' . $vals["background"] . '"'; } print '>'; if (($vals["vlabel"]) || ($vals["hlabel"])) { if (($vals["type"] == 0) || ($vals["type"] == 2 ))// horizontal chart { $rowspan = SizeOf($names) + 1; $colspan = 3; } elseif ($vals["type"] == 1 || ($vals["type"] == 3 )) // vertical chart { $rowspan = 3; $colspan = SizeOf($names) + 1; } print '<TR><TD ALIGN=CENTER VALIGN="CENTER" '; // If a background was choosen don't print cell BGCOLOR if (! $vals["background"]) { print 'BGCOLOR="' . $vals["hbgcolor"] . '"'; } print ' COLSPAN="' . $colspan . '">'; print '<FONT COLOR="' . $vals["hfcolor"] . '" STYLE="' . $vals["hfstyle"] . '">'; print "<B>" . $vals["hlabel"] . "</B>"; print '</FONT></TD></TR>'; print '<TR><TD ALIGN="CENTER" VALIGN="CENTER" '; // If a background was choosen don't print cell BGCOLOR if (! $vals["background"]) { print 'BGCOLOR="' . $vals["vbgcolor"] . '"'; } print ' ROWSPAN="' . $rowspan . '">'; print '<FONT COLOR="' . $vals["vfcolor"] . '" STYLE="' . $vals["vfstyle"] . '">'; print "<B>" . $vals["vlabel"] . "</B>"; print '</FONT></TD>'; } } /* # # # */ function end_graph() { print "</TABLE>"; } /* # # # */ function hv_graph_defaults($vals) { if (! $vals["vfcolor"]) { $vals["vfcolor"]="#000000"; } if (! $vals["hfcolor"]) { $vals["hfcolor"]="#000000"; } if (! $vals["vbgcolor"]) { $vals["vbgcolor"]="#FFFFFF"; } if (! $vals["hbgcolor"]) { $vals["hbgcolor"]="#FFFFFF"; } if (! $vals["cellpadding"]) { $vals["cellpadding"]=0; } if (! $vals["cellspacing"]) { $vals["cellspacing"]=0; } if (! $vals["border"]) { $vals["border"]=0; } if (! $vals["scale"]) { $vals["scale"]=1; } if (! $vals["namebgcolor"]) { $vals["namebgcolor"]="#FFFFFF"; } if (! $vals["valuebgcolor"]) { $vals["valuebgcolor"]="#FFFFFF"; } if (! $vals["namefcolor"]) { $vals["namefcolor"]="#000000"; } if (! $vals["valuefcolor"]) { $vals["valuefcolor"]="#000000"; } if (! $vals["doublefcolor"]) { $vals["doublefcolor"]="#886666"; } return ($vals); } /* # # # */ function horizontal_graph($names, $values, $bars, $vals) { for( $i=0;$i<SizeOf($values);$i++ ) { ?> <TR> <TD ALIGN=RIGHT <? // If a background was choosen don't print cell BGCOLOR if (! $vals["background"]) { print ' BGCOLOR="' . $vals["namebgcolor"] . '"'; } ?> > <FONT SIZE="-1" COLOR="<? echo $vals["namefcolor"] ?>" STYLE="<? echo $vals["namefstyle"] ?>"> <? echo $names[$i] ?> </FONT> </TD> <TD ALIGN=LEFT <? // If a background was choosen don't print cell BGCOLOR if (! $vals["background"]) { print ' BGCOLOR="' . $vals["valuebgcolor"] . '"'; } ?> > <? // Decide if the value in bar is a color code or image. if (ereg("^#", $bars[$i])) { ?> <TABLE ALIGN="LEFT" CELLPADDING=0 CELLSPACING=0 BGCOLOR="<? echo $bars[$i] ?>" WIDTH="<? echo $values[$i] * $vals["scale"] ?>"> <TR><TD> </TD></TR> </TABLE> <? } else { print '<IMG SRC="' . $bars[$i] . '"'; print ' HEIGHT=10 WIDTH="' . $values[$i] * $vals["scale"] . '">'; } if (! $vals["noshowvals"]) { print '<I><FONT SIZE="-2" COLOR="' . $vals["valuefcolor"] . '" '; print ' STYLE="' . $vals["valuefstyle"] . '">('; print $values[$i] . ")</FONT></I>"; } ?> </TD> </TR> <? } // endfor } // end horizontal_graph /* # # # */ function vertical_graph($names, $values, $bars, $vals) { print "<TR>"; for( $i=0;$i<SizeOf($values);$i++ ) { print '<TD ALIGN="CENTER" VALIGN="BOTTOM" '; // If a background was choosen don't print cell BGCOLOR if (! $vals["background"]) { print ' BGCOLOR="' . $vals["valuebgcolor"] . '"'; } print ">"; if (! $vals["noshowvals"]) { print '<I><FONT SIZE="-2" COLOR="' . $vals["valuefcolor"] . '" '; print ' STYLE="' . $vals["valuefstyle"] . '">('; print $values[$i] . ")</FONT></I><BR>"; } ?> <IMG SRC="<? echo $bars[$i] ?>" WIDTH=5 HEIGHT="<? // Values of zero are displayed wrong because a image height of zero // gives a strange behavior in Netscape. For this reason the height // is set at 1 pixel if the value is zero. - Jan Diepens if ($values[$i] != 0) { echo $values[$i] * $vals["scale"]; } else { echo "1"; } ?>"> </TD> <? } // endfor print "</TR><TR>"; for( $i=0;$i<SizeOf($values);$i++ ) { ?> <TD ALIGN="CENTER" VALIGN="TOP" <? // If a background was choosen don't print cell BGCOLOR if (! $vals["background"]) { print ' BGCOLOR="' . $vals["namebgcolor"] . '"'; } ?> > <FONT SIZE="-1" COLOR="<? echo $vals["namefcolor"] ?>" STYLE="<? echo $vals["namefstyle"] ?>"> <? echo $names[$i] ?> </FONT> </TD> <? } // endfor } // end vertical_graph /* # # $vals, $dvalues, $dbars) # Purpose: Prints out the actual data for the double horizontal chart. ####################################################################### */ function double_horizontal_graph($names, $values, $bars, $vals, $dvalues, $dbars) { for( $i=0;$i<SizeOf($values);$i++ ) { ?> <TR> <TD ALIGN=RIGHT <? // If a background was choosen don't print cell BGCOLOR if (! $vals["background"]) { print ' BGCOLOR="' . $vals["namebgcolor"] . '"'; } ?> > <FONT SIZE="-1" COLOR="<? echo $vals["namefcolor"] ?>" STYLE="<? echo $vals["namefstyle"] ?>"> <? echo $names[$i] ?> </FONT> </TD> <TD ALIGN=LEFT <? // If a background was choosen don't print cell BGCOLOR if (! $vals["background"]) { print ' BGCOLOR="' . $vals["valuebgcolor"] . '"'; } ?> > <TABLE ALIGN="LEFT" CELLPADDING=0 CELLSPACING=0 WIDTH="<? echo $dvalues[$i] * $vals["scale"] ?>"> <TR><TD <? // Set background to a color if it starts with # or // an image otherwise. if (ereg("^#", $dbars[$i])) { print 'BGCOLOR="' . $dbars[$i] . '">'; } else { print 'BACKGROUND="' . $dbars[$i] . '">'; } ?> <NOWRAP> <? // Decide if the value in bar is a color code or image. if (ereg("^#", $bars[$i])) { ?> <TABLE ALIGN="LEFT" CELLPADDING=0 CELLSPACING=0 BGCOLOR="<? echo $bars[$i] ?>" WIDTH="<? echo $values[$i] * $vals["scale"] ?>"> <TR><TD> </TD></TR> </TABLE> <? } else { print '<IMG SRC="' . $bars[$i] . '"'; print ' HEIGHT=10 WIDTH="' . $values[$i] * $vals["scale"] . '">'; } if (! $vals["noshowvals"]) { print '<I><FONT SIZE="-3" COLOR="' . $vals["valuefcolor"] . '" '; print ' STYLE="' . $vals["valuefstyle"] . '">('; print $values[$i] . ")</FONT></I>"; } ?> </NOWRAP> </TD></TR> </TABLE> <? if (! $vals["noshowvals"]) { print '<I><FONT SIZE="-3" COLOR="' . $vals["doublefcolor"] . '" '; print ' STYLE="' . $vals["valuefstyle"] . '">('; print $dvalues[$i] . ")</FONT></I>"; } ?> </TD> </TR> <? } // endfor } // end double_horizontal_graph /* # # # # */ function double_vertical_graph($names, $values, $bars, $vals, $dvalues, $dbars) { // print "<TR>"; for( $i=0;$i<SizeOf($values);$i++ ) { print '<TD ALIGN="CENTER" VALIGN="BOTTOM" '; // If a background was choosen don't print cell BGCOLOR if (! $vals["background"]) { print ' BGCOLOR="' . $vals["valuebgcolor"] . '"'; } print ">"; print '<TABLE><TR><TD ALIGN="CENTER" VALIGN="BOTTOM" '; // If a background was choosen don't print cell BGCOLOR if (! $vals["background"]) { print ' BGCOLOR="' . $vals["valuebgcolor"] . '"'; } print ">"; if (! $vals["noshowvals"]) { print '<I><FONT SIZE="-2" COLOR="' . $vals["valuefcolor"] . '" '; print ' STYLE="' . $vals["valuefstyle"] . '">('; print $values[$i] . ")</FONT></I><BR>"; } ?> <IMG SRC="<? echo $bars[$i] ?>" WIDTH=10 HEIGHT="<? if ($values[$i]!=0){ echo $values[$i] * $vals["scale"]; } else { echo "1";} ?>"> </TD><TD ALIGN="CENTER" VALIGN="BOTTOM" <? // If a background was choosen don't print cell BGCOLOR if (! $vals["background"]) { print ' BGCOLOR="' . $vals["valuebgcolor"] . '"'; } print ">"; if (! $vals["noshowvals"]) { print '<I><FONT SIZE="-2" COLOR="' . $vals["doublefcolor"] . '" '; print ' STYLE="' . $vals["valuefstyle"] . '">('; print $dvalues[$i] . ")</FONT></I><BR>"; } ?> <IMG SRC="<? echo $dbars[$i] ?>" WIDTH=10 HEIGHT="<? if ($dvalues[$i]!=0){ echo $dvalues[$i] * $vals["scale"]; } else { echo "1";} ?>"> </TD></TR></TABLE> </TD> <? } // endfor print "</TR><TR>"; for( $i=0;$i<SizeOf($values);$i++ ) { ?> <TD ALIGN="CENTER" VALIGN="TOP" <? // If a background was choosen don't print cell BGCOLOR if (! $vals["background"]) { print ' BGCOLOR="' . $vals["namebgcolor"] . '"'; } ?> > <FONT SIZE="-1" COLOR="<? echo $vals["namefcolor"] ?>" STYLE="<? echo $vals["namefstyle"] ?>"> <? echo $names[$i] ?> </FONT> </TD> <? } // endfor } // end double_vertical_graph print "</TR>"; ?>