Location: PHPKode > scripts > HTML Graph maker > html-graph-maker/Graph.php
<?
class Graph
{
  var $HPath = "";
  var $GraphHeight = 200;
  var $GraphWidth = 300;
  var $VerticalThicks = 8;
  var $SeriesIDs = Array();
  var $SeriesDesc = Array();
  var $SeriesValues = Array();
  var $SeriesPivot = Array();
  var $ShowVerticalThicks = false;
  var $LineWidth = 1;
  var $ShowLegenda = true;
  var $ShowPivotX = true;
  var $ShowPivotY = true;
  var $SeriesColors = Array(
                            "/pix_red.gif",
                            "/pix_green.gif",
                            "/pix_blu.gif",
                            "/pix_cyan.gif",
                            "/pix_magenta.gif");
  var $GraphMode = "Bar";

  function ShowLegenda($Bool){
    $this->ShowLegenda = $Bool;
  }
  function ShowPivotX($Bool){
    $this->ShowPivotX = $Bool;
  }
  function ShowPivotY($Bool){
    $this->ShowPivotY = $Bool;
  }

  function SetGraphMode($Mode){
    $this->GraphMode = $Mode;
  }

  function SetLineWidth($LW){
    if($LW < 1){
      $LW = 1;
    }
    $this->LineWidth = $LW;
  }

  function SetGraphHeight($H){
    $this->GraphHeight = $H;
  }
  function SetGraphWidth($W){
    $this->GraphWidth = $W;
  }
  function SetVerticalThicks($VT){
    $this->VerticalThicks = $VT;
  }

  function ShowVerticalThicks($TrueFalse){
    $this->ShowVerticalThicks = $TrueFalse;
  }

  function SetClassDir($HDir){
    if(substr($HDir,-1)=="/"){
      $HDir = substr($HDir,0,strlen($HDir)-1);
    }
    $this->HPath = trim($HDir);
  }

  function AddSerie($SerieID,$Description){
    $this->SeriesIDs[] = trim($SerieID);
    $this->SeriesDesc[trim($SerieID)] = trim($Description);
  }

  function SetSerieValues($SerieID,$ArrayValues){
    $this->SeriesValues[trim($SerieID)] = $ArrayValues;
  }
  function SetSeriePivot($ArrayPivot){
    $this->SeriesPivot = $ArrayPivot;
  }

  function DrawGraph(){
    $HTML_PathToLibrary = $this->HPath."/GraphImg";
    $NumberOfData = count($this->SeriesValues[$this->SeriesIDs[0]]);
    if($NumberOfData>0){
      $NumberOfCols =  ($NumberOfData * count($this->SeriesIDs));
      $ColStep = count($this->SeriesIDs);

      $NumberOfRows = $this->VerticalThicks * 2;
      $VarRowH = round(($this->GraphHeight - $NumberOfRows) / $this->VerticalThicks);
      $VarColW = round(($this->GraphWidth - $NumberOfCols) / ($NumberOfData*$ColStep));
      $VarSpanHeight=($VarRowH*$this->VerticalThicks)+($this->LineWidth*$this->VerticalThicks);
      #Search Max Value
      $MaxValue = 0;
      for($CSerie=0; $CSerie<count($this->SeriesIDs); $CSerie++){
        for ($CData=0; $CData<$NumberOfData; $CData++){
          $theValue=$this->SeriesValues[$this->SeriesIDs[$CSerie]][$CData];
          if($MaxValue<$theValue){
            $MaxValue = $theValue;
          }
        }
      }
      ?>
      <table border=0 cellspacing=0 cellpadding=0>
      <?
      for ($Rows = 0; $Rows<$NumberOfRows; $Rows+=2){
        $thisVerticalVal = round((($MaxValue / $this->VerticalThicks) * (($NumberOfRows - $Rows)/2)));
        if(!$this->ShowPivotY){
          $thisVerticalVal = "";
        }
        ?>
          <tr height="0px">
            <td rowspan=2 style="font-family: Tahoma, Verdana, Geneva, sans-serif; font-size: 9px;"><? echo $thisVerticalVal; ?></td>
            <td width="0px" align=right><img src="<? echo $HTML_PathToLibrary; ?>/pix_black.gif" height="<? echo $this->LineWidth; ?>" width="14px"></td>
            <?
            if($Rows==0){
              for($Cols=0; $Cols<$NumberOfCols; $Cols+=$ColStep){
                for($DataCol=0; $DataCol<$ColStep; $DataCol++){
                  $thisVal = $this->SeriesValues[$this->SeriesIDs[$DataCol]][$Cols/$ColStep];
                  $thisHei = round(($VarSpanHeight / $MaxValue) * $thisVal);
                  $LImage = $this->SeriesColors[$DataCol];
                  $HTrasp = $VarSpanHeight-$thisHei;
                  $HPic = $thisHei;
                  #Print the Data
                  if (strtoupper($this->GraphMode) == "BAR"){
                    if($HTrasp==0){
                      $HTrasp ++;
                      $HPic --;
                    }
                    if($HPic==0){
                      $HTrasp --;
                      $HPic ++;
                    }
                    echo "
                      <td rowspan=\"$NumberOfRows\" width=\"".$VarColW."px\" valign=\"bottom\"><img src=\"$HTML_PathToLibrary/transp.gif\" width=\"".$VarColW."px\" height=\"".($HTrasp)."px\"><img src=\"$HTML_PathToLibrary/".$LImage."\" width=\"".$VarColW."px\" height=\"".$HPic."px\" alt=\"$thisVal\"></td>
                      ";
                  }
                  else{
                    if($HTrasp==0){
                      $HTrasp ++;
                      $HPic --;
                    }
                    if($HPic<5){
                      $HTrasp = $HTrasp - (5-$HPic);
                      $HPic = 5;
                    }
                    echo "
                      <td rowspan=\"$NumberOfRows\" width=\"".$VarColW."px\" valign=\"bottom\"><img src=\"$HTML_PathToLibrary/transp.gif\" width=\"".$VarColW."px\" height=\"".($HTrasp)."px\"><img src=\"$HTML_PathToLibrary/".$LImage."\" width=\"".$VarColW."px\" height=\"4px\" alt=\"$thisVal\"><img src=\"$HTML_PathToLibrary/transp.gif\" width=\"".$VarColW."px\" height=\"".($HPic-4)."px\"></td>
                      ";
                  }
                }
                if($this->ShowVerticalThicks){
                  echo "
                    <td rowspan=$NumberOfRows width=".$this->LineWidth."px  style=\"background: url($HTML_PathToLibrary/mark_v.gif); background-repeat: repeat-y;\"><img src=\"$HTML_PathToLibrary/transp.gif\" width=\"".$this->LineWidth."px\" height=\"".$VarSpanHeight."px\"></td>
                  ";
                }
                else{
                  echo "
                    <td rowspan=$NumberOfRows width=".$this->LineWidth."px><img src=\"$HTML_PathToLibrary/transp.gif\" width=\"".$this->LineWidth."px\" height=\"".$VarSpanHeight."px\"></td>
                  ";
                }
              }
              # Legenda
              echo "
                    <td rowspan=$NumberOfRows>
                      <table border=0 cellspacing=0 cellpadding=0>
              ";
              if($this->ShowLegenda){
                for ($LRows = 0; $LRows<$NumberOfRows; $LRows+=2){
                  $StepSerie = ($LRows / 2);
                  if ($StepSerie < count($this->SeriesIDs)){
                    $SerID = $this->SeriesIDs[$StepSerie];
                    $thisDesc = $this->SeriesDesc[$SerID];
                    $LImage = $this->SeriesColors[$StepSerie];
                    echo "
                           <tr>
                             <td><img src=\"$HTML_PathToLibrary/transp.gif\" width=\"20px\" height=\"1px\"></td>
                             <td width=10px><img border=1 src=\"$HTML_PathToLibrary/".$LImage."\" width=\"10px\" height=\"10px\"></td>
                             <td valign=middle>&nbsp;&nbsp;$thisDesc</td>
                           </tr>
                         ";
                  }
                 }
               }
               echo "
                     </table>
                   </td>
               ";
            }
            ?>
          </tr>
          <tr height=<? echo $VarRowH; ?>px>
            <td width=14px align=right><img src="<? echo $HTML_PathToLibrary; ?>/pix_black.gif" height="<? echo $VarRowH; ?>px" width="<? echo $this->LineWidth; ?>px"></td>
          </tr>
        <?
      }
      # X Axis
      echo "
          <tr>
            <td>&nbsp;</td>
            <td valign=top><img src=\"$HTML_PathToLibrary/pix_black.gif\" width=\"14px\" height=\"".$this->LineWidth."\"></td>
           ";
      for($Cols=0; $Cols<$NumberOfCols; $Cols+=$ColStep){
        for($DataCol=0; $DataCol<$ColStep; $DataCol++){
          echo "
                    <td valign=top width=\"".$VarColW."px\"><img src=\"$HTML_PathToLibrary/pix_black.gif\" width=\"".$VarColW."px\" height=\"".$this->LineWidth."\"></td>
               ";
        }
        echo "
                  <td width=".$this->LineWidth."px><img src=\"$HTML_PathToLibrary/bar_o.gif\" width=\"".$this->LineWidth."px\" height=\"14px\"></td>
             ";
      }
      echo "
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td valign=top><img src=\"$HTML_PathToLibrary/transp.gif\" width=\"14px\" height=\"".$this->LineWidth."\"></td>
           ";
      for($Cols=0; $Cols<$NumberOfCols; $Cols+=$ColStep){
        $OutStr = "";
        if($this->ShowPivotX){
          for($a=0; $a<strlen($this->SeriesPivot[($Cols/$ColStep)]); $a++){
            $OutStr .= "<tr height=0px><td width=0px style=\"font-family: Tahoma, Verdana, Geneva, sans-serif; font-size: 9px;\">".substr($this->SeriesPivot[($Cols/$ColStep)],$a,1)."</td></tr>";
          }
        }
        echo "
                  <td colspan=".($ColStep+1)." valign=top align=center width=\"".$VarColW."px\"><table border=0 cellspacing=0 cellpadding=0>$OutStr</table></td>
             ";
      }
      echo "
            <td>&nbsp;</td>
          </tr>
        </table>
      ";
    }
  }
}
?>
Return current item: HTML Graph maker