当前位置: 首页 > news >正文

php写统计投票数,php画投票统计图 - huolong的个人空间 - OSCHINA - 中文开源技术交流社区...

//Vote.class.php

/***********************************

*投票结果条形统计图

*

*@作者:王永强

*@时间 :2015年05月11日

*@邮箱:1442022614@qq.com

*

*注意:需要将$this->strTTF改成指定的字体文件路径地址(后缀名为.ttf)

**********************************/

class Vote{

//投票的对象

private $arrCandidate;

//票数和

private $nTotal;

//统计图标题

private $strTitle = 'the result of this voting';

//图像宽度

private $nWidth = 500;

//图像高度

private $nHeight;

//条形图宽度

private $nBarWidth;

//条形图高度

private $nBarHeight = 40;

//标题高度

private $nTitleHeight = 50;

//条形图间距离

private $nBarSpacing;

//标题大小

private $nTitleSize = 16;

//正文大小

private $nMainSize = 12;

//投票对象名称所占宽度

private $nNameWidth = 80;

//左右padding大小

private $nPadding = 10;

//条形图开始坐标

private $nX;

private $nY;

//部件间距

private $nIndent = 10;

//1%100的条形图长度

private $nBarUnit;

//百分比宽度

private $nPercentWidth = 50;

//票数宽度

private $nNumberWidth = 50;

//图像资源

private $srcImage;

//图像背景色

private $nBgColor;

//图像背景色rgb值

private $arrBgColor = array(255, 255, 255);

//文本色

private $nTextColor;

//文本色rgb值

private $arrTextColor = array(0, 0, 0);

//百分比颜色

private $nPercentColor;

//百分比颜色rgb值

private $arrPercentColor = array(0, 0, 0);

//直线颜色

private $nLineColor;

//直线颜色rgb值

private $arrLineColor = array(0, 0, 0);

//条形图颜色

private $nBarColor;

//条形图颜色rgb值

private $arrBarColor = array(0, 64, 128);

//票数颜色

private $nNumberColor;

//票数颜色rgb值

private $arrNumberColor = array(255, 78, 243);

//标题颜色

private $nTitleColor;

//标题颜色rgb值

private $arrTitleColor = array(255, 255, 255);

//名称颜色

private $nNameColor;

//名称颜色rgb值

private $arrNameColor = array(0, 0, 0);

//TrueTypeFont

private $strTTF = '/usr/share/fonts/truetype/fonts-japanese-gothic.ttf';

//输出图像类型

private $strImageType = 'png';

/**

*构造函数,初始化主要参数

*/

public function __construct($arrCandidate, $strTitle=null){

if (!isset($arrCandidate) || empty($arrCandidate)) {

die('必须传入投票的对象');

}

$this->arrCandidate = $arrCandidate;

if (isset($strTitle)) {

$this->strTitle;

}

//初始化参数

$this->init();

}

/**

*析构函数,销毁图像

*/

public function __destruct(){

if (!empty($this->srcImage)) {

imagedestroy($this->srcImage);

}

}

/**

*输出条形统计图

*

*@param $strFileUrl 将图像保存到该目录路径下,没有后缀名

*/

public function show($strFileUrl=null){

//更新成员变量

$this->init();

//画轮廓

imagefilledrectangle($this->srcImage, 0, 0, $this->nWidth, $this->nHeight, $this->nBgColor);

imagerectangle($this->srcImage, 0, 0, $this->nWidth-1, $this->nHeight-1, $this->nLineColor);

//画标题部分

$this->paintTitle();

//画名称部分

$this->paintName();

//画条形图部分

$this->paintBar();

//画百分比部分

$this->paintPercent();

//输出图像或保存图像

$imageFunc = 'image' . $this->strImageType;

if (isset($strFileUrl)) {

$imageFunc($this->srcImage, $strFileUrl . '.' . $this->strImageType);

echo 'you have paint the image to the path :', $strFileUrl, '.', $this->strImageType;

return ;

}

header('Content-type: image/' . $this->strImageType);

$imageFunc($this->srcImage);

}

/**

*设置成员变量时自动调用

*

*@param $strName 成员变量

*@param $mixArgs 参数

*/

public function __set($strName, $mixArgs){

$this->$strName = $mixArgs;

}

/**

*初始化参数(这些参数用户无法改变),,或者当设置了变量时,更新变量

*/

private function init(){

//宽度高度

$this->nBarSpacing = $this->nBarHeight / 2;

$this->nHeight = $this->nTitleHeight + count($this->arrCandidate) * ($this->nBarHeight + $this->nBarSpacing);

$this->nX = $this->nPadding + $this->nNameWidth + $this->nIndent;

$this->nY = $this->nTitleHeight;

$this->nBarWidth = $this->nWidth - $this->nX - $this->nPadding - $this->nPercentWidth;

$this->nBarUnit = $this->nBarWidth / 100;

//票数和

$this->nTotal = 0;

foreach ($this->arrCandidate as $value) {

$this->nTotal += $value['count'];

}

//实例化图像句柄

$this->srcImage = imagecreatetruecolor($this->nWidth, $this->nHeight);

//颜色

$this->nBgColor = imagecolorallocate($this->srcImage, $this->arrBgColor[0], $this->arrBgColor[1], $this->arrBgColor[2]);

$this->nTextColor = imagecolorallocate($this->srcImage, $this->arrTextColor[0], $this->arrTextColor[1], $this->arrTextColor[2]);

$this->nPercentColor = imagecolorallocate($this->srcImage, $this->arrPercentColor[0], $this->arrPercentColor[1], $this->arrPercentColor[2]);

$this->nLineColor =imagecolorallocate($this->srcImage, $this->nLineColor[0], $this->nLineColor[1], $this->nLineColor[2]);

$this->nBarColor = imagecolorallocate($this->srcImage, $this->arrBarColor[0], $this->arrBarColor[1], $this->arrBarColor[2]);

$this->nNumberColor = imagecolorallocate($this->srcImage, $this->arrNumberColor[0], $this->arrNumberColor[1], $this->arrNumberColor[2]);

$this->nNameColor = imagecolorallocate($this->srcImage, $this->arrNameColor[0], $this->arrNameColor[1], $this->arrNameColor[2]);

}

/**

*画标题部分

*/

private function paintTitle(){

$textBox = imagettfbbox($this->nTitleSize, 0, $this->strTTF, $this->strTitle);

$textWidth = $textBox[2] - $textBox[0];

$textHeight = abs($textBox[7] - $textBox[1]);

$textAboveLine = abs($textBox[7]);

$x = ($this->nWidth - $textWidth) / 2;

$y = ($this->nY - $textHeight) / 2 + $textAboveLine;

imagettftext($this->srcImage, $this->nTitleSize, 0, $x, $y, $this->nTitleColor, $this->strTTF, $this->strTitle);

}

/**

*画名称部分

*/

private function paintName(){

//循环画出所有名称

$y = $this->nY;

$x = $this->nPadding;

foreach ($this->arrCandidate as $value) {

$y +=  $this->nBarHeight / 2;

imagettftext($this->srcImage, $this->nMainSize, 0, $x, $y, $this->nNameColor, $this->strTTF, $value['name']);

$y += $this->nBarHeight / 2 + $this->nBarSpacing;

}

}

/**

*画条形图部分

*/

private function paintBar(){

$tempY = $this->nY;

foreach ($this->arrCandidate as $value) {

//着色部分

$x1 = $this->nX;

$y1 = $tempY;

$x2 = $this->nX + $this->nBarUnit * $value['count'] * 100 / $this->nTotal;

$y2 = $tempY + $this->nBarHeight;

imagefilledrectangle($this->srcImage, $x1, $y1, $x2, $y2, $this->nBarColor);

//条形图轮廓

$x2 = $this->nX + $this->nBarWidth;

$y2 = $tempY + $this->nBarHeight;

imagerectangle($this->srcImage, $x1, $y1, $x2, $y2, $this->nLineColor);

//票数部分

$x = $this->nX + $this->nBarWidth - $this->nNumberWidth;

$y = $tempY + $this->nBarHeight / 2;

imagettftext($this->srcImage, $this->nMainSize, 0, $x, $y, $this->nNumberColor, $this->strTTF, $value['count'] . '/' . $this->nTotal);

$tempY += $this->nBarHeight + $this->nBarSpacing;

}

}

/**

*画百分比部分

*/

private function paintPercent(){

$x = $this->nX + $this->nBarWidth + $this->nIndent;

$y = $this->nY;

foreach ($this->arrCandidate as $value) {

$percent = intval(100 * $value['count'] / $this->nTotal);

$y += $this->nBarHeight / 2;

imagettftext($this->srcImage,  $this->nMainSize, 0, $x, $y, $this->nPercentColor, $this->strTTF, $percent . '%');

$y += $this->nBarHeight / 2 + $this->nBarSpacing;

}

}

}

?>

///test.php

require('Vote.class.php');

$data = array(

array(

'name' => 'zhansan',

'count' => 14

),

array(

'name' => 'lisi',

'count' => 16

),

array(

'name' => 'wangwu',

'count' => 20

)

);

$vote = new Vote($data);

$vote->strTitle = 'the result of this voting';

$vote->arrBgColor = array(33, 33, 33);

//输出到指定文件

//$vote->show('./gd');

//直接输出到浏览器

$vote->show();

?>

相关文章:

  • php会话类,php简单的会话类代码
  • php设置超时等待时间,php设置超时时间的方法
  • oracle零基础可以学么,零基础学Oracle
  • oracle数据库异机备份脚本,服务器A制定计划任务,BAT脚本自动备份oracle数据文件,拷贝至服务器B的共享目录。...
  • 百度sms php,php 调用百度sms来发送短信的实现示例
  • oracle索引的storage,Oracle全文索引的STORAGE属性有什么特点?
  • php 字符串向前截取,php字符串截取函数
  • oracle 查询本年12月,ORACLE 写一条Sql语句查出年份,1月,2月,3月....12月的订单总数列表...
  • sqlserver新建oracle发布是咋回事,SQLServer创建链接服务器对象链接oracle
  • linux桌面环境占用内存对比,最新精简型Linux桌面环境大比拼:LXDE Vs Xfce Vs MATE
  • dnw linux 内核,神器DNW2 FOR LINUX!
  • linux 单独装ftp服务,linux下安装FTP服务
  • linux日志服务重开,linux重新編譯內核
  • linux arm中断进程,ARM Linux对中断的处理
  • linux jvm自动退出,在Linux中启动Eclipse时出错:“ JVM已终止。退出代码= 13”
  • 【翻译】babel对TC39装饰器草案的实现
  • happypack两次报错的问题
  • iOS | NSProxy
  • js ES6 求数组的交集,并集,还有差集
  • js学习笔记
  • laravel with 查询列表限制条数
  • MySQL数据库运维之数据恢复
  • Node.js 新计划:使用 V8 snapshot 将启动速度提升 8 倍
  • 大整数乘法-表格法
  • 前端临床手札——文件上传
  • 融云开发漫谈:你是否了解Go语言并发编程的第一要义?
  • 温故知新之javascript面向对象
  • 一、python与pycharm的安装
  • 一份游戏开发学习路线
  • 一起来学SpringBoot | 第三篇:SpringBoot日志配置
  • 用element的upload组件实现多图片上传和压缩
  • C# - 为值类型重定义相等性
  • ​ 全球云科技基础设施:亚马逊云科技的海外服务器网络如何演进
  • #Linux(帮助手册)
  • #常见电池型号介绍 常见电池尺寸是多少【详解】
  • (Matlab)遗传算法优化的BP神经网络实现回归预测
  • (分布式缓存)Redis持久化
  • (十六)一篇文章学会Java的常用API
  • (心得)获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列。
  • (转)http-server应用
  • (最优化理论与方法)第二章最优化所需基础知识-第三节:重要凸集举例
  • .dat文件写入byte类型数组_用Python从Abaqus导出txt、dat数据
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .NET Framework 4.6.2改进了WPF和安全性
  • .NET 中小心嵌套等待的 Task,它可能会耗尽你线程池的现有资源,出现类似死锁的情况
  • .NET中 MVC 工厂模式浅析
  • @Not - Empty-Null-Blank
  • [AutoSar]BSW_Memory_Stack_004 创建一个简单NV block并调试
  • [BZOJ1053][HAOI2007]反素数ant
  • [BZOJ1060][ZJOI2007]时态同步 树形dp
  • [C# 开发技巧]实现属于自己的截图工具
  • [CF]Codeforces Round #551 (Div. 2)
  • [CSS]CSS 字体属性
  • [hdu 3746] Cyclic Nacklace [kmp]
  • [iHooya]2023年1月30日作业解析