越简单越好!

PHP生成柱状图的类 - 纵向

发表于 2007-10-22 14:09 | 1107次阅读 0次点赞   PHP
<?php
class histogram_v
{
    /***********************************
     * V: vertical(纵)
     * HTML 柱状图
     * 设计: 第二信息(www.DearInfo.com) / 唐辉
     * 源稿设计日期: 2004年11月11日
     * PHP社区: http://php.DearInfo.com
     * 转载时请保留作者信息及出处, 多谢!
     * 使用方法:
     * $htg        = new histogram_v;
     * $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;

    //柱状图高度, 默认为200, 用户可自定义
    var $height=200;

    //每条柱的宽度,默认为25, 用户可自定义
    var $perwidth=25;

    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>
      <tr valign="bottom"><?php /*一定要设置为低部对齐*/ ?>
      <?php
        for($i=0,$j=0;$i<count($shu);$i++,$j++)
        {
          if($j>19){$j=0;}
      ?>
      <td align="center">
      <?php
        /*在柱状图的顶端输出数值*/
        echo $shu[$i];
      ?>
          <table height="<?echo floor(($this->height/$zuidashu)*$shu[$i]);?>" border=0>
          <tr>
          <td bgcolor="<?php /*柱状颜色*/ echo $color[$j]; ?>" width="<?php /*第条柱的宽度*/ echo $this->perwidth; ?>"></td>
            </tr>
        </table><font color="#3F7F9F"><?php /*打印低部对应标签*/ echo $biaoqian[$i];?></font>
        </td>
      <?php } ?>
      </tr>
    </table>
    <?php
    } //End function
} // End histogram Class

?>
返回顶部 ^