PHP生成条形图的方法

yipeiwu_com6年前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 session的传递方式

学习PHP session的传递方式

本文实例为大家分享了PHP session的三种传递方式,供大家参考,具体内容如下 既然学习到了就做下笔记,解决数据的共享,在也不要担心,什么时候还要你自己手动去设置打开cookie了!...

PHP 源代码分析 Zend HashTable详解第1/3页

HashTable在通常的数据结构教材中也称作散列表,哈希表。其基本原理比较简单(如果你对其不熟悉,请查阅随便一本数据结构教材或在网上搜索),但PHP的实现有其独特的地方。理解了Hash...

php判断当前用户已在别处登录的方法

本文实例讲述了php判断当前用户已在别处登录的方法。分享给大家供大家参考。具体分析如下: 主要思路如下: 1.登录时,将用户的SessionID记录下来 2.验证登录时,将记录的该用户S...

php表单请求获得数据求和示例

获得表单请求的值: 案例: request.php 复制代码 代码如下: <html> <head> <meta http-equiv="content-t...

PHP实现基于图的深度优先遍历输出1,2,3...n的全排列功能

本文实例讲述了PHP实现基于图的深度优先遍历输出1,2,3...n的全排列功能。分享给大家供大家参考,具体如下: <?php $n=$_REQUEST["n"]; if...