免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3966 | 回复: 17
打印 上一主题 下一主题

用PHP进行图形分析[20040621] [复制链接]

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-06-21 00:02 |只看该作者 |倒序浏览
贴出来大家批评指正。
觉得有用的拿去用;
觉得垃圾的提意见。



  1. <?php
  2. //最后一次修改:2004-6-21
  3. //一个生成矩形图,曲线图的图形分析类
  4. //作者:tonera
  5. //说明:
  6. //任何人可在任何场合自由使用这个类。但由此所发生的损害跟作者无关。
  7. //可根据数据自适应X和Y轴大小。
  8. //在同一个图形中可显示多个曲线图
  9. //用户可给出生成的图的尺寸大小,数据参数。类根据数据的值来判断生成的图形的高(默认10格)和宽各分几格。
  10. //若用户没有给出图的尺寸大小,则图形高和宽为255像素
  11. //数据参数通过一个方法add_data($array)来多次添加,每次一个数组。
  12. //可自设定图形边框,矩形图内线,深色边框线,浅色边框线,曲线,点的颜色。若用户没有指定,则为默认值
  13. //set_colors方法设定不同曲线的不同色彩
  14. //可进行图形的叠加显示:点,线,矩形
  15. //注意:需要GD库支持

  16. class build_graph {
  17.         var $graphwidth=255;
  18.         var $graphheight=255;
  19.         var $width_num=0;                                        //宽分多少等分
  20.         var $height_num=10;                                        //高分多少等分,默认为10
  21.         var $height_var=0;                                        //高度增量(用户数据平均数)
  22.         var $width_var=0;                                        //宽度增量(用户数据平均数)
  23.         var $height_max=0;                                        //最大数据值
  24.         var $array_data=array();                        //用户待分析的数据的二维数组
  25.         var $array_error=array();                        //收集错误信息

  26.         var $colorBg=array(255,255,255);        //图形背景-白色
  27.         var $colorGrey=array(192,192,192);        //灰色画框
  28.         var $colorBlue=array(0,0,255);                //蓝色
  29.         var $colorRed=array(255,0,0);                //红色(点)
  30.         var $colorDarkBlue=array(0,0,255);        //深色
  31.         var $colorLightBlue=array(200,200,255);                //浅色

  32.         var $array_color;                                        //曲线着色(存储十六进制数)
  33.         var $image;                                                        //我们的图像


  34.         //方法:接受用户数据
  35.         function add_data($array_user_data){
  36.                 if(!is_array($array_user_data) or empty($array_user_data)){
  37.                         $this->;array_error['add_data']="没有可供分析的数据";
  38.                         return false;
  39.                         exit();
  40.                 }
  41.                 $i=count($this->;array_data);
  42.                 $this->;array_data[$i]=$array_user_data;
  43.         }

  44.         //方法:定义画布宽和长
  45.         function set_img($img_width,$img_height){
  46.                 $this->;graphwidth=$img_width;
  47.                 $this->;graphheight=$img_height;
  48.         }

  49.         //设定Y轴的增量等分,默认为10份
  50.         function set_height_num($var_y){
  51.                 $this->;height_num=$var_y;
  52.         }

  53.         //定义各图形各部分色彩
  54.         function get_RGB($color){                                //得到十进制色彩
  55.         $R=($color>;>;16) & 0xff;
  56.         $G=($color>;>;8) & 0xff;
  57.         $B=($color) & 0xff;
  58.         return (array($R,$G,$B));
  59.         }
  60.         //---------------------------------------------------------------
  61.         #定义背景色
  62.         function set_color_bg($c1,$c2,$c3){
  63.                 $this->;colorBg=array($c1,$c2,$c3);
  64.         }
  65.         #定义画框色
  66.         function set_color_Grey($c1,$c2,$c3){
  67.                 $this->;colorGrey=array($c1,$c2,$c3);
  68.         }
  69.         #定义蓝色
  70.         function set_color_Blue($c1,$c2,$c3){
  71.                 $this->;colorBlue=array($c1,$c2,$c3);
  72.         }
  73.         #定义色Red
  74.         function set_color_Red($c1,$c2,$c3){
  75.                 $this->;colorRed=array($c1,$c2,$c3);
  76.         }
  77.         #定义深色
  78.         function set_color_DarkBlue($c1,$c2,$c3){
  79.                 $this->;colorDarkBlue=array($c1,$c2,$c3);
  80.         }
  81.         #定义浅色
  82.         function set_color_LightBlue($c1,$c2,$c3){
  83.                 $this->;colorLightBlue=array($c1,$c2,$c3);
  84.         }
  85.         //---------------------------------------------------------------

  86.         //方法:由用户数据将画布分成若干等份宽
  87.         //并计算出每份多少像素
  88.         function get_width_num(){
  89.                 $this->;width_num=count($this->;array_data[0]);
  90.         }
  91.         function get_max_height(){
  92.                 //获得用户数据的最大值
  93.                 $tmpvar=array();
  94.                 foreach($this->;array_data as $tmp_value){
  95.                         $tmpvar[]=max($tmp_value);
  96.                 }
  97.                 $this->;height_max=max($tmpvar);
  98.                 return max($tmpvar);
  99.         }
  100.         function get_height_length(){
  101.                 //计算出每格的增量长度(用户数据,而不是图形的像素值)
  102.                 $max_var=$this->;get_max_height();
  103.                 $max_var=round($max_var/$this->;height_num);
  104.                 $first_num=substr($max_var,0,1);
  105.                 if(substr($max_var,1,1)){
  106.                         if(substr($max_var,1,1)>;=5)
  107.                                 $first_num+=1;
  108.                 }
  109.                 for($i=1;$i<strlen($max_var);$i++){
  110.                         $first_num.="0";
  111.                 }
  112.                 return (int)$first_num;
  113.         }
  114.         function get_var_wh(){                        //得到高和宽的增量
  115.                 $this->;get_width_num();
  116.                 //得到高度增量和宽度增量
  117.                 $this->;height_var=$this->;get_height_length();
  118.                 $this->;width_var=round($this->;graphwidth/$this->;width_num);
  119.         }

  120.         function set_colors($str_colors){
  121.                 //用于多条曲线的不同着色,如$str_colors="ee00ff,dd0000,cccccc"
  122.                 $this->;array_color=split(",",$str_colors);
  123.         }

  124. ######################################################################################################
  125.         function build_line($var_num){
  126.                 if(!empty($var_num)){                                                //如果用户只选择显示一条曲线
  127.                         $array_tmp[0]=$this->;array_data[$var_num-1];
  128.                         $this->;array_data=$array_tmp;
  129.                 }

  130.                 for($j=0;$j<count($this->;array_data);$j++){
  131.                         list($R,$G,$B)=$this->;get_RGB(hexdec($this->;array_color[$j]));
  132.                         $colorBlue=imagecolorallocate($this->;image,$R,$G,$B);

  133.                         for($i=0;$i<$this->;width_num-1;$i++){
  134.                                 $height_pix=round(($this->;array_data[$j][$i]/$this->;height_max)*$this->;graphheight);
  135.                                 $height_next_pix=round($this->;array_data[$j][$i+1]/$this->;height_max*$this->;graphheight);
  136.                                 imageline($this->;image,$this->;width_var*$i,$this->;graphheight-$height_pix,$this->;width_var*($i+1),$this->;graphheight-$height_next_pix,$colorBlue);
  137.                         }
  138.                 }
  139.                 //画点
  140.                 $colorRed=imagecolorallocate($this->;image, $this->;colorRed[0], $this->;colorRed[1], $this->;colorRed[2]);
  141.                 for($j=0;$j<count($this->;array_data);$j++){
  142.                         for($i=0;$i<$this->;width_num;$i++){
  143.                                 $height_pix=round(($this->;array_data[$j][$i]/$this->;height_max)*$this->;graphheight);
  144.                                 imagearc($this->;image,$this->;width_var*$i,$this->;graphheight-$height_pix,6,5,0,360,$colorRed);
  145.                                 imagefilltoborder($this->;image,$this->;width_var*$i,$this->;graphheight-$height_pix,$colorRed,$colorRed);
  146.                         }
  147.                 }
  148.         }

  149. ######################################################################################################
  150.         function build_rectangle($select_gra){
  151.                 if(!empty($select_gra)){                                                //用户选择显示一个矩形
  152.                         $select_gra-=1;
  153.                 }
  154.                 //画矩形
  155.                 //配色
  156.                 $colorDarkBlue=imagecolorallocate($this->;image, $this->;colorDarkBlue[0], $this->;colorDarkBlue[1], $this->;colorDarkBlue[2]);
  157.                 $colorLightBlue=imagecolorallocate($this->;image, $this->;colorLightBlue[0], $this->;colorLightBlue[1], $this->;colorLightBlue[2]);

  158.                 if(empty($select_gra))
  159.                         $select_gra=0;
  160.                 for($i=0; $i<$this->;width_num; $i++){
  161.                         $height_pix=round(($this->;array_data[$select_gra][$i]/$this->;height_max)*$this->;graphheight);
  162.                         imagefilledrectangle($this->;image,$this->;width_var*$i,$this->;graphheight-$height_pix,$this->;width_var*($i+1),$this->;graphheight, $colorDarkBlue);
  163.                         imagefilledrectangle($this->;image,($i*$this->;width_var)+1,($this->;graphheight-$height_pix)+1,$this->;width_var*($i+1)-5,$this->;graphheight-2, $colorLightBlue);
  164.                 }
  165.         }

  166. ######################################################################################################
  167.         function create_cloths(){
  168.                 //创建画布
  169.                 $this->;image=imagecreate($this->;graphwidth+20,$this->;graphheight+20);
  170.         }
  171.         function create_frame(){
  172.                 //创建画框
  173.                 $this->;get_var_wh();
  174.                 //配色
  175.                 $colorBg=imagecolorallocate($this->;image, $this->;colorBg[0], $this->;colorBg[1], $this->;colorBg[2]);
  176.                 $colorGrey=imagecolorallocate($this->;image, $this->;colorGrey[0], $this->;colorGrey[1], $this->;colorGrey[2]);
  177.                 //创建图像周围的框
  178.                 imageline($this->;image, 0, 0, 0, $this->;graphheight,$colorGrey);
  179.                 imageline($this->;image, 0, 0, $this->;graphwidth, 0,$colorGrey);
  180.                 //imageline($this->;image, ($this->;graphwidth-1),0,($this->;graphwidth-1),($this->;graphheight-1),$colorGrey);
  181.                 imageline($this->;image, 0,($this->;graphheight-1),($this->;graphwidth-1),($this->;graphheight-1),$colorGrey);
  182.         }
  183.         function create_line(){
  184.                 //创建网格。
  185.                 $this->;get_var_wh();
  186.                 $colorBg=imagecolorallocate($this->;image, $this->;colorBg[0], $this->;colorBg[1], $this->;colorBg[2]);
  187.                 $colorGrey=imagecolorallocate($this->;image, $this->;colorGrey[0], $this->;colorGrey[1], $this->;colorGrey[2]);
  188.                 $colorRed=imagecolorallocate($this->;image, $this->;colorRed[0], $this->;colorRed[1], $this->;colorRed[2]);
  189.                 for($i=1;$i<=$this->;height_num;$i++){
  190.                         //画横线
  191.                         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);
  192.                         //标出数字
  193.                         imagestring($this->;image,2,0,$this->;graphheight-($this->;height_var/$this->;height_max*$this->;graphheight)*$i,$this->;height_var*$i,$colorRed);
  194.                 }
  195.                 for($i=1;$i<=$this->;width_num;$i++){
  196.                         //画竖线
  197.                         imageline($this->;image,$this->;width_var*$i,0,$this->;width_var*$i,$this->;graphwidth,$colorGrey);
  198.                         //标出数字
  199.                         imagestring($this->;image,2,$this->;width_var*$i,$this->;graphheight-15,$i,$colorRed);
  200.                 }
  201.         }

  202.         function build($graph,$str_var){
  203.                 //$graph是用户指定的图形种类,$str_var是生成哪个数据的图
  204.                 header("Content-type: image/png");
  205.                 $this->;create_cloths();                        //先要有画布啊~~
  206.                 switch ($graph){
  207.                         case "line":
  208.                                 $this->;create_frame();                        //画个框先:)
  209.                                 $this->;create_line();                        //打上底格线
  210.                                 $this->;build_line($str_var);                        //画曲线
  211.                                 break;
  212.                         case "rectangle":
  213.                                 $this->;create_frame();                                                //画个框先:)
  214.                                 $this->;build_rectangle($str_var);                        //画矩形
  215.                                 $this->;create_line();                                                //打上底格线
  216.                                 break;
  217.                 }


  218.                 //输出图形并清除内存
  219.                 imagepng($this->;image);
  220.                 imagedestroy($this->;image);
  221.         }

  222. ######################################################################################################

  223. }

  224. //使用示例
  225. $gg=new build_graph();
  226. $d1=array(62,25,20,20,200,99);
  227. $d2=array(200,125,255,200,56,79);
  228. $d3=array(60,50,25,12,56,45);
  229. $gg->;add_data($d1);
  230. $gg->;add_data($d2);
  231. $gg->;add_data($d3);

  232. $gg->;set_colors("ee00ff,dd8800,00ff00");
  233. //生成曲线图
  234. //$gg->;build("line",1);                        //参数0表示显示所有曲线,1为显示第一条,依次类推
  235. //生成矩形图
  236. $gg->;build("rectangle","2");        //参数0表示显示第一个矩形,1也为显示第一条,其余依次类推

  237. /*       
  238. //自定义图形显示,可任意图形叠加显示
  239.                 header("Content-type: image/png");
  240.                 $gg->;create_cloths();                        //画布
  241.                 $gg->;create_frame();                        //画个框先
  242.                 $gg->;build_rectangle(2);                //画矩形
  243.                 $gg->;create_line();                                //画线
  244.                 $gg->;build_line(2);                                //画曲线
  245.                 imagepng($gg->;image);
  246.                 imagedestroy($gg->;image);
  247. */       
  248. ?>;
复制代码

论坛徽章:
0
2 [报告]
发表于 2004-06-21 00:48 |只看该作者

用PHP进行图形分析[20040621]

支持一下原创先。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
3 [报告]
发表于 2004-06-21 00:59 |只看该作者

用PHP进行图形分析[20040621]

你的进步让我吃惊,赞一个

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
4 [报告]
发表于 2004-06-21 07:40 |只看该作者

用PHP进行图形分析[20040621]

这个类是昨天下午写的。当时是在玩CS和写这个类之间做了十分钟思想斗争过后决定完后的。 只做了简单测试。本想还加上一个饼图生成,但看到已有人写过了,所以没加上去。
这个类有一点比较令我高兴的是:它的Y轴增量值可根据用户数据自适应成我们的习惯。
比如,最大值是688时,它会自动将增量值设为70(假设Y轴分为10格的话),而不是68.8,这样会更适合我们的习惯。

论坛徽章:
0
5 [报告]
发表于 2004-06-21 09:05 |只看该作者

用PHP进行图形分析[20040621]

原帖由 "tonera" 发表:
这个类是昨天下午写的。当时是在玩CS和写这个类之间做了十分钟思想斗争过后决定完后的。 只做了简单测试。本想还加上一个饼图生成,但看到已有人写过了,所以没加上去。
这个类有一点比较令我高兴的是:它的Y轴?.........



    

又有现成的可以用了,活活!
正在做的项目有统计功能,正好用上,哈哈哈哈!
可以用在商业软件中吧?如果我进行了修改或扩展一定贴出来。

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
6 [报告]
发表于 2004-06-21 09:16 |只看该作者

用PHP进行图形分析[20040621]

原帖由 "dualface" 发表:



    

又有现成的可以用了,活活!
正在做的项目有统计功能,正好用上,哈哈哈哈!
可以用在商业软件中吧?如果我进行了修改或扩展一定贴出来。


自由使用!只要开头保留tonera六个字母就行了(一点点慰藉: ),
另外:里面有些逻辑感觉还不太清楚,还有坐标轴上的文字位置放的不好。你改改看,我来学习。

论坛徽章:
0
7 [报告]
发表于 2004-06-21 09:25 |只看该作者

用PHP进行图形分析[20040621]

样例中y轴上部为何少半格?

建议扩充图形叠加功能,比如“直方图”就需要矩形图和折线图共存

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
8 [报告]
发表于 2004-06-21 09:50 |只看该作者

用PHP进行图形分析[20040621]

原帖由 "xuzuning" 发表:
样例中y轴上部为何少半格?

建议扩充图形叠加功能,比如“直方图”就需要矩形图和折线图共存


谢谢。
上面不是少半格,是因为这个图形的Y轴最大值是由用户数据的最大值决定的。上面突出的半格是因为数据不足一格,只有半格。

图形叠加确实是一个好主意,有时间试着看看。

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
9 [报告]
发表于 2004-06-21 21:26 |只看该作者

用PHP进行图形分析[20040621]

晚上回来修改了一下,进一步模块化。现在才像个真正的类。(点和线仍然没有分离,一因为大多数时是在一起的,二是因为太懒了。)
可以叠加显示了。也能指定显示几条曲线。

论坛徽章:
0
10 [报告]
发表于 2004-06-23 11:12 |只看该作者

用PHP进行图形分析[20040621]

本来自己也琢磨这几天写个类似的统计图形生成类,没想到今天竟然看到了现成的,感谢楼主   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP