<?php

  1. //最后一次修改:2004-6-21
  2. //一个生成矩形图,曲线图的图形分析类
  3. //作者:tonera
  4. //说明:
  5. //任何人可在任何场合自由使用这个类。但由此所发生的损害跟作者无关。
  6. //可根据数据自适应X和Y轴大小。
  7. //在同一个图形中可显示多个曲线图
  8. //用户可给出生成的图的尺寸大小,数据参数。类根据数据的值来判断生成的图形的高(默认10格)和宽各分几格。
  9. //若用户没有给出图的尺寸大小,则图形高和宽为255像素
  10. //数据参数通过一个方法add_data($array)来多次添加,每次一个数组。
  11. //可自设定图形边框,矩形图内线,深色边框线,浅色边框线,曲线,点的颜色。若用户没有指定,则为默认值
  12. //set_colors方法设定不同曲线的不同色彩
  13. //可进行图形的叠加显示:点,线,矩形
  14. //注意:需要GD库支持
  15. /*
  16. //使用示例
  17. $gg=new build_graph();
  18. $d1=array(0,62,25,20,20,100,99);
  19. $d2=array(0,80,75,65,100,56,79);
  20. $d3=array(0,60,50,25,12,56,45);
  21. $gg->add_data($d1);
  22. $gg->add_data($d2);
  23. $gg->add_data($d3);
  24. $gg->set_colors("ee00ff,dd8800,00ff00");
  25. //生成曲线图
  26. $gg->build("line",0); //参数0表示显示所有曲线,1为显示第一条,依次类推
  27. //生成矩形图
  28. //$gg->build("rectangle","2"); //参数0表示显示第一个矩形,1也为显示第一条,其余依次类推
  29. */
  30.  
  31. class build_graph {
  32. var $graphwidth = 300;
  33. var $graphheight = 300;
  34. var $width_num = 0; //宽分多少等分
  35. var $height_num = 10; //高分多少等分,默认为10
  36. var $height_var = 0; //高度增量(用户数据平均数)
  37. var $width_var=0; //宽度增量(用户数据平均数)
  38. var $height_max=0; //最大数据值
  39. var $array_data=array(); //用户待分析的数据的二维数组
  40. var $array_error=array(); //收集错误信息
  41.  
  42. var $colorBg=array(255,255,255); //图形背景-白色
  43. var $colorGrey=array(192,192,192); //灰色画框
  44. var $colorBlue=array(0,0,255); //蓝色
  45. var $colorRed=array(255,0,0); //红色(点)
  46. var $colorDarkBlue=array(0,0,255); //深色
  47. var $colorLightBlue=array(200,200,255); //浅色
  48. var $colorLightBlack = array(138,138,120); //浅黑
  49. var $colorBlack = array(30,30,30); //黑
  50. var $rgbArray = array("ee00ff","dd8800","00ff00","104E8B");
  51.  
  52. var $array_color; //曲线着色(存储十六进制数)
  53. var $image; //我们的图像
  54. var $lineType = ''; //画底线时标的数字值,则x的值,范围为'day', 'hour'两值
  55. var $beginDay = '';
  56.  
  57. //方法:接受用户数据
  58. function add_data($array_user_data, $dataKey = 0){
  59. if(!is_array($array_user_data)){
  60. $array_user_data = array();
  61. }
  62. if(!is_array($array_user_data) or empty($array_user_data)){
  63. $this->array_error['add_data']="没有可供分析的数据";
  64. return false;
  65. exit();
  66. }
  67. if($dataKey == 0 || array_key_exists($dataKey, $this->array_data)){
  68. $dataKey = count($this->array_data);
  69. }
  70. $this->array_data[$dataKey] = $array_user_data;
  71. }
  72.  
  73. //方法:定义画布宽和长
  74. function set_img($img_width,$img_height){
  75. $this->graphwidth = $img_width;
  76. $this->graphheight = $img_height;
  77. }
  78.  
  79. //设定Y轴的增量等分,默认为10份
  80. function set_height_num($var_y){
  81. $this->height_num=$var_y;
  82. }
  83.  
  84. //定义各图形各部分色彩
  85. function get_RGB($color){ //得到十进制色彩
  86. $R=($color>>16) & 0xff;
  87. $G=($color>>8) & 0xff;
  88. $B=($color) & 0xff;
  89. return (array($R,$G,$B));
  90. }
  91. //---------------------------------------------------------------
  92. #定义背景色
  93. function set_color_bg($c1,$c2,$c3){
  94. $this->colorBg=array($c1,$c2,$c3);
  95. }
  96. #定义画框色
  97. function set_color_Grey($c1,$c2,$c3){
  98. $this->colorGrey=array($c1,$c2,$c3);
  99. }
  100. #定义蓝色
  101. function set_color_Blue($c1,$c2,$c3){
  102. $this->colorBlue=array($c1,$c2,$c3);
  103. }
  104. #定义色Red
  105. function set_color_Red($c1,$c2,$c3){
  106. $this->colorRed=array($c1,$c2,$c3);
  107. }
  108. #定义深色
  109. function set_color_DarkBlue($c1,$c2,$c3){
  110. $this->colorDarkBlue=array($c1,$c2,$c3);
  111. }
  112. #定义浅色
  113. function set_color_LightBlue($c1,$c2,$c3){
  114. $this->colorLightBlue=array($c1,$c2,$c3);
  115. }
  116.  
  117. //设定画底线时标的数字值,则x的值,范围为'day', 'hour'两值
  118. function setLineStrinType($type){
  119. if(!in_array($type, array('day', 'hour'))){
  120. return false;
  121. }
  122. $this->lineType = $type;
  123. }
  124.  
  125. //---------------------------------------------------------------
  126.  
  127. //方法:由用户数据将画布分成若干等份宽
  128. //并计算出每份多少像素
  129. function get_width_num(){
  130. $dataCount = array();
  131. foreach ($this->array_data as $key => $oneData){
  132. $dataCount[] = count($oneData);
  133. }
  134.  
  135. $this->width_num = max($dataCount);
  136. }
  137. function get_max_height(){
  138. //获得用户数据的最大值
  139. $tmpvar = array();
  140. foreach($this->array_data as $tmp_value){
  141. $tmpvar[]= max($tmp_value);
  142. }
  143. if(isset($tmpvar[0])){
  144. $more = $tmpvar[0];
  145. }
  146. $this->height_max = max($tmpvar) + $this->height_var;
  147. return max($tmpvar);
  148. }
  149. function get_height_length(){
  150. //计算出每格的增量长度(用户数据,而不是图形的像素值)
  151. $max_var = $this->get_max_height();
  152. $max_var = round($max_var/$this->height_num);
  153. $first_num = substr($max_var,0,1);
  154. if(substr($max_var,1,1)){
  155. if(substr($max_var,1,1)>=5)
  156. $first_num += 1;
  157. }
  158. for($i = 1; $i < strlen($max_var); $i++){
  159. $first_num .= "0";
  160. }
  161. return (int)$first_num;
  162. }
  163. function get_var_wh(){ //得到高和宽的增量
  164. $this->get_width_num();
  165. //得到高度增量和宽度增量
  166. $this->height_var = $this->get_height_length();
  167. $this->width_var = round($this->graphwidth/$this->width_num, 3);
  168. }
  169.  
  170. function set_colors($str_colors){
  171. //用于多条曲线的不同着色,如$str_colors="ee00ff,dd0000,cccccc"
  172. $this->array_color = split(",",$str_colors);
  173. }
  174.  
  175. function set_begin_day($beginDay){
  176. if(!is_numeric($beginDay)){
  177. return false;
  178. }
  179. $this->beginDay = $beginDay;
  180. }
  181.  
  182. ######################################################################################################
  183. function build_line($var_num){
  184. if(!empty($var_num)){ //如果用户只选择显示一条曲线
  185. $array_tmp[0] = $this->array_data[$var_num-1];
  186. $this->array_data = $array_tmp;
  187. }
  188.  
  189. for($j=0;$j<count($this->array_data);$j++){
  190. list($R,$G,$B)=$this->get_RGB(hexdec($this->array_color[$j]));
  191. $colorBlue=imagecolorallocate($this->image,$R,$G,$B);
  192.  
  193. for($i = 0;$i < $this->width_num - 1;$i++){
  194. $height_pix = round(($this->array_data[$j][$i]/$this->height_max)*$this->graphheight);
  195. $height_next_pix = round($this->array_data[$j][$i + 1]/$this->height_max*$this->graphheight);
  196. imageline($this->image,$this->width_var*($i + 1),$this->graphheight-$height_pix,$this->width_var*($i + 2),$this->graphheight-$height_next_pix,$colorBlue);
  197. }
  198. }
  199. //画点
  200. $colorRed = imagecolorallocate($this->image, $this->colorBlue[0], $this->colorBlue[1], $this->colorBlue[2]);
  201.  
  202. for($j = 0; $j < count($this->array_data); $j++){
  203. list($R,$G,$B) = $this->get_RGB(hexdec($this->array_color[$j]));
  204. $colorPoint = imagecolorallocate($this->image,$R,$G,$B);
  205.  
  206. for($i = 0; $i < $this->width_num; $i++){
  207. if(isset($this->array_data[$j][$i])){
  208. $height_pix = round(($this->array_data[$j][$i]/$this->height_max)*$this->graphheight);
  209. $arcRet = imagearc($this->image,$this->width_var*($i + 1),$this->graphheight-$height_pix,6,5,0,360,$colorPoint);
  210. $borderRet = imagefilltoborder($this->image,$this->width_var*($i + 1),$this->graphheight-$height_pix,$colorPoint,$colorPoint);
  211. if(!$arcRet || !$borderRet){
  212. break;
  213. }
  214. }
  215. }
  216. }
  217.  
  218. for($j = 0; $j < count($this->array_data); $j++){
  219. list($R,$G,$B) = $this->get_RGB(hexdec($this->array_color[$j]));
  220. $colorPoint = imagecolorallocate($this->image,$R,$G,$B);
  221. for($i = 0; $i < $this->width_num; $i++){
  222. $height_pix = round(($this->array_data[$j][$i]/$this->height_max) * $this->graphheight);
  223. $arcRet = imagearc($this->image,$this->width_var*($i + 1),$this->graphheight-$height_pix,6,5,0,360,$colorPoint);
  224. $arcRet = imagefilltoborder($this->image,$this->width_var*($i + 1),$this->graphheight-$height_pix,$colorPoint,$colorPoint);
  225. if(!$arcRet || !$borderRet){
  226. break;
  227. }
  228. }
  229. }
  230.  
  231. }
  232.  
  233. ######################################################################################################
  234. function build_rectangle($select_gra){
  235. if(!empty($select_gra)){ //用户选择显示一个矩形
  236. $select_gra-=1;
  237. }
  238. //画矩形
  239. //配色
  240. $colorDarkBlue=imagecolorallocate($this->image, $this->colorDarkBlue[0], $this->colorDarkBlue[1], $this->colorDarkBlue[2]);
  241. $colorLightBlue=imagecolorallocate($this->image, $this->colorLightBlue[0], $this->colorLightBlue[1], $this->colorLightBlue[2]);
  242.  
  243. if(empty($select_gra))
  244. $select_gra=0;
  245. for($i=0; $i<$this->width_num; $i++){
  246. $height_pix=round(($this->array_data[$select_gra][$i]/$this->height_max)*$this->graphheight);
  247. imagefilledrectangle($this->image,$this->width_var*$i,$this->graphheight-$height_pix,$this->width_var*($i+1),$this->graphheight, $colorDarkBlue);
  248. imagefilledrectangle($this->image,($i*$this->width_var)+1,($this->graphheight-$height_pix)+1,$this->width_var*($i+1)-5,$this->graphheight-2, $colorLightBlue);
  249. }
  250. }
  251.  
  252. ######################################################################################################
  253. function create_cloths(){
  254. //创建画布
  255. $this->image = imagecreate($this->graphwidth+20,$this->graphheight+20);
  256. }
  257. function create_frame(){
  258. //创建画框
  259. $this->get_var_wh();
  260. //配色
  261. $colorBg=imagecolorallocate($this->image, $this->colorBg[0], $this->colorBg[1], $this->colorBg[2]);
  262. $colorGrey=imagecolorallocate($this->image, $this->colorGrey[0], $this->colorGrey[1], $this->colorGrey[2]);
  263. //创建图像周围的框
  264. imageline($this->image, 0, 0, 0, $this->graphheight,$colorGrey);
  265. imageline($this->image, 0, 0, $this->graphwidth, 0,$colorGrey);
  266. //imageline($this->image, ($this->graphwidth-1),0,($this->graphwidth-1),($this->graphheight-1),$colorGrey);
  267. imageline($this->image, 0,($this->graphheight-1),($this->graphwidth-1),($this->graphheight-1),$colorGrey);
  268. }
  269. function create_line(){
  270. //创建网格。
  271. $this->get_var_wh();
  272. $colorBg = imagecolorallocate($this->image, $this->colorBg[0], $this->colorBg[1], $this->colorBg[2]);
  273. $colorGrey = imagecolorallocate($this->image, $this->colorGrey[0], $this->colorGrey[1], $this->colorGrey[2]);
  274. $colorRed = imagecolorallocate($this->image, $this->colorRed[0], $this->colorRed[1], $this->colorRed[2]);
  275. $colorLightBlack = imagecolorallocate($this->image, $this->colorLightBlack[0], $this->colorLightBlack[1], $this->colorLightBlack[2]);
  276. $colorBlack = imagecolorallocate($this->image, $this->colorBlack[0], $this->colorBlack[1], $this->colorBlack[2]);
  277. for($i = 1 ;$i <= $this->height_num + 5; $i++){
  278. //画横线
  279. imageline($this->image,0,$this->graphheight-($this->height_var/$this->height_max*$this->graphheight)*$i,$this->graphwidth,$this->graphheight-($this->height_var/$this->height_max*$this->graphheight)*$i,$colorGrey);
  280. //标出数字
  281. imagestring($this->image,2,0,$this->graphheight-($this->height_var/$this->height_max*$this->graphheight)*$i,$this->height_var*$i,$colorLightBlack);
  282. }
  283. $type = $this->lineType;
  284. if($type == "day"){
  285. if($this->beginDay){
  286. $j = $this->beginDay;
  287. }else{
  288. $j = 1;
  289. }
  290. }elseif($type == "hour"){
  291. $j = 0;
  292. }
  293. for($i = 1;$i <= $this->width_num; $i++){
  294. //画竖线
  295. imageline($this->image,$this->width_var*$i,0,$this->width_var*$i,$this->graphwidth,$colorGrey);
  296. //标出数字
  297. if($type == 'day'){
  298. if($i == 1){
  299. imagestring($this->image,2,$this->width_var*$i,$this->graphheight-15,$j,$colorBlack);
  300. }
  301. imagestring($this->image,2,$this->width_var*($i + 1),$this->graphheight-15,$j + 1,$colorBlack);
  302. }elseif($type == 'hour'){
  303. if($i == 1){
  304. imagestring($this->image,2,$this->width_var*$i,$this->graphheight-15,$j,$colorBlack);
  305. }
  306. imagestring($this->image,2,$this->width_var*($i + 1),$this->graphheight-15,$j + 1,$colorBlack);
  307. }else{
  308. imagestring($this->image,2,$this->width_var*($i + 1),$this->graphheight-15,$i,$colorBlack);
  309. }
  310. $j++;
  311. if($type == "day" && $j == 31){
  312. $j = 1;
  313. }elseif($type == "hour" && $j == 23){
  314. $j = -1;
  315. }
  316. }
  317. }
  318.  
  319. function build($graph,$str_var, $outpwd = ""){
  320. //$graph是用户指定的图形种类,$str_var是生成哪个数据的图
  321. if(empty($outpwd)){
  322. header("Content-type: image/jpeg");
  323. }
  324. $this->create_cloths(); //先要有画布啊~~
  325. switch ($graph){
  326. case "line":
  327. $this->create_frame(); //画个框先:)
  328. $this->create_line(); //打上底格线
  329. $this->build_line($str_var); //画曲线
  330. break;
  331. case "rectangle":
  332. $this->create_frame(); //画个框先:)
  333. $this->build_rectangle($str_var); //画矩形
  334. $this->create_line(); //打上底格线
  335. break;
  336. }
  337. //输出图形并清除内存
  338. if(!empty($outpwd)){
  339. imagepng($this->image, $outpwd);
  340. }else{
  341. imagepng($this->image);
  342. }
  343. imagedestroy($this->image);
  344. }
  345.  
  346. ######################################################################################################
  347.  
  348. }

使用PHP画统计图的方法的更多相关文章

  1. javascript画直线和画圆的方法(非HTML5的方法)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 纯JS画点、画线、画圆的方法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. iOS 画平滑曲线的方法及取音频数据的方法

    源码:http://files.cnblogs.com/ios8/iOS%E5%BF%83%E7%94%B5%E5%9B%BEDemo.zip 取音频数据和画波形图的方法 ViewController ...

  4. SDL系列之 - 用画直线的方法来画正弦曲线

    线段长度无限短后就成为点,所以,现在让我们用画直线的方法来画正弦曲线吧 #include <SDL.h> #include <stdlib.h> #include <st ...

  5. Canvas画椭圆的方法

    虽然标题是画椭圆,但是我们先来说说Canvas中的圆 相信大家对于Canvas画圆都不陌生   oGC.arc(400, 300, 100, 0, 2*Math.PI, false); 如上所示,直接 ...

  6. android布局中画线的方法

    1.自定义View画线 http://fariytale.iteye.com/blog/1264225 下面介绍几种简单的方法 2.textView和View画直线 <TextView andr ...

  7. Java中的的画正三角方法

    在循环的语句的练习中,画正三角是一个很经典的例子,但是如果方法找的不对的话,即使最终画出来了,那么得到的代码也是非常的复杂,应用性不高. 下面有两种方法来画正三角,第一种是一种比较麻烦的办法,是通过归 ...

  8. win32之取画刷的方法

    取画刷(HBRUSH) 的六种方法2009-- :00HBRUSH hbr; 第一种: hbr= CreateSolidBrush(RGB(,,)); //单色的画刷 第二种: hbr= (HBRUS ...

  9. 几种画直线的方法-孙鑫C++笔记

    // HDC画直线 CPoint m_ptOrigin ; void CDrawView::OnLButtonDown(UINT nFlags, CPoint point) { m_ptOrigin ...

随机推荐

  1. 排序算法总结(C++版)

    总结下学过的排序算法,方便以后用到. 1.插入排序——将一个记录插入到已排序好的有序表中,从而得到一个新,记录数增1的有序表. void insertSort(int a[],int len) { ; ...

  2. sphinx实时索引和高亮显示

    sphinx实时索引和高亮显示 时间 2014-06-25 14:50:58  linux技术分享 -欧阳博客 原文  http://www.wantlearn.net/825 主题 Sphinx数据 ...

  3. 从json_encode过来的的字符串被返回到html页面时的解析

    在工作过程中经常需要向服务器请求数据.在需要返回多个值的时候,使用json_encode处理数组然后返回是很常用的做法.如果没有指定返回数据类型的情况下,默认返回的是json格式的字符串.那么需要将这 ...

  4. Just a Hook(区间set)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. Problem F: 分数类的类型转换

    Description 封装一个分数类Fract,用来处理分数功能和运算,支持以下操作:   1. 构造:传入两个参数n和m,表示n/m:分数在构造时立即转化成最简分数. 2. show()函数:分数 ...

  6. SElinux用户管理操作

    查看当前用户上下文 id -Z 查看登陆的用户和其对应的SELinux用户 semanage login -l 改变用户和SELinux的对应关系 semanage login -a选项能改变,-s用 ...

  7. PHP--最常用--必背函数总结!php学习者收藏必备!

    一.PHP系统函数 函数 功能 用法 var_dump() 打印变量结构信息,包括类型和值.数组将递归展开值 var_dump ( $arg1...); echo():是语法结构 输出一个或者多个字符 ...

  8. java中自动装箱带来的性能问题

    之前没有特别注意自动封装所带来的性能问题,今天看了effective java,实验了一下,结果大吃一惊: 考虑下面这段代码: public static void main(String args[ ...

  9. 40个Java多线程问题

    1.多线程有什么用? 一个可能在很多人看来很扯淡的一个问题:我会用多线程就好了,还管它有什么用?在我看来,这个回答更扯淡.所谓”知其然知其所以然”,”会用”只是”知其然”,”为什么用”才是”知其所以然 ...

  10. Vue浅谈

    谈Vue 最近在学习Vue相关的知识点并且也做一些练手,就在学习过程中出现的各种坑爹的地方做一个总结!之后再遇到也不会抓瞎. 1.Vue工程的安装 (1)首先先安装node.js这是Vue的运行基础. ...