帐号: 密码:
// 设为首页 // 收藏本站 // 请您留言 // 网址导航
远方教程-满足你的求知欲!
站内搜索:
HTML ASP PHP CSS DIV Dreamweaver Photoshop Word Excel PPT SEO技巧
您当前位置:网站首页 >> PHP教程 >> 常用PHP函数 >> 阅读文章

常用php函数[08]:生成曲线图的類

来源:远方教程 作者:远方教程 发布时间:2015-06-17 查看次数:3350 访问[新版]
  1. <?php
  2.     class LabelFormat{
  3.         var $width;
  4.         var $precision;
  5.         var $specific;
  6.         var $vertical;
  7.         var $fontsize;
  8.         var $fun;
  9.  
  10.         function LabelFormat ($width=0, $prec=1, $spec="f", $fontsize=3, $vert=0, $function="") {
  11.             $this->fun=$function;
  12.             $this->width=$width;
  13.             $this->precision=$prec;
  14.             $this->specific=$spec;
  15.             $this->fontsize=$fontsize;
  16.             $this->vertical=$vert;
  17.         }
  18.  
  19.         function Out ($value){
  20.              //$this->fun=$fun;
  21.             $format_string="%";
  22.             if ($this->width)
  23.             $format_string.=$this->width;
  24.             if ($this->precision)
  25.                 $format_string.=".".$this->precision;
  26.             $format_string.=$this->specific;
  27.             if (!empty($this->fun)){
  28.                 $out=call_user_func ($this->fun, $value);
  29.                 $wstring=sprintf($format_string, $out);
  30.             }
  31.             else
  32.                 $wstring=sprintf($format_string, $value);
  33.             return $wstring;
  34.         }
  35.     }
  36.  
  37.     class graph2d{
  38.         var $image_type;
  39.         var $dimx;
  40.         var $dimy;
  41.  
  42.         var $xmin;
  43.         var $xmax;
  44.  
  45.         var $ymin;
  46.         var $ymax;
  47.  
  48.         var $xstep;
  49.         var $ystep;
  50.  
  51.         var $xskip;
  52.         var $yskip;
  53.  
  54.         var $xformat;
  55.         var $yformat;
  56.  
  57.         var $b_red;
  58.         var $b_green;
  59.         var $b_blue;
  60.         var $b_alpha;
  61.         var $a_red;
  62.         var $a_green;
  63.         var $a_blue;
  64.         var $a_alpha;
  65.  
  66.         var $is_grid;
  67.         var $grid_red;
  68.         var $grid_green;
  69.         var $grid_blue;
  70.         //var $grid_style;
  71.  
  72.         var $top;
  73.         var $left;
  74.         var $right;
  75.         var $bottom;
  76.  
  77.         var $xlegend;
  78.         var $ylegend;
  79.         var $title;
  80.  
  81.         var $image;
  82.  
  83.         function graph2d ($image_type, $width, $height, $xleft=0, $ytop=0, $xright=0, $ybottom=0, $br=0xFF, $bg=0xFF, $bb=0xFF, $b_alpha=0, $ar=0, $ag=0, $ab=0, $a_alpha=0)
  84.         {
  85.             $this->image_type=$image_type;
  86.             $this->dimx=(int)$width;
  87.             $this->dimy=(int)$height;
  88.            
  89.             $this->b_red=(int)$br;
  90.             $this->b_green=(int)$bg;
  91.             $this->b_blue=(int)$bb;
  92.  
  93.             $this->a_red=(int)$ar;
  94.             $this->a_green=(int)$ag;
  95.             $this->a_blue=(int)$ab;
  96.  
  97.  
  98.             $this->a_alpha=(int)$a_alpha;
  99.             $this->b_alpha=(int)$b_alpha;
  100.  
  101.             $this->left=(int)$xleft;
  102.             $this->top=(int)$ytop;
  103.             $this->right=(int)$xright;
  104.             $this->bottom=(int)$ybottom;
  105.  
  106.             $this->xskip=1;
  107.             $this->yskip=1;
  108.  
  109.             $this->xformat=new LabelFormat();
  110.             $this->yformat=new LabelFormat();
  111.  
  112.             $this->xlegend="";
  113.             $this->ylegend="";
  114.             $this->title="";
  115.  
  116.             $this->is_grid=FALSE;
  117.             //$this->grid_style=array();
  118.  
  119.             $imtype="Content-type: image/".$this->image_type;
  120.             header ($imtype);
  121.             $this->image=@imagecreate ($this->dimx, $this->dimy);// or die "Cannot initialize new GD Image stream";
  122.         }
  123.  
  124.         function SetAxisScales ($xmin, $xmax, $xstep, $xskip, $ymin, $ymax, $ystep, $yskip)
  125.         {
  126.             $this->xmin=(double)$xmin;
  127.             $this->xmax=(double)$xmax;
  128.             $this->xstep=(double)$xstep;
  129.             $this->xskip=(int)$xskip;
  130.  
  131.             $this->ymin=(double)$ymin;
  132.             $this->ymax=(double)$ymax;
  133.             $this->ystep=(double)$ystep;
  134.             $this->yskip=(int)$yskip;
  135.         }
  136.  
  137.         function SetXLegend ($xleg) { $this->xlegend=$xleg; }
  138.  
  139.         function SetYLegend ($yleg) { $this->ylegend=$yleg; }
  140.  
  141.         function SetTitle ($title) { $this->title=$title; }
  142.  
  143.         function SetLineThickness ($thikness)
  144.         {
  145.             if ( is_resource ($this->image) && is_int ($thikness))
  146.                 imagesetthickness ($this->image, $thikness);
  147.         }
  148.  
  149.         function SetGrid($grid, $red, $green, $blue)
  150.         {
  151.             $this->is_grid=$grid;
  152.             $this->grid_red=$red;
  153.             $this->grid_green=$green;
  154.             $this->grid_blue=$blue;
  155.             //$this->grid_style=$style;
  156.         }
  157.  
  158.         function GraphCoord ()
  159.         {
  160.             if (function_exists("imagecolorallocatealpha"))
  161.             {
  162.                 $background=imagecolorallocatealpha ($this->image, $this->b_red, $this->b_green, $this->b_blue, $this->b_alpha);
  163.                 $foreground=imagecolorallocatealpha ($this->image, $this->a_red, $this->a_green, $this->a_blue, $this->a_alpha);
  164.             }
  165.             else
  166.             {
  167.                 $background=imagecolorallocate ($this->image, $this->b_red, $this->b_green, $this->b_blue);
  168.                 if ($this->b_alpha)
  169.                     imagecolortransparent($this->image, $background);
  170.                 $foreground=imagecolorallocate ($this->image, $this->a_red, $this->a_green, $this->a_blue);
  171.             }
  172.  
  173.             $X=$this->dimx-($this->left+$this->right);
  174.             $Y=$this->dimy-($this->top+$this->bottom);
  175.  
  176.             $scaleX=($this->xmax-$this->xmin)/$X;
  177.             $scaleY=($this->ymax-$this->ymin)/$Y;
  178.  
  179.             $gcolor=imagecolorallocate ($this->image, $this->grid_red, $this->grid_green, $this->grid_blue);
  180.             if ($this->is_grid)
  181.                 $coord=imagerectangle($this->image, $this->left, $this->top, $this->dimx-$this->right, $this->dimy-$this->bottom, $gcolor);
  182.             else
  183.                 $coord=imagerectangle($this->image, $this->left, $this->top, $this->dimx-$this->right, $this->dimy-$this->bottom, $foreground);
  184.             if (!empty($this->title))
  185.                 imagestring ($this->image, 4, $this->left+$X/2-strlen($this->title)*imagefontwidth(4)/2, $this->top/2-imagefontheight(4)/2, $this->title, $foreground);
  186.             $numx=($this->xmax-$this->xmin)/$this->xstep+1;
  187.             $numy=($this->ymax-$this->ymin)/$this->ystep+1;
  188.             for ($i=0; $i<$numx; $i++)
  189.             {
  190.                 $x=($i*$X)/($numx-1)+$this->left;
  191.                 if ($i>0 && $i<$numx-1)
  192.                     if ($this->is_grid)
  193.                         imageline ($this->image, $x, $Y+$this->top, $x, $this->top, $gcolor);
  194.                     else
  195.                         imageline ($this->image, $x, $Y+$this->top-5, $x, $Y+$this->top+5, $foreground);
  196.                 if ($i%$this->xskip==0)
  197.                 {
  198.                     $xstr=$this->xformat->Out($this->xmin+($i*$X)*$scaleX/($numx-1));
  199.                     if ($this->xformat->vertical)
  200.                         imagestringup ($this->image, $this->xformat->fontsize, $x-imagefontwidth($this->xformat->fontsize), $Y+$this->top+strlen($xstr)*imagefontheight($this->xformat->fontsize)/2+5, $xstr, $foreground);
  201.                     else
  202.                         imagestring ($this->image, $this->xformat->fontsize, $x-strlen($xstr)*imagefontwidth($this->xformat->fontsize)/2, $Y+$this->top+imagefontheight($this->xformat->fontsize)/2, $xstr, $foreground);
  203.                 }
  204.             }
  205.             if (!empty($this->xlegend))
  206.                 imagestring ($this->image, 3, $this->left+$X/2, $this->top+$Y+3*imagefontheight(3)/2, $this->xlegend, $foreground);
  207.             for ($i=0; $i<$numy; $i++)
  208.             {
  209.                 $y=($i*$Y)/($numy-1);
  210.                 if ($i>0 && $i<$numy-1)
  211.                     if ($this->is_grid)
  212.                         imageline ($this->image, $this->left-5, $y+$this->top, $this->left+$X, $y+$this->top, $gcolor);
  213.                     else
  214.                         imageline ($this->image, $this->left-5, $y+$this->top, $this->left+5, $y+$this->top, $foreground);
  215.                 if ($i%$this->yskip==0)
  216.                 {
  217.                     $ystr=$this->yformat->Out($this->ymin+($i*$Y)*$scaleY/($numy-1));
  218.                     if ($this->yformat->vertical)
  219.                         imagestringup ($this->image, $this->yformat->fontsize, $this->left-imagefontheight($this->yformat->fontsize)-5, $Y+$this->top+$this->bottom-$y-3*strlen($ystr)*imagefontwidth($this->yformat->fontsize)/2, $ystr, $foreground);
  220.                     else
  221.                         imagestring ($this->image, $this->yformat->fontsize, $this->left-strlen($ystr)*imagefontwidth($this->yformat->fontsize)-5, $Y+$this->top-$y-imagefontheight($this->yformat->fontsize)/2, $ystr, $foreground);
  222.                 }
  223.             }
  224.             if (!empty($this->ylegend))
  225.                 imagestringup ($this->image, 3, $this->left/2-3*imagefontheight(3)/2, $this->top+$Y/2+strlen($this->ylegend)*imagefontwidth(3)/2, $this->ylegend, $foreground);
  226.         }
  227.  
  228.         function Draw ($xarray, $yarray, $r=0, $g=0, $b=0)
  229.         {
  230.             if (!is_array($xarray) || !is_array($yarray)) return;
  231.  
  232.             if (function_exists("imagecolorallocatealpha"))
  233.             {
  234.                 $background=imagecolorallocatealpha ($this->image, $this->b_red, $this->b_green, $this->b_blue, $this->b_alpha);
  235.                 $foreground=imagecolorallocatealpha ($this->image, $this->a_red, $this->a_green, $this->a_blue, $this->a_alpha);
  236.             }
  237.             else
  238.             {
  239.                 $background=imagecolorallocate ($this->image, $this->b_red, $this->b_green, $this->b_blue);
  240.                 if ($this->b_alpha)
  241.                     imagecolortransparent($this->image, $background);
  242.                 $foreground=imagecolorallocate ($this->image, $this->a_red, $this->a_green, $this->a_blue);
  243.             }
  244.             $graphcolor=imagecolorallocate ($this->image, $r, $g, $b);
  245.  
  246.             $X=$this->dimx-($this->left+$this->right);
  247.             $Y=$this->dimy-($this->top+$this->bottom);
  248.  
  249.             $scaleX=($this->xmax-$this->xmin)/$X;
  250.             $scaleY=($this->ymax-$this->ymin)/$Y;
  251.  
  252.             $numx=($this->xmax-$this->xmin)/$this->xstep+1;
  253.             $numy=($this->ymax-$this->ymin)/$this->ystep+1;
  254.  
  255.             $nx=count($xarray);
  256.             $ny=count($yarray);
  257.             $n=min($nx, $ny);
  258.  
  259.             for ($i=1; $i<$n; $i++)
  260.                 imageline($this->image, $this->left+($xarray[$i-1]-$this->xmin)/$scaleX, $this->top+($this->ymax-$yarray[$i-1])/$scaleY, $this->left+($xarray[$i]-$this->xmin)/$scaleX, $this->top+($this->ymax-$yarray[$i])/$scaleY, $graphcolor);
  261.         }
  262.  
  263.         function ImageOut ($filename)
  264.         {
  265.             if (strcasecmp ($this->image_type, "jpeg")==0)
  266.                 imagejpeg ($this->image, $filename, 100);
  267.             elseif (strcasecmp ($this->image_type, "png")==0)
  268.                 imagepng ($this->image, $filename, 100);
  269.             elseif (strcasecmp ($this->image_type, "gif")==0)
  270.                 imagegif ($this->image, $filename, 100);
  271.             elseif (strcasecmp ($this->image_type, "wbmp")==0)
  272.                 imagewbmp ($this->image, $filename, 100);
  273.         }
  274.  
  275.         function Close ()
  276.         {
  277.             imagedestroy ($this->image);
  278.         }
  279.     }
  280. ?>
打印 打印 | 关闭 关闭 评论
相关文章
图片新闻
站内搜索  
搜索
猜您喜欢  
最新更新  
阅读排行  
关于我们 | 联系方式 | 大事记 | 免责声明 | | 给我留言
部分广告源自金山联盟2345联盟 QQ咨询 站长之家QQ群:232617873
Copyright 2024 远方教程 © All Rights Reserved.

回顶部