越简单越好!

PHP生成柱状图的类 - 水平

发表于 2007-10-22 14:12 | 1372次阅读 0次点赞   PHP
<?php
class histogram_h
{
    /***********************************
     * H: horizontal(水平)
     * HTML 柱状图
     * 设计: 第二信息(www.DearInfo.com) / 唐辉
     * 源稿设计日期: 2004年11月11日
     * PHP社区: http://php.DearInfo.com
     * 转载时请保留作者信息及出处, 多谢!
     * 使用方法:
     * $htg        = new histogram_h;
     * $htg->num    = "250,370,621,720,326,455,132,345,611,126,551,266";
     * $htg->label    = "1月,2月,3月,4月,5月,6月,7月,8月,9月,10月,11月,12月";
     * $htg->show();
     ************************************/

    //数值
    var $num;

    //低部标签值
    var $label;

    //柱状图宽度, 默认为399, 用户可自定义
    var $width=399;

    //每条柱的高度,默认为25, 用户可自定义
    var $perheight=25;
  
    //柱与柱之间的间隔
    var $cellspacing=5;

    function show()
    {
      //获取外部数值
      $shu=split(",",$this->num);
      //获取标签值
      $biaoqian=split(",",$this->label);
      //颜色, 默认为20种, 如果客户数据超过20种, 则回到第一种, 如此循环.
      $color    = array('#97bd00','#009900','#cc3300',
                '#ffcc00','#3366cc','#33cc33',
                '#ff9933','#cccc99','#99cc66',
                '#66ff99','#4f6600','#003300',
                '#481000','#7d6400','#173064',
                '#1a6a1a','#974b00','#78793c',
                '#557e27','#009337');        
      if ($shu=="") die("error id:1");
      $shuju=split(",",$shu);
      //计算最大值
      for($i=0;$i<count($shu);$i++)
      {
        if(!is_numeric($shu[$i])) die("error id:2");
        if($shu[$i]>$zuidashu) $zuidashu=$shu[$i];
      }
    ?>


    <table border="0" cellpadding="0" cellspacing="<?php echo $this->cellspacing; ?>">
      <?php
        for($i=0,$j=0;$i<count($shu);$i++,$j++)
        {
          if($j>19){$j=0;}
      ?>

    <tr>
      <td><p align="right"><?php echo    /*打印左边标签*/ $biaoqian[$i]; ?></td>
      <td>
     <table border="0" cellpadding="0" cellspacing="0" height="<?php echo $this->perheight; ?>">
       <tr>
         <td width="<?echo floor(($this->width/$zuidashu)*$shu[$i]);?>" bgcolor="<?php /*柱状颜色*/ echo $color[$j]; ?>">&nbsp;</td>
         <td>&nbsp;<?php /*在柱状图的顶端输出数值*/ echo $shu[$i]; ?></td>
       </tr>
     </table>
     </td>
    </tr>
<?php
        }
?>
</table>
<?php
    } //End function
} // End histogram Class
?>
返回顶部 ^