PHP生成条形图的方法

yipeiwu_com5年前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实现代码,供大家参考,具体内容如下 实现代码: <!DOCTYPE html> <html> <head&g...

PHP编程基本语法快速入门手册

php脚本的后面名为.php,代码放置在下面的括号里面: <?php ....... ?> echo可以打印信息,类似于printf。 <&...

PHP面向对象程序设计之接口用法

接口是PHP面向对象程序设计中非常重要的一个概念。本文以实例形式较为详细的讲述了PHP接口的用法。具体如下: 接口:interface 在PHP中,我们可以规定,一个对象应该具有哪些公共...

浅谈php调用python文件

浅谈php调用python文件

关于PHP调用Python数据传输问题 这是以前大学时做项目出现的问题,现在把它挪上来,希望给遇到问题的未来大佬给出一些小的思路,请大佬们不要大意的帮我改正,如果出现问题或者有更好的解决...

php获得客户端浏览器名称及版本的方法(基于ECShop函数)

php获得客户端浏览器名称及版本的方法(基于ECShop函数)

本文实例讲述了php获得客户端浏览器名称及版本的方法。分享给大家供大家参考,具体如下: 看到ecshop中有这么一个函数get_user_browser(),获取浏览器的名称和版本。虽然...