PHP生成条形图的方法

 更新时间:2014年12月10日 14:30:26   投稿:shichen2014  
这篇文章主要介绍了PHP生成条形图的方法,可实现生成柱状的条形图,适用于一些类似柱状图显示报表的场合,具有一定的实用价值,需要的朋友可以参考下

本文实例讲述了PHP生成条形图的方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:

<?php
  // create an array of values for the chart. These values 
  // could come from anywhere, POST, GET, database etc. 
  $values = array(23,32,35,57,12,3,36,54,32,15,43,24,30);
 
  // now we get the number of values in the array. this will 
  // tell us how many columns to plot 
    $columns  = count($values);
 
  // set the height and width of the graph image
 
    $width = 300; 
    $height = 200;
 
  // Set the amount of space between each column 
    $padding = 5;
 
  // Get the width of 1 column 
    $column_width = $width / $columns ;
 
  // set the graph color variables 
    $im        = imagecreate($width,$height); 
    $gray      = imagecolorallocate ($im,0xcc,0xcc,0xcc); 
    $gray_lite = imagecolorallocate ($im,0xee,0xee,0xee); 
    $gray_dark = imagecolorallocate ($im,0x7f,0x7f,0x7f); 
    $white     = imagecolorallocate ($im,0xff,0xff,0xff);
 
  // set the background color of the graph 
    imagefilledrectangle($im,0,0,$width,$height,$white);
 
 
  // Calculate the maximum value we are going to plot 
  $max_value = max($values);
 
  // loop over the array of columns 
    for($i=0;$i<$columns;$i++) 
        {
    // set the column hieght for each value 
        $column_height = ($height / 100) * (( $values[$i] / $max_value)
 
*100); 
    // now the coords
        $x1 = $i*$column_width; 
        $y1 = $height-$column_height; 
        $x2 = (($i+1)*$column_width)-$padding; 
        $y2 = $height;
 
        // write the columns over the background 
        imagefilledrectangle($im,$x1,$y1,$x2,$y2,$gray);
 
        // This gives the columns a little 3d effect 
        imageline($im,$x1,$y1,$x1,$y2,$gray_lite); 
        imageline($im,$x1,$y2,$x2,$y2,$gray_lite); 
        imageline($im,$x2,$y1,$x2,$y2,$gray_dark); 
        }
 
   // set the correct png headers 
   header ("Content-type: image/png"); 
  // spit the image out the other end 
  imagepng($im); 
?>

运行效果如下图所示:

希望本文所述对大家的PHP程序设计有所帮助。

相关文章

  • 提高php运行速度的一些小技巧分享

    提高php运行速度的一些小技巧分享

    使用PHP的最大1个优势就是速度快。一般情况下,PHP总是具有足够的速度支持Web内容动态生成,许多时候甚至无法找出比它更快的方法。然而,当面对庞大的访问量、高负荷的应用、有限的带宽,以及其他各种带来性能瓶颈的因素时,就需要考虑怎样提高PHP的性能了
    2012-07-07
  • php操作access数据库的方法详解

    php操作access数据库的方法详解

    这篇文章主要介绍了php操作access数据库的方法,结合实例形式详细分析了php基于ADOdb、PDO及ODBC等方式连接与操作access数据库的具体实现技巧与相关注意事项,需要的朋友可以参考下
    2017-02-02
  • PHP中常用的字符串格式化函数总结

    PHP中常用的字符串格式化函数总结

    这篇文章主要介绍了PHP中常用的字符串格式化函数总结,本文讲解的函数是WEB中经常用到的,例如取出空格和字符串填补函数、字符串大小写的转换、和HTML标签相关的字符串格式化等,需要的朋友可以参考下
    2014-11-11
  • PHP+Tidy-完美的XHTML纠错+过滤

    PHP+Tidy-完美的XHTML纠错+过滤

    PHP+Tidy-完美的XHTML纠错+过滤...
    2007-04-04
  • PHP二维数组排序简单实现方法

    PHP二维数组排序简单实现方法

    这篇文章主要介绍了PHP二维数组排序简单实现方法,涉及PHP针对数组的遍历与排序操作常用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-02-02
  • 8个PHP数组面试题

    8个PHP数组面试题

    这篇文章主要介绍了8个PHP数组面试题,例如写函数创建长度为10的数组,数组中的元素为递增的奇数,首项为1、创建长度为10的数组,数组中的数为递增的等比数,比值为3,首项为等题目,需要的朋友可以参考下
    2015-06-06
  • php获取服务器信息的实现代码

    php获取服务器信息的实现代码

    php获取服务器信息的一段代码,包括获取mysql数据库版本信息、GD库信息、php上传相关信息等。有需要的朋友,可以参考下
    2013-02-02
  • php5中date()得出的时间为什么不是当前时间的解决方法

    php5中date()得出的时间为什么不是当前时间的解决方法

    从php5.10开始,php中加入了时区的设置,在php中显示的时间都是格林威治标准时间,这就造成了我们 中国的用户会差八个小时的问题!
    2008-06-06
  • PHP 开发环境配置(Zend Server安装)

    PHP 开发环境配置(Zend Server安装)

    运行安装文件(ZendServer-CE-php-5.3.2-5.0.1-Windows_x86.exe)开始安装,选项请参照我的选择。
    2010-04-04
  • phpmyadmin中配置文件现在需要绝密的短语密码的解决方法

    phpmyadmin中配置文件现在需要绝密的短语密码的解决方法

    phpmyadmin中配置文件现在需要绝密的短语密码的解决方法...
    2007-02-02

最新评论