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( ...
随机推荐
- javascript日常总结
如何去除掉inline-block元素之间的默认间距 前几天写一个页面 1 2 3 4 div{width:900px;} div li{ display:inline-block; width:30 ...
- Yii 框架学习--02 进阶
应用结构 入口文件 文件位置: web/index.php <?php //开启debug,应用会保留更多日志信息,如果抛出异常,会显示详细的错误调用堆栈 defined('YII_DEBUG' ...
- NodeMCU初探
对于ESP8266模块,早就想知道如何用其脚本语言, 自己先用的这个模块测试的 首先是先下载需要用到的工具和固件 链接:http://pan.baidu.com/s/1dF5NZ3N 密码:bziq ...
- Iframe去掉滚动条
<html><head><title></title></head><body STYLE='OVERFLOW:SCROLL;OVER ...
- ES6 对象解构
ES6 对象解构 第一眼看到,什么鬼? const { body } = document `` 其实等于: const body = document.body ``` http://es6.rua ...
- javascript中function 函数递归的陷阱问题
//看下这个递归方法,最后输出的值function fn(i){ i++; if(i<10){ fn(i); } else{ return i; } } var result = fn(0); ...
- mysqll底层分享(一):MySQL索引背后的数据结构及算法原理
http://www.uml.org.cn/sjjm/201107145.asp#nav-2 http://tech.it168.com/a2011/0711/1216/000001216087_al ...
- HTML5移动Web开发(八)——避免文本字体大小重置
适用设备:iOS.Windows Mobile在一些移动设备上,比方说iPhone,Windows Mobile,当用户把手机切换到横屏时,浏览器会自动地重置文本字体大小.这可能会对我们造成困扰,因为 ...
- Android中的内存储、外存储概念、文件操作与PC端的有些不同
其实安卓文件的操作和java在pc环境下的操作并无二致,之所以需要单独讲解是因为安卓系统提供了不同于pc的访问文件系统根路径的api,同时对一个应用的私有文件做了统一的管理.初学者在这部分感到很容易混 ...
- Android进程间通信之LocalSocket通信
LocalSocket,在Unix域名空间创建的一个套接字(非服务端). 是对Linux中Socket进行了封装,采用JNI方式调用,实现进程间通信. 具体就是Native层Server和Framew ...