Login   Register  
PHP Classes
elePHPant
Icontem

File: HTML_graph.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of trawa  >  HTML_graph  >  HTML_graph.php  >  Download  
File: HTML_graph.php
Role: ???
Content type: text/plain
Description: includes examples
Class: HTML_graph
Author: By
Last change:
Date: 13 years ago
Size: 6,304 bytes
 

Contents

Class file image Download
<?
/*
Piotr Trawinski <piotr.trawinski@weblab.pl>  2001.05.01

Modified by Clive Smart <clive@dev.co.za>    2001.05.02

If you make any improvements to this class please let me
know so that i can put them up here.

Examples included on the bottom
This software is Free like Linux
*/
class HTML_graph {
        var $text;            // Text describtions of elements
        var $value;           // Element values
        var $size;            // (array) Width or height of the image
        var $size2;           // constant for all elements height or width
        var $decp;            // Decimal Places for Percentage
        var $colour;          // Bar Colour - Hex eg FACE00
        var $bgcolour;        // Background Colour - Hex eg FACE00
        var $border;          // Border Size
        var $scale;           // Maximum x/y Scale
        var $perc;            // (array) Percentage values after calculations
        var $elements;        // Number of elements to be shown

        function set($text,$value,$scale,$size2,$decp=2,$colour="FF0000",$bgcolour="",$border=0) {
                $this->text      = $text;
                $this->value     = $value;
                $this->scale     = $scale;
                $this->size2     = $size2;
                $this->decp      = $decp;
                $this->colour    = $colour;
                $this->bgcolour  = $bgcolour;
                $this->border    = $border;
        }
        function calculate() {
        // Calculate Colours
                $this->r = hexdec(substr($this->colour,0,2));
                $this->g = hexdec(substr($this->colour,2,2));
                $this->b = hexdec(substr($this->colour,4,2));
                $this->elements = count($this->value);
        // Calculate Values
                for ($i=0; $i < $this->elements; $i++) {
                        $sumvalue += $this->value[$i];
                }
                for ($i=0; $i < $this->elements;$i++) {
                        if ($this->value[$i] != 0) {
                                // Fixed the Percentage Calculation to be more accurate
                                $bar_perc = (100 * $this->value[$i] / $sumvalue);
                                $bar_perc = $bar_perc * pow(10,$this->decp);
                                $bar_perc = round($bar_perc);
                                $bar_perc = $bar_perc / pow(10,$this->decp);
                                $this->size[$i] = (int)($this->value[$i] * ($this->scale / $sumvalue));
                        } elseif ($this->value[$i]==0) {
                                $bar_perc=0.00;
                        }
                        // Handle Decimal Places
                        if ($this->decp > 0) {
                           $prtf = "%.".$this->decp."f%% ";
                        } else {
                           $prtf = "%d%% ";
                        }
                        $this->perc[$i] = sprintf($prtf, $bar_perc);
                }
        }
        function sort() { // Function to sort values descending ...
                                #in the future
        }

        function horizontal() {     // Needs: @text @value  @perc  @size  $elements  $height
                $this->calculate();
                $html = "<TABLE cellspacing=3 cellpadding=3 border=".$this->border." bgcolor=#".$this->bgcolour.">\n  <TR>\n    <TD>\n";
                $html .= "      <TABLE align=center cellspacing=0 cellpadding=0 border=0>\n";
                for ($i=0; $i < $this->elements; $i++) {
                        $html .= "        <TR>\n          <TD height=15 align=right>".$this->text[$i]."</TD>\n";
                        $html .= "          <TD>&nbsp;</TD>\n          <TD valign=top>";
                        if ($this->value[$i] != 0) {
                                $html .= "<IMG src='im.php3?r=".$this->r."&g=".$this->g."&b=".$this->b."' height=".$this->size2." width=".$this->size[$i]."> ";
                        }
                        $html .= $this->perc[$i]." (".($this->value[$i]).")</TD>\n";
                }
                $html .= "        </TR>\n      </TABLE>\n";
                $html .= "    </TD>\n  </TR>\n</TABLE>\n";
                return $html;
        }
        function vertical () {
                $html = "<TABLE cellspacing=3 cellpadding=3 border=".$this->border." bgcolor=#".$this->bgcolour.">\n  <TR>\n    <TD>\n";
                $html .= "      <TABLE align=center cellspacing=0 cellpadding=0 border=0>\n        <TR>\n";
                for ($i=0; $i < $this->elements; $i++) {
                        $html .= "          <TD valign=bottom align=middle>".$this->perc[$i]."<BR>";
                        if ($this->value[$i] != 0) {
                                $html .= "<IMG src='im.php3?r=".$this->r."&g=".$this->g."&b=".$this->b."' width=".$this->size2." height=".$this->size[$i].">";
                        }
                        $html .= "</TD>\n";
                }
                $html .= "        </TR>\n        <TR>\n";
                for ($i=0; $i < $this->elements; $i++) {
                        $html .= "          <TD width=20 valign=top align=middle>(".$this->value[$i].")<BR>".$this->text[$i]."</TD>\n";
                }
                $html .= "        </TR>\n      </TABLE>\n";
                $html .= "    </TD>\n  </TR>\n</TABLE>\n";
                return $html;
        }

}
// Examples

Header("Content-type:  text/html");

$bar = new HTML_graph;

$text = array("Absolutly","Yes","No","Not sure");
$value = array (100,50,40,50);

# There are 4 input values
#     Required
#       1. text elements,
#       2. Numerical values,
#       3. Scale - sets the maximum width or height for a bar
#       4. Value constant for all all elements  height (horizontal) or width (vertical)
#     Optional
#       5. Decimal Places to display in the Percentage - Default=2
#       6. Bar Colour in Hex (eg. A0A0A8) - Default=FF0000 (Red)
#       7. Background Colour in Hex (eg. A0A0A8) - Default='' (None)
#       8. Boder Size - Default=0

$bar->set($text,$value,300,10,0,"FACE00","A0A0A0",2);

echo $bar->horizontal();
echo "<HR>";
echo $bar->vertical();

?>