3 * SourceForge: Breaking Down the Barriers to Open Source Development
4 * Copyright 1999-2001 (c) VA Linux Systems
5 * Copyright (C) 2012 Alain Peyrat - Alcatel-Lucent
6 * http://sourceforge.net
8 * @author Tim Perdue <tperdue@valnux.com>
10 * Ryan T. Sammartino made several changes to make the output XHTML 1.0
15 #######################################################################
17 # Revision 1.5 1998/11/05 06:15:52 pdavis
18 # Added error_reporting setting per Jean-Pierre Arneodo's request.
19 # (Though redundant) Added html_graph_init() to initialize vars array.
21 # Revision 1.4 1998/07/08 05:24:25 pdavis
22 # Add double_vertical_graph from Jan Diepens.
23 # Added "max" function to find $largest in examples page.
24 # Added code to increase values of zero to one.
25 # Added double_vertical_graph example
26 # Combined all source into one zip.
28 # Revision 1.3 1998/06/17 23:37:19 pdavis
29 # Added mixed color codes and images to double graph.
31 # Revision 1.2 1998/06/17 21:20:20 pdavis
32 # Fixed Background problem, added mixed color codes and graphics.
34 # Revision 1.1 1998/06/17 15:52:41 pdavis
38 #######################################################################
43 # * Smyrna, Tennessee 37167 USA
46 # * http://www.pobox.com/~pdavis/
51 # Printed in the United States of America
53 # This program is free software; you can redistribute it
54 # and/or modify it under the terms of the GNU General
55 # Public License version 2 as published by the Free
56 # Software Foundation.
58 # This program is distributed in the hope that it will
59 # be useful, but WITHOUT ANY WARRANTY; without even the
60 # implied warranty of MERCHANTABILITY or FITNESS FOR A
61 # PARTICULAR PURPOSE. See the GNU General Public License
64 # Released under GNU Public License v2.0, available
65 # at www.fsf.org. The author hereby disclaims all
66 # warranties relating to this software, express or implied,
67 # including with no limitation any implied warranties of
68 # merchantability, quality performance, or fitness for a
69 # particular purpose. The author and their distributors
70 # shall not be liable for any special, incidental,
71 # consequential, indirect or similar damages due to loss
72 # of data, even if an agent of the author has been found
73 # to be the source of loss or damage. In no event shall the
74 # author's liability for any damages ever exceed the price
75 # paid for the license to use software, regardless of the
76 # form of the claim. The person using the software bears all
77 # risk as to the quality and performance of the software.
79 # Swim at your own risk!
81 # This software program, documentation, accompanying
82 # written and disk-based notes and specifications, and all
83 # referenced and related program files, screen display
84 # renditions, and text files, are the property of the
87 # The authors have done their best to insure that the
88 # material found in this document is both useful and
89 # accurate. However, please be aware that errors may exist,
90 # the author does not make any guarantee concerning the
91 # accuracy of the information found here or in the uses
92 # to which it may be put.
94 #######################################################################
98 # The following PHP3 code provides a nice class interface for
99 # html graphs. It provides a single, reasonably consistent
100 # interface for creating HTML based graphs. The idea behind
101 # this code is that the user of the class sets up four or five
102 # arrays and pass these to html_graph() which then takes
103 # care of all the messy HTML layout. I am reasonably happy
104 # with the outcome of this interface. The HTML that must be
105 # generated for HTML graphs *is* messy, and the interface is
106 # very clean and flexible. I think that once you generate
107 # one graph with it, you'll never look at creating HTML graphs
108 # the same. The arrays that must be set up consist of:
110 # * A names array containing column/row identifiers ($names)
111 # * One or two values arrays containg corresponding
112 # values to the column/row names ($values & $dvalues)
113 # * One or two bars array which also corresponds to the names
114 # array. The values in these arrays are URLS to graphics
115 # or color codes starting with a # which will be used to
116 # generate the graph bar. Color codes and graphics may
117 # be mixed in the same chart, although color codes can't
118 # be used on Vertical charts. ($bars & $dbars)
119 # * The heart of customization... a vals array. If this
120 # array isn't created then html_graphs will use all
121 # default values for the chart. Items that are customizable
122 # include font styles & colors, backgrounds, graphics,
123 # labels, cellspacing, cellpadding, borders, anotations
124 # and scaling factor. ($vals)
126 #######################################################################
130 # * Currently the $vals["background"] tag element doesn't
133 #######################################################################
137 # * Would like to make the $vals array to html_graph() completely
138 # optional. Currently it has to at least be an empty array.
140 #######################################################################
144 # Jan Diepens - Eindhoven University of Technologie
145 # Jean-Pierre Arneodo
147 #######################################################################
151 # If you have questions, suggestions, bugs, bug fixes, or enhancements
152 # please send them to pdavis@pobox.com so that they may be wrapped into
153 # future versions of HTML_Graph.
155 #######################################################################
159 # See http://www.pobox.com/~pdavis/programs/
161 #######################################################################
165 #######################################################################
167 # Function: html_graph($names, $values, $bars, $vals[, $dvalues, $dbars])
169 # Purpose: Calls routines to initialize defaults, set up table
170 # print data, and close table.
173 # $names - Array of element names.
174 # $values - Array of corresponding values for elements.
175 # $bars - Array of corresponding graphic image names
176 # or color codes (begining with a #) for elements.
177 # Color codes can't be used on vertical charts.
178 # $dvalues - Array of corresponding values for elements.
179 # This set is required only in the double graph.
180 # $dbars - Array of corresponding graphic image names
181 # or color codes (begining with a #) for elements.
182 # This set is required only in the double graph.
184 # $vals - array("vlabel"=>"",
201 # "valuebgcolor"=>"",
206 # "doublefcolor"=>"")
210 # vlabel - Vertical Label to apply
212 # hlabel - Horizontal Label to apply
214 # type - Type of graph
217 # 2 = double horizontal
218 # 3 = double vertical
220 # cellpadding - Padding for the overall table
222 # cellspacing - Space for the overall table
224 # border - Border size for the overall table
226 # width - Width of the overall table
228 # background - Background image for the overall table
229 # If this value exists then no BGCOLOR
230 # codes will be added to table elements.
232 # vfcolor - Vertical label font color
234 # hfcolor - Horizontal label font color
236 # vbgcolor - Vertical label background color
237 # Not used if background is set
239 # hbgcolor - Horizontal label background color
240 # Not used if background is set
242 # vfstyle - Vertical label font style
244 # hfstyle - Horizontal label font style
246 # noshowvals - Don't show numeric value at end of graphic
247 # Boolean value, default is FALSE
248 # scale - Scale values by some number.
250 # namebgcolor - Color code for element name cells
251 # Not used if background is set
252 # default is "#000000"
253 # valuebgcolor - Color code for value cells
254 # Not used if background is set
255 # default is "#000000"
256 # namefcolor - Color code for font of name element
257 # default is "#FFFFFF"
258 # valuefcolor - Color code for font of value element
259 # default is "#000000"
260 # namefstyle - Style code for font of name element
262 # valuefstyle - Style code for font of value element
264 # doublefcolor - Color code for font of second element value
265 # default is "#886666"
267 #######################################################################
269 function html_graph($names, $values, $bars, $vals, $dvalues=0, $dbars=0)
271 // Set the error level on entry and exit so as not to interfear
272 // with anyone elses error checking.
273 $er = error_reporting(1);
275 // Set the values that the user didn't
276 $vals = hv_graph_defaults($vals);
277 start_graph($vals, $names);
279 if ($vals["type"] == 0)
281 horizontal_graph($names, $values, $bars, $vals);
283 elseif ($vals["type"] == 1)
285 vertical_graph($names, $values, $bars, $vals);
287 elseif ($vals["type"] == 2)
289 double_horizontal_graph($names, $values, $bars, $vals, $dvalues, $dbars);
291 elseif ($vals["type"] == 3)
293 double_vertical_graph($names, $values, $bars, $vals, $dvalues, $dbars);
298 // Set the error level back to where it was.
299 error_reporting($er);
303 #######################################################################
305 # Function: html_graph_init()
307 # Purpose: Sets up the $vals array by initializing all values to
308 # null. Used to avoid warnings from error_reporting being
309 # set high. This routine only needs to be called if you
310 # are woried about using uninitialized variables.
312 # Returns: The initialized $vals array
314 #######################################################################
316 function html_graph_init()
318 $vals = array("vlabel"=>"",
345 #######################################################################
347 # Function: start_graph($vals, $names)
349 # Purpose: Prints out the table header and graph labels.
351 #######################################################################
353 function start_graph($vals, $names)
355 print "<!-- Start Inner Graph Table -->\n\n<table";
356 print ' cellpadding="' . $vals["cellpadding"] . '"';
357 print ' cellspacing="' . $vals["cellspacing"] . '"';
358 print ' border="' . $vals["border"] . '"';
360 if ($vals["width"] != 0) { print ' width="' . $vals["width"] . '"'; }
361 if ($vals["background"]) { print ' background="' . $vals["background"] . '"'; }
365 if (($vals["vlabel"]) || ($vals["hlabel"]))
367 if (($vals["type"] == 0) || ($vals["type"] == 2 ))// horizontal chart
369 $rowspan = SizeOf($names) + 1;
372 elseif ($vals["type"] == 1 || ($vals["type"] == 3 )) // vertical chart
375 $colspan = SizeOf($names) + 1;
378 print '<tr><td class="align-center" valign="center" ';
380 // If a background was choosen don't print cell BGCOLOR
381 if (! $vals["background"]) { print 'style="background-color:' . $vals["hbgcolor"] . '"'; }
383 print ' colspan="' . $colspan . '">';
384 print '<span style="color:' . $vals["hfcolor"] . '; ' . $vals["hfstyle"] . '">';
385 print "<strong>" . $vals["hlabel"] . "</strong>";
386 print '</span></td></tr>';
388 print '<tr><td class="align-center" valign="center" ';
390 // If a background was choosen don't print cell BGCOLOR
391 if (! $vals["background"]) { print 'style="background-color:' . $vals["vbgcolor"] . '"'; }
393 print ' rowspan="' . $rowspan . '">';
394 print '<span style="color:' . $vals["vfcolor"] . '; ' . $vals["vfstyle"] . '">';
395 print "<strong>" . $vals["vlabel"] . "</strong>";
396 print '</span></td>';
401 #######################################################################
403 # Function: end_graph()
405 # Purpose: Prints out the table footer.
407 #######################################################################
411 print "\n</table>\n\n<!-- end inner graph table -->\n\n";
415 #######################################################################
417 # Function: hv_graph_defaults($vals)
419 # Purpose: Sets the default values for the $vals array
421 #######################################################################
423 function hv_graph_defaults($vals)
425 if (!isset($vals["vfcolor"])) { $vals["vfcolor"]="#000000"; }
426 if (!isset($vals["hfcolor"])) { $vals["hfcolor"]="#000000"; }
427 if (!isset($vals["vbgcolor"])) { $vals["vbgcolor"]="#ffffff"; }
428 if (!isset($vals["hbgcolor"])) { $vals["hbgcolor"]="#ffffff"; }
429 if (!isset($vals["cellpadding"])) { $vals["cellpadding"]=0; }
430 if (!isset($vals["cellspacing"])) { $vals["cellspacing"]=0; }
431 if (!isset($vals["border"])) { $vals["border"]=0; }
432 if (!isset($vals["scale"])) { $vals["scale"]=1; }
433 if (!isset($vals["namebgcolor"])) { $vals["namebgcolor"]="#ffffff"; }
434 if (!isset($vals["valuebgcolor"])) { $vals["valuebgcolor"]="#ffffff"; }
435 if (!isset($vals["namefcolor"])) { $vals["namefcolor"]="#000000"; }
436 if (!isset($vals["valuefcolor"])) { $vals["valuefcolor"]="#000000"; }
437 if (!isset($vals["doublefcolor"])) { $vals["doublefcolor"]="#886666"; }
443 #######################################################################
445 # Function: horizontal_graph($names, $values, $bars, $vals)
447 # Purpose: Prints out the actual data for the horizontal chart.
449 #######################################################################
451 function horizontal_graph($names, $values, $bars, $vals)
453 for( $i=0;$i<SizeOf($values);$i++ )
458 <td class="align-right" <?php
459 // If a background was choosen don't print cell BGCOLOR
460 if (! $vals["background"]) { print ' style="background-color:' . $vals["namebgcolor"] . '"'; }
462 <span style="font-size: -1;color:<?php
463 echo $vals["namefcolor"];
465 echo $vals["namefstyle"];
467 echo "\n".$names[$i]; ?>
471 <td align="left" <?php
472 // If a background was choosen don't print cell BGCOLOR
473 if (! $vals["background"]) { print ' style="background-color:' . $vals["valuebgcolor"] . '"'; }
477 // Decide if the value in bar is a color code or image.
478 if (preg_match("/^#/", $bars[$i]))
482 <table align="left" style="background-color:<?php echo $bars[$i] ?>" width="<?php echo $values[$i] * $vals["scale"] ?>">
483 <tr><td> </td></tr>
490 print '<img src="' . $bars[$i] . '"';
491 print ' height="10" width="' . $values[$i] * $vals["scale"] . '" alt= "" />';
493 if (! $vals["noshowvals"])
495 print ' <em><span style="font-size: -2;color:' . $vals["valuefcolor"] . ';'
496 . $vals["valuefstyle"] . '">(';
497 print $values[$i] . ")</span></em>";
506 } // end horizontal_graph
509 #######################################################################
511 # Function: vertical_graph($names, $values, $bars, $vals)
513 # Purpose: Prints out the actual data for the vertical chart.
515 #######################################################################
517 function vertical_graph($names, $values, $bars, $vals)
521 for( $i=0;$i<SizeOf($values);$i++ )
524 print '<td align="center" valign="bottom" ';
526 // If a background was choosen don't print cell BGCOLOR
527 if (! $vals["background"]) { print ' style="background-color:' . $vals["valuebgcolor"] . '"'; }
530 if (! $vals["noshowvals"])
532 print '<em><span style="font-size: -2;color:' . $vals["valuefcolor"] . ';'
533 . $vals["valuefstyle"] . '">(';
534 print $values[$i] . ")</span></em><br />";
538 <img src="<?php echo $bars[$i] ?>" width="5" height="<?php
540 // Values of zero are displayed wrong because a image height of zero
541 // gives a strange behavior in Netscape. For this reason the height
542 // is set at 1 pixel if the value is zero. - Jan Diepens
543 if ($values[$i] != 0)
545 echo $values[$i] * $vals["scale"];
559 for( $i=0;$i<SizeOf($values);$i++ )
562 <td class="align-center" valign="top"
565 // If a background was choosen don't print cell BGCOLOR
566 if (! $vals["background"]) { print ' style="background-color:' . $vals["namebgcolor"] . '"'; }
569 <span style="font-size: -1;color:<?php echo $vals["namefcolor"] ?>;<?php echo $vals["namefstyle"] ?>">
570 <?php echo $names[$i] ?>
576 } // end vertical_graph
579 #######################################################################
581 # Function: double_horizontal_graph($names, $values, $bars,
582 # $vals, $dvalues, $dbars)
584 # Purpose: Prints out the actual data for the double horizontal chart.
586 #######################################################################
588 function double_horizontal_graph($names, $values, $bars, $vals, $dvalues, $dbars)
590 for( $i=0;$i<SizeOf($values);$i++ )
594 <td class="align-right"
596 // If a background was choosen don't print cell BGCOLOR
597 if (! $vals["background"]) { print ' style="background-color:' . $vals["namebgcolor"] . '"'; }
600 <span style="font-size: -1;color:<?php echo $vals["namefcolor"] ?>;<?php echo $vals["namefstyle"] ?>">
601 <?php echo $names[$i] ?>
606 // If a background was choosen don't print cell BGCOLOR
607 if (! $vals["background"]) { print ' style="background-color:' . $vals["valuebgcolor"] . '"'; }
610 <table align="left" width="<?php echo $dvalues[$i] * $vals["scale"] ?>">
613 // Set background to a color if it starts with # or
614 // an image otherwise.
615 if (preg_match("/^#/", $dbars[$i])) { print 'style="background-color:' . $dbars[$i] . '">'; }
616 else { print 'background="' . $dbars[$i] . '">'; }
620 // Decide if the value in bar is a color code or image.
621 if (preg_match("/^#/", $bars[$i]))
625 style="background-color:"<?php echo $bars[$i] ?>"
626 width="<?php echo $values[$i] * $vals["scale"] ?>">
627 <tr><td> </td></tr>
633 print '<img src="' . $bars[$i] . '"';
634 print ' height="10" width="' . $values[$i] * $vals["scale"] . '" alt="" />';
637 if (! $vals["noshowvals"])
639 print '<em><span style="font-size: -3:color:' . $vals["valuefcolor"] . ';'
640 . $vals["valuefstyle"] . '">(';
641 print $values[$i] . ")</span></em>";
648 if (! $vals["noshowvals"])
650 print '<em><span style="font-size:-3;color:' . $vals["doublefcolor"] . ';'
651 . $vals["valuefstyle"] . '">(';
652 print $dvalues[$i] . ")</span></em>";
660 } // end double_horizontal_graph
663 #######################################################################
665 # Function: double_vertical_graph($names, $values, $bars, $vals, $dvalues, $dbars)
667 # Purpose: Prints out the actual data for the double vertical chart.
669 # Author: Jan Diepens
671 #######################################################################
673 function double_vertical_graph($names, $values, $bars, $vals, $dvalues, $dbars)
677 for( $i=0;$i<SizeOf($values);$i++ )
680 print '<td class="align-center" valign="bottom" ';
681 // If a background was choosen don't print cell BGCOLOR
682 if (! $vals["background"]) { print ' style="background-color:' . $vals["valuebgcolor"] . '"'; }
685 print '<table><tr><td class="align-center" valign="bottom" ';
687 // If a background was choosen don't print cell BGCOLOR
688 if (! $vals["background"]) { print ' style="background-color:' . $vals["valuebgcolor"] . '"'; }
691 if (! $vals["noshowvals"])
693 print '<em><span style="font-size:-2;color:' . $vals["valuefcolor"] . ';'
694 . $vals["valuefstyle"] . '">(';
695 print $values[$i] . ")</span></em><br />";
699 <img src="<?php echo $bars[$i] ?>" width="10" height="<?php if ($values[$i]!=0){
700 echo $values[$i] * $vals["scale"];
701 } else { echo "1";} ?>" alt="" />
702 </td><td class="align-center" valign="bottom"
704 // If a background was choosen don't print cell BGCOLOR
705 if (! $vals["background"]) { print ' style="background-color:' . $vals["valuebgcolor"] . '"'; }
708 if (! $vals["noshowvals"])
710 print '<em><span style="font-size:-2;color:' . $vals["doublefcolor"] . ';'
711 . $vals["valuefstyle"] . '">(';
712 print $dvalues[$i] . ")</span></em><br />";
716 <img src="<?php echo $dbars[$i] ?>" width="10" height="<?php if ($dvalues[$i]!=0){
717 echo $dvalues[$i] * $vals["scale"];
718 } else { echo "1";} ?>" alt="" />
726 for( $i=0;$i<SizeOf($values);$i++ )
729 <td class="align-center" valign="top"
732 // If a background was choosen don't print cell BGCOLOR
733 if (! $vals["background"]) { print ' style="background-color:' . $vals["namebgcolor"] . '"'; }
736 <span style="font-size:-1;color:<?php echo $vals["namefcolor"] ?>;<?php echo $vals["namefstyle"] ?>">
737 <?php echo $names[$i] ?>
743 } // end double_vertical_graph
748 #######################################################################
750 # Function: horizontal_absolute_multi_graph($names, $multi_rows,
753 # $multi_rows - array of arrays of values (may be seen as
754 # array of columns - column for first color, for second, etc.)
755 # $colors - array of color names or codes
756 # $additive - treat data as absolute values (will be
757 # differentiated for drawing, and hence should be non-decreasing
758 # sequence) or additive (just stick one on another).
760 # Purpose: Prints out the actual data for the horizontal chart of
761 # bars with multiple sections
763 #######################################################################
765 function horizontal_multisection_graph($names, $multi_rows, $colors, $vals, $additive=false)
767 $subbars_num=SizeOf($multi_rows);
768 for( $i=0;$i<SizeOf($names);$i++ )
773 <td class="align-right" <?php
774 // If a background was choosen don't print cell BGCOLOR
775 if (! $vals["background"]) {
776 print ' style="background-color:' . $vals["namebgcolor"] . '"';
779 <span style="font-size:-1;color:<?php echo $vals["namefcolor"]; ?>;<?php echo $vals["namefstyle"]; ?>">
781 echo "\n".$names[$i]; ?>
785 <td align="left" <?php
786 // If a background was choosen don't print cell BGCOLOR
787 if (! $vals["background"]) {
788 print ' style="background-color:' . $vals["valuebgcolor"] . '"';
792 echo '<table align="left"><tr>'."\n";
795 for( $j=0;$j<$subbars_num;$j++ ) {
796 $width=$multi_rows[$j][$i];
797 if (!$additive) $width-=$prev_val;
798 if ($width<=0 && ($j!=$subbars_num-1 || $shown)) continue;
799 // make sure that we show at least stump, but only one
801 $prev_val=$multi_rows[$j][$i];
802 $pix_width=$width * $vals["scale"];
803 echo "<td style=\"background-color:".$colors[$j]."\" width=\"".$pix_width."\"> </td>";
805 echo '</tr></table>';
807 if (! $vals["noshowvals"]) {
808 print ' <em><span style="font-size:-2;color:' . $vals["valuefcolor"] . ';'
809 . $vals["valuefstyle"] . '"> (';
810 for( $j=0;$j<SizeOf($multi_rows);$j++ ) {
812 print $multi_rows[$j][$i];
814 print ")</span></em>";
823 } // end horizontal_graph
825 function graph_calculate_scale($multi_rows,$width) {
827 $rows_num=count($multi_rows);
829 for ($row_i = 0; $row_i < $rows_num; $row_i++) {
830 $row=$multi_rows[$row_i];
831 $counter=count($row);
833 for ($i = 0; $i < $counter; $i++) {
834 if ($row[$i] > $max_value) {
840 if ($max_value < 1) {
844 $scale=($width/$max_value);