PHP中利用GD实现的柱状图
PHP中利用GD实现的柱状图,自己写的一个画柱状图的类,上代码。
<?php
Class Chart{
private $image; // 定义图像
private $title; // 定义标题
private $ydata; // 定义Y轴数据
private $xdata; // 定义X轴数据
private $color; // 定义条形图颜色
private $bgcolor; // 定义图片背景颜色
private $width; // 定义图片的宽
private $height; // 定义图片的长 /*
* 构造函数
* String title 图片标题
* Array xdata 索引数组,X轴数据
* Array ydata 索引数组,数字数组,Y轴数据
*/
function __construct($title,$xdata,$ydata) {
$this->title = $title;
$this->xdata = $xdata;
$this->ydata = $ydata;
$this->color = array('#058DC7', '#50B432', '#ED561B', '#DDDF00', '#24CBE5', '#64E572', '#FF9655', '#FFF263', '#6AF9C4');
} /*
* 公有方法,设置条形图的颜色
* Array color 颜色数组,元素取值为'#058DC7'这种形式
*/
function setBarColor($color){
$this->color = $color;
} /*
* 公有方法,画条形图
*/
function mkBarChart(){
$ydataNum = $this->arrayNum($this->ydata); // 取得数据分组的个数
$max = $this->arrayMax($this->ydata); // 取得所有呈现数据的最大值
$multi = ($max > 100)? $max/100 : 1; // 如果最大数据是大于100的则进行缩小处理,获取
$barHeightMulti = 2.2; // 条形高缩放的比例
$barWidth = (16 - 2*($ydataNum - 1)) > 10 ? (16 - 2*($ydataNum - 1)) : 10; // 条的宽
$barSpace = 16; // 条之间的间距
$chartLeft = (1+strlen($max))*12; // 设置图片左边的margin $barY = 250; // 初始化条形图的Y的坐标
// 设置图片的宽、高
$this->width = ($ydataNum*$barWidth + $barSpace)*count($this->xdata) + $chartLeft;
$this->height = 300;
$this->image = imagecreatetruecolor($this->width ,$this->height); // 准备画布
$this->bgcolor = imagecolorallocate($this->image,255,255,255); // 图片的背景颜色 // 设置条形图的颜色
$color = array();
foreach($this->color as $col) {
$col = substr($col,1,strlen($col)-1);
$red = hexdec(substr($col,0,2));
$green = hexdec(substr($col,2,2));
$blue = hexdec(substr($col,4,2));
$color[] = imagecolorallocate($this->image ,$red, $green, $blue);
} // 设置线段的颜色、字体的颜色、字体的路径
$lineColor = imagecolorallocate($this->image ,0xcc,0xcc,0xcc);
$fontColor = imagecolorallocate($this->image, 0x95,0x8f,0x8f);
$fontPath = 'font/simsun.ttc'; imagefill($this->image,0,0,$this->bgcolor); // 绘画背景 // 绘画图的分短线与左右边线
for($i = 0; $i < 6; $i++ ) {
imageline($this->image,$chartLeft-10,$barY-$barHeightMulti*$max/5/$multi*$i,$this->width,$barY-$barHeightMulti*$max/5/$multi*$i,$lineColor);
imagestring($this->image,4,5,$barY-$barHeightMulti*$max/5/$multi*$i-8,floor($max/5*$i),$fontColor);
}
imageline($this->image,$chartLeft-10,30,$chartLeft-10,$barY,$lineColor);
imageline($this->image,$this->width-1,30,$this->width-1,$barY,$lineColor); // 绘画图的条形
foreach($this->ydata as $key => $val) {
if($ydataNum == 1) {
// 一个系列数据时
$barX = $chartLeft + 3 + ($barWidth+$barSpace)*$key;
imagefilledrectangle($this->image,$barX,$barY-$barHeightMulti*$val/$multi,$barX+$barWidth,$barY,$color[$key%count($this->color)]);
}elseif($ydataNum > 1) {
// 多个系列的数据时
$cbarSpace = $barSpace + $barWidth*($ydataNum-1);
foreach($val as $ckey => $cval) {
$barX = $chartLeft + 3 + $barWidth*$key + $ckey*($cbarSpace+$barWidth);
imagefilledrectangle($this->image,$barX,$barY-$barHeightMulti*$cval/$multi,$barX+$barWidth,$barY,$color[$key%count($this->color)]);
}
} } // 绘画条形图的x坐标的值
foreach($this->xdata as $key => $val) {
$barX = $chartLeft + ($ydataNum*$barWidth+$barSpace)*$key + $ydataNum*$barWidth/3;
imagettftext($this->image,10,-45,$barX,$barY+15,$fontColor,$fontPath,$this->xdata[$key]);
} // 绘画标题
$titleStart = ($this->width - 5.5*strlen($this->title))/2;
imagettftext($this->image,11,0,$titleStart,20,$fontColor,$fontPath,$this->title); // 输出图片
header("Content-Type:image/png");
imagepng ( $this->image );
} /*
* 私有方法,当数组为二元数组时,统计数组的长度
* Array arr 要做统计的数组
*/
private function arrayNum($arr) {
$num = 0;
if(is_array($arr)) {
$num++;
for($i = 0; $i < count($arr); $i++){
if(is_array($arr[$i])) {
$num = count($arr);
break;
}
}
}
return $num;
} /*
* 私有方法,计算数组的深度
* Array arr 数组
*/
private function arrayDepth($arr) {
$num = 0;
if(is_array($arr)) {
$num++;
for($i = 0; $i < count($arr); $i++){
if(is_array($arr[$i])) {
$num += $this->arrayDepth($arr[$i]);
break;
}
}
}
return $num;
} /*
* 私有方法,找到一组中的最大值
* Array arr 数字数组
*/
private function arrayMax($arr) {
$depth = $this->arrayDepth($arr);
$max = 0;
if($depth == 1) {
rsort($arr);
$max = $arr[0];
}elseif($depth > 1) {
foreach($arr as $val) {
if(is_array($val)) {
if($this->arrayMax($val) > $max) {
$max = $this->arrayMax($val);
}
}else{
if($val > $max){
$max = $val;
}
}
}
}
return $max;
} function arrayAver($arr) {
$aver = array();
foreach($arr as $val) {
if(is_array($val)) {
$aver = array_merge($aver,$val);
}else{
$aver[] = $val;
}
}
return array_sum($aver)/count($aver); }
// 析构函数
function __destruct(){
imagedestroy($this->image);
}
}
?> 这个类可以实现画一个系列的柱状图和多个系列的柱状图,如下:
一个系列的柱状图
多个系列的柱状图
PHP中利用GD实现的柱状图的更多相关文章
- Hadoop 中利用 mapreduce 读写 mysql 数据
Hadoop 中利用 mapreduce 读写 mysql 数据 有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...
- [原创]MYSQL中利用外键实现级联删除和更新
MySQL中利用外键实现级联删除.更新 MySQL支持外键的存储引擎只有InnoDB,在创建外键的时候,要求父表必须有对应的索引,子表在创建外键的时候也会自动创建对应的索引.在创建索引的时候,可以指定 ...
- [.net 面向对象程序设计进阶] (21) 反射(Reflection)(下)设计模式中利用反射解耦
[.net 面向对象程序设计进阶] (21) 反射(Reflection)(下)设计模式中利用反射解耦 本节导读:上篇文章简单介绍了.NET面向对象中一个重要的技术反射的基本应用,它可以让我们动态的调 ...
- springMVC中利用model在JSTL进行回填值
1.ringMVC中利用model回填值 后台中,利用model返回值,如 model.addAttribute("MS_info" , MS_info); 前台回填值: text ...
- zk框架中利用map类型传值来创建window,并且传值
@Command @NotifyChange("accList") public void clear(@BindingParam("id") String a ...
- php学习笔记:利用gd库生成图片,并实现随机验证码
说明:一些基本的代码我都进行了注释,这里实现的验证码位数.需要用的字符串都可以再设置.有我的注释,大家应该很容易能看得懂. 基本思路: 1.用mt_rand()随机生成数字确定需要获取的字符串,对字符 ...
- ArcGIS中利用ArcMap将地理坐标系转换成投影坐标系(从WKID=4326到WKID=102100)
原文:ArcGIS中利用ArcMap将地理坐标系转换成投影坐标系(从WKID=4326到WKID=102100) 对于非地理专业的开发人员,对与这些生涩的概念,我们不一定都要了解,但是我们要理解,凡是 ...
- 【转】asp.net中利用session对象传递、共享数据[session用法]
来自:http://blog.unvs.cn/archives/session-transfer-method.html 下面介绍Asp.net中利用session对象传递.共享数据用法: 1.传递值 ...
- SpingMVC中利用BindingResult将错误信息返回到页面中
SpingMVC中利用BindingResult将错误信息返回到页面中. ActionFrom中: private String name; private String password; get( ...
随机推荐
- thinkphp里的session、cookie方法
thinkphp里,对于session和cookie的操作,不管是存值.获取.删除,均只有一个方法.现分享出来,供大家参考参考. /** * session管理函数 * @param string|a ...
- python学习 函数
# -*- config=utf-8 -*- #################################### 函数 ################################## de ...
- javascript_basic_01之概述
1.javascript组成: ①核心ECMAScript:②文档对象模型DOM(Document Object Model):③浏览器对象模型BOM(Browser Object Model): 2 ...
- Java基础-继承 利用接口做参数,写个计算器,能完成+-*/运算
38.利用接口做参数,写个计算器,能完成+-*/运算 (1)定义一个接口Compute含有一个方法int computer(int n,int m); (2)设计四个类分别实现此接口,完成+-*/运算 ...
- 12.创建一个Point类,有成员变量x,y,方法getX(),setX(),还有一个构造方 法初始化x和y。创建类主类A来测试它。
package java1; public class Point { int x; int y; Point(int x,int y) { this.x = x; this.y = y; } pub ...
- php的mysql\mysqli\PDO(一)mysql
原文链接:http://www.orlion.ga/1140/ 工作中数据库的操作都被封装好了,这些怎么用的都快忘了干脆写篇博客重新复习下,以后要是再忘记了可以看这篇文章. PHP 5.5.0 起已废 ...
- 使用office生成PDF文件
网络上有很多word转pdf的软件,功能效果不尽相同,对于想要把word转换成pdf格式的网友来说,一款实用强大的工具是必不可少的,踏破铁鞋无觅处,原来office2010就有符合你要求的功能.PDF ...
- 深入解读A/B 测试的统计学原理
了解一些统计学知识对正确地进行 A/B 测试和研判试验结果是很有帮助的,本篇文章深入介绍了A/B 测试的原理和背后的统计学依据.完全理解本文中提到的数学计算需要你掌握概率方面的一点基础知识. 统计学在 ...
- web接口开发与测试
最近一直在学习和整理web开发与接口测试的相关资料.接口测试本身毫无任何难度,甚至有很多工具和类库来帮助我们进行接口测试.大多测试人员很难深入了解web接口测试的原因是对web开发不太了解,当你越了解 ...
- 如何用Ajax加载服务器的图片
用Ajax请求服务器的图片,并显示在浏览器中 前言 一直在数据库里面存的都是图片在服务器的地址,然后再到浏览器中显示,但是发现两个问题 第一:为了安全起见,js是无法读取本地的图片的,不然你写一个js ...