PHP生成条形图的方法


Posted in PHP onDecember 10, 2014

本文实例讲述了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连mysql和oracle数据库性能比较
Oct 09 PHP
最省空间的计数器
Oct 09 PHP
Discuz! Passport 通行证整合
Mar 27 PHP
解析php中用PHPMailer来发送邮件的示例(126.com的例子)
Jun 24 PHP
使用php方法curl抓取AJAX异步内容思路分析及代码分享
Aug 25 PHP
PHP return语句另类用法不止是在函数中
Sep 17 PHP
使用Composer安装Yii框架的方法
Mar 15 PHP
9个比较实用的php代码片段
Mar 15 PHP
ThinkPHP框架整合微信支付之Native 扫码支付模式一图文详解
Apr 09 PHP
解决Laravel5.2 Auth认证退出失效的问题
Oct 14 PHP
PHP call_user_func和call_user_func_array函数的简单理解与应用分析
Nov 25 PHP
PHP基于进程控制函数实现多线程
Dec 09 PHP
php自定文件保存session的方法
Dec 10 #PHP
php通过session防url攻击方法
Dec 10 #PHP
php利用cookies实现购物车的方法
Dec 10 #PHP
php针对cookie操作的队列操作类实例
Dec 10 #PHP
php利用cookie实现自动登录的方法
Dec 10 #PHP
PHP使用header()输出图片缓存实例
Dec 09 #PHP
PHP实现服务器状态监控的方法
Dec 09 #PHP
You might like
全国FM电台频率大全 - 4 山西省
2020/03/11 无线电
PHP基于自定义函数生成笛卡尔积的方法示例
2017/09/30 PHP
postman的安装与使用方法(模拟Get和Post请求)
2018/08/06 PHP
php图片裁剪函数
2018/10/31 PHP
JavaScript实现关键字高亮功能
2014/11/12 Javascript
js实现自定义路由
2017/02/04 Javascript
浅析webpack 如何优雅的使用tree-shaking(摇树优化)
2017/08/16 Javascript
实例分析编写vue组件方法
2019/02/12 Javascript
JQuery获取可视区尺寸和文档尺寸及制作悬浮菜单示例
2019/05/14 jQuery
[01:56]《DOTA2》中文配音CG
2013/04/22 DOTA
Python判断操作系统类型代码分享
2014/11/22 Python
实践Python的爬虫框架Scrapy来抓取豆瓣电影TOP250
2016/01/20 Python
python实现mysql的读写分离及负载均衡
2018/02/04 Python
python使用matplotlib库生成随机漫步图
2018/08/27 Python
python接口自动化(十六)--参数关联接口后传(详解)
2019/04/16 Python
树莓派采用socket方式文件传输(python)
2019/06/22 Python
使用python打印十行杨辉三角过程详解
2019/07/10 Python
python基于property()函数定义属性
2020/01/22 Python
python_array[0][0]与array[0,0]的区别详解
2020/02/18 Python
在python3中使用shuffle函数要注意的地方
2020/02/28 Python
意大利领先的奢侈品在线时装零售商:MCLABELS
2020/10/13 全球购物
青年文明号事迹材料
2014/01/18 职场文书
运动会广播稿500字
2014/01/28 职场文书
机电专业大学生职业规划书范文
2014/02/25 职场文书
股权投资意向书
2014/04/01 职场文书
大学生第一学年自我鉴定
2014/09/12 职场文书
党员群众路线剖析材料
2014/10/08 职场文书
大学生党员批评与自我批评范文
2014/10/14 职场文书
2014年班主任工作总结
2014/11/08 职场文书
2015年管理人员工作总结
2015/05/13 职场文书
抢劫罪辩护词
2015/05/21 职场文书
恋恋笔记本观后感
2015/06/16 职场文书
2015迎新晚会活动总结
2015/07/16 职场文书
2016小学优秀教师先进事迹材料
2016/02/26 职场文书
Python基于Opencv识别两张相似图片
2021/04/25 Python
Html5同时支持多端sdk的小技巧
2021/11/17 HTML / CSS