用户名:
密 码: 记住
您当前的位置:首页 > 网络编程 > php教程

php生成曲线图程序

时间:2015-01-23  来源:西部数据  作者:西部数据
  1. <?php 
  2. /*******************用法************************* 
  3.     $gg=new build_graph(); 
  4.      
  5.     $d1=array(0,62,25,20,20,100,99);  //曲线一 
  6. //$d1=array('15'=>5,'16'=>8,'17'=>7,'18'=>9,'19'=>10,'20'=>15,'21'=>9); 改成这个形式啦 
  7.     $d2=array(0,80,75,65,100,56,79);  //曲线二 
  8.     $d3=array(0,60,50,25,12,56,45);   //曲线三 一下可以继续以此类推 
  9.      
  10.     $gg->add_data($d1); 
  11.     $gg->add_data($d2); 
  12.     $gg->add_data($d3); 
  13.      
  14.     $gg->set_colors("ee00ff,dd8800,00ff00"); //对应曲线的颜色 
  15.      
  16.     //生成曲线图 
  17.     $gg->build("line",0);          //参数0表示显示所有曲线,1为显示第一条,依次类推 
  18.      
  19.     //生成矩形图 
  20.     //$gg->build("rectangle","2");    //参数0表示显示第一个矩形,1也为显示第一条,其余依次类推 
  21.      
  22. /////////////////////////////////////////////////////////// 
  23.     //自定义图形显示,可任意图形叠加显示 
  24.        header("Content-type: image/png"); 
  25.        $gg->create_cloths();          //画布 
  26.        $gg->create_frame();          //画个框先 
  27.        //$gg->build_rectangle(2);       //画矩形 
  28.        $gg->create_line();             //画线 
  29.        $gg->build_line(0);             //画曲线 
  30.        imagepng($gg->image); 
  31.        imagedestroy($gg->image); 
  32. */ 
  33. class build_graph { 
  34.     var $graphwidth=300; 
  35.     var $graphheight=300; 
  36.     var $width_num=0;                //宽分多少等分 
  37.     var $height_num=10;                //高分多少等分,默认为10 
  38.     var $height_var=0;                //高度增量(用户数据平均数) 
  39.     var $width_var=0;                //宽度增量(用户数据平均数) 
  40.     var $height_max=0;                //最大数据值 
  41.     var $array_data=array();          //用户待分析的数据的二维数组 
  42.     var $array_error=array();          //收集错误信息 
  43.     var $colorBg=array(255,255,255);    //图形背景-白色 
  44.     var $colorGrey=array(192,192,192);    //灰色画框 
  45.     var $colorBlue=array(0,0,255);       //蓝色 
  46.     var $colorRed=array(255,0,0);       //红色(点) 
  47.     var $colorDarkBlue=array(0,0,255);    //深色 
  48.     var $colorLightBlue=array(200,200,255);       //浅色 
  49.     var $array_color;                //曲线着色(存储十六进制数) 
  50.     var $image;                      //我们的图像 
  51.     //方法:接受用户数据 
  52.     function add_data($array_user_data
  53.     { 
  54.        if(!is_array($array_user_dataor emptyempty($array_user_data)) 
  55.        { 
  56.           $this->array_error['add_data']="没有可供分析的数据"
  57.           return false; 
  58.           exit(); 
  59.        } 
  60.         
  61.        $i=count($this->array_data); 
  62.         
  63.        $this->array_data[$i]=$array_user_data
  64.         
  65.     } 
  66.     //方法:定义画布宽和长 
  67.     function set_img($img_width,$img_height){ 
  68.        $this->graphwidth=$img_width
  69.        $this->graphheight=$img_height
  70.     } 
  71.     //设定Y轴的增量等分,默认为10份 
  72.     function set_height_num($var_y){ 
  73.        $this->height_num=$var_y
  74.     } 
  75.     //定义各图形各部分色彩 
  76.     function get_RGB($color){             //得到十进制色彩 
  77.     $R=($color>>16) & 0xff; 
  78.     $G=($color>>8) & 0xff; 
  79.     $B=($color) & 0xff; 
  80.     return (array($R,$G,$B)); 
  81.     } 
  82.     //--------------------------------------------------------------- 
  83.     #定义背景色 
  84.     function set_color_bg($c1,$c2,$c3){ 
  85.        $this->colorBg=array($c1,$c2,$c3); 
  86.     } 
  87.     #定义画框色 
  88.     function set_color_Grey($c1,$c2,$c3){ 
  89.        $this->colorGrey=array($c1,$c2,$c3); 
  90.     } 
  91.     #定义蓝色 
  92.     function set_color_Blue($c1,$c2,$c3){ 
  93.        $this->colorBlue=array($c1,$c2,$c3); 
  94.     } 
  95.     #定义色Red 
  96.     function set_color_Red($c1,$c2,$c3){ 
  97.        $this->colorRed=array($c1,$c2,$c3); 
  98.     } 
  99.     #定义深色 
  100.     function set_color_DarkBlue($c1,$c2,$c3){ 
  101.        $this->colorDarkBlue=array($c1,$c2,$c3); 
  102.     } 
  103.     #定义浅色 
  104.     function set_color_LightBlue($c1,$c2,$c3){ 
  105.        $this->colorLightBlue=array($c1,$c2,$c3); 
  106.     } 
  107.     //--------------------------------------------------------------- 
  108.     //方法:由用户数据将画布分成若干等份宽 
  109.     //并计算出每份多少像素 
  110.     function get_width_num(){ 
  111.        $this->width_num=count($this->array_data[0]); 
  112.     } 
  113.      
  114.     function get_max_height(){ 
  115.        //获得用户数据的最大值 
  116.        $tmpvar=array(); 
  117.            
  118.        foreach($this->array_data as $tmp_value
  119.        { 
  120.           $tmpvar[]=max($tmp_value); 
  121.        } 
  122.         
  123.        $this->height_max=max($tmpvar); 
  124.         
  125.        return max($tmpvar); 
  126.     } 
  127.      
  128.     function get_height_length(){ 
  129.        //计算出每格的增量长度(用户数据,而不是图形的像素值) 
  130.        $max_var=$this->get_max_height(); 
  131.        $max_var=round($max_var/$this->height_num); 
  132.        $first_num=substr($max_var,0,1); 
  133.        if(substr($max_var,1,1)){ 
  134.           if(substr($max_var,1,1)>=5) 
  135.              $first_num+=1; 
  136.        } 
  137.        for($i=1;$i<strlen($max_var);$i++){ 
  138.           $first_num.="0"
  139.        } 
  140.        return (int)$first_num
  141.     } 
  142.      
  143.     function get_var_wh()  //得到高和宽的增量 
  144.     {           
  145.        $this->get_width_num(); 
  146.        //得到高度增量和宽度增量 
  147.        $this->height_var=$this->get_height_length(); 
  148.         
  149.        $this->width_var=round($this->graphwidth/$this->width_num); 
  150.     } 
  151.     function set_colors($str_colors){ 
  152.        //用于多条曲线的不同着色,如$str_colors="ee00ff,dd0000,cccccc" 
  153.        $this->array_color=split(",",$str_colors); 
  154.     } 
  155. ###################################################################################################### 
  156.     function build_line($var_num
  157.     { 
  158.        if(!emptyempty($var_num)) 
  159.        {                   //如果用户只选择显示一条曲线 
  160.           $array_tmp[0]=$this->array_data[$var_num-1]; 
  161.           $this->array_data=$array_tmp
  162.        } 
  163.         
  164.         //画线 
  165.           
  166.        for($j=0;$j<count($this->array_data);$j++) 
  167.        { 
  168.           list($R,$G,$B)=$this->get_RGB(hexdec($this->array_color[$j])); 
  169.            
  170.           $colorBlue=imagecolorallocate($this->image,$R,$G,$B); 
  171.            
  172.             $i=0; 
  173.               foreach($this->array_data[$jas $keys=>$values
  174.             { 
  175.                 $height_next_pix[]=round($this->array_data[$j][$keys]/$this->height_max*$this->graphheight); 
  176.             } 
  177.              
  178.             foreach($this->array_data[$jas $key=>$value
  179.             { 
  180.                 $height_pix=round(($this->array_data[$j][$key]/$this->height_max)*$this->graphheight); 
  181.                  
  182.                 if($i!=count($this->array_data[$j])-1) 
  183.                 { 
  184.                     imageline($this->image,$this->width_var*$i,$this->graphheight-$height_pix,$this->width_var*($i+1),$this->graphheight-$height_next_pix[$i+1],$colorBlue); 
  185.                 } 
  186.                  
  187.                 $i++; 
  188.             } 
  189.           
  190.         //print_r($height_next_pix); 
  191.         // exit; 
  192.          /* 
  193.           for($i=0;$i<$this->width_num-1;$i++) 
  194.           { 
  195.              $height_pix=round(($this->array_data[$j][$i]/$this->height_max)*$this->graphheight); 
  196.              $height_next_pix=round($this->array_data[$j][$i+1]/$this->height_max*$this->graphheight); 
  197.              imageline($this->image,$this->width_var*$i,$this->graphheight-$height_pix,$this->width_var*($i+1),$this->graphheight-$height_next_pix,$colorBlue); 
  198.           }*/ 
  199.        } 
  200.         
  201.         
  202.        //画点 
  203.         
  204.        $colorRed=imagecolorallocate($this->image, $this->colorRed[0], $this->colorRed[1], $this->colorRed[2]); 
  205.        for($j=0;$j<count($this->array_data);$j++) 
  206.        { 
  207.                $i=0; 
  208.             foreach($this->array_data[$jas $key=>$value
  209.             { 
  210.                  $height_pix=round(($this->array_data[$j][$key]/$this->height_max)*$this->graphheight); 
  211.                  imagearc($this->image,$this->width_var*$i,$this->graphheight-$height_pix,6,5,0,360,$colorRed); 
  212.                  imagefilltoborder($this->image,$this->width_var*$i,$this->graphheight-$height_pix,$colorRed,$colorRed); 
  213.                  $i++; 
  214.             } 
  215.          
  216.         /* 
  217.           for($i=0;$i<$this->width_num;$i++) 
  218.           { 
  219.              $height_pix=round(($this->array_data[$j][$i]/$this->height_max)*$this->graphheight); 
  220.              imagearc($this->image,$this->width_var*$i,$this->graphheight-$height_pix,6,5,0,360,$colorRed); 
  221.              imagefilltoborder($this->image,$this->width_var*$i,$this->graphheight-$height_pix,$colorRed,$colorRed); 
  222.           } 
  223.          */ 
  224.        } 
  225.         
  226.     } 
  227. ###################################################################################################### 
  228.     function build_rectangle($select_gra){ 
  229.        if(!emptyempty($select_gra)){                   //用户选择显示一个矩形 
  230.           $select_gra-=1; 
  231.        } 
  232.        //画矩形 
  233.        //配色 
  234.        $colorDarkBlue=imagecolorallocate($this->image, $this->colorDarkBlue[0], $this->colorDarkBlue[1], $this->colorDarkBlue[2]); 
  235.        $colorLightBlue=imagecolorallocate($this->image, $this->colorLightBlue[0], $this->colorLightBlue[1], $this->colorLightBlue[2]); 
  236.        if(emptyempty($select_gra)) 
  237.           $select_gra=0; 
  238.        for($i=0; $i<$this->width_num; $i++){ 
  239.           $height_pix=round(($this->array_data[$select_gra][$i]/$this->height_max)*$this->graphheight); 
  240.           imagefilledrectangle($this->image,$this->width_var*$i,$this->graphheight-$height_pix,$this->width_var*($i+1),$this->graphheight, $colorDarkBlue); 
  241.           imagefilledrectangle($this->image,($i*$this->width_var)+1,($this->graphheight-$height_pix)+1,$this->width_var*($i+1)-5,$this->graphheight-2, $colorLightBlue); 
  242.        } 
  243.     } 
  244. ###################################################################################################### 
  245.     function create_cloths(){ 
  246.        //创建画布 
  247.        $this->image=imagecreate($this->graphwidth+20,$this->graphheight+20); 
  248.     } 
  249.     function create_frame(){ 
  250.        //创建画框 
  251.        $this->get_var_wh(); 
  252.        //配色 
  253.        $colorBg=imagecolorallocate($this->image, $this->colorBg[0], $this->colorBg[1], $this->colorBg[2]); 
  254.        $colorGrey=imagecolorallocate($this->image, $this->colorGrey[0], $this->colorGrey[1], $this->colorGrey[2]); 
  255.        //创建图像周围的框 
  256.        imageline($this->image, 0, 0, 0, $this->graphheight,$colorGrey); 
  257.        imageline($this->image, 0, 0, $this->graphwidth, 0,$colorGrey); 
  258.        imageline($this->image, ($this->graphwidth-1),0,($this->graphwidth-1),($this->graphheight-1),$colorGrey); 
  259.        imageline($this->image, 0,($this->graphheight-1),($this->graphwidth-1),($this->graphheight-1),$colorGrey); 
  260.     } 
  261.      
  262.     function create_line() 
  263.     {//开源代码phpfensi.com 
  264.        //创建网格。 
  265.        $this->get_var_wh(); 
  266.        $colorBg=imagecolorallocate($this->image, $this->colorBg[0], $this->colorBg[1], $this->colorBg[2]); 
  267.        $colorGrey=imagecolorallocate($this->image, $this->colorGrey[0], $this->colorGrey[1], $this->colorGrey[2]); 
  268.        $colorRed=imagecolorallocate($this->image, $this->colorRed[0], $this->colorRed[1], $this->colorRed[2]); 
  269.         
  270.        for($i=1;$i<=$this->height_num;$i++) 
  271.        { 
  272.           //画横线 
  273.           $y1=($this->graphheight-($this->height_var/$this->height_max*$this->graphheight)*$i); 
  274.            
  275.           $y2=($this->graphheight-($this->height_var/$this->height_max*$this->graphheight)*$i); 
  276.            
  277.           imageline($this->image,0,$y1,$this->graphwidth,$y2,$colorGrey); 
  278.            
  279.           //标出数字 
  280.           imagestring($this->image,2,0,$this->graphheight-($this->height_var/$this->height_max*$this->graphheight)*$i,$this->height_var*$i,$colorRed); 
  281.        } 
  282.         
  283.        unset($i); 
  284.         
  285.        foreach($this->array_data[0] as $key=>$value
  286.        { 
  287.               //画竖线 
  288.               imageline($this->image,$this->width_var*$i,0,$this->width_var*$i,$this->graphwidth,$colorGrey); 
  289.            
  290.           //标出数字 
  291.            imagestring($this->image,2,$this->width_var*$i,$this->graphheight-15,$key,$colorRed); 
  292.             
  293.            $i++; 
  294.        } 
  295.         
  296.        /* 
  297.        for($i=1;$i<=$this->width_num;$i++) 
  298.        { 
  299.           //画竖线 
  300.           imageline($this->image,$this->width_var*$i,0,$this->width_var*$i,$this->graphwidth,$colorGrey); 
  301.           //标出数字 
  302.           imagestring($this->image,2,$this->width_var*$i,$this->graphheight-15,$i,$colorRed); 
  303.        } 
  304.        */ 
  305.     } 
  306.     function build($graph,$str_var){ 
  307.        //$graph是用户指定的图形种类,$str_var是生成哪个数据的图 
  308.        header("Content-type: image/jpeg"); 
  309.        $this->create_cloths();          //先要有画布啊~~ 
  310.        switch ($graph){ 
  311.           case "line"
  312.              $this->create_frame();          //画个框先:) 
  313.              $this->create_line();          //打上底格线 
  314.              $this->build_line($str_var);          //画曲线 
  315.              break
  316.           case "rectangle"
  317.              $this->create_frame();                   //画个框先:) 
  318.              $this->build_rectangle($str_var);          //画矩形 
  319.              $this->create_line();                   //打上底格线 
  320.              break
  321.        } 
  322.        //输出图形并清除内存 
  323.        imagepng($this->image); 
  324.        imagedestroy($this->image); 
  325.     } 
  326. ?> 
来顶一下
返回首页
返回首页
推荐资讯
WiFi太不安全:7岁女孩11分钟内入侵公共网络 WiFi太不安全:7岁女孩11分钟内入侵近期刚刚发布研究说WiFi网络能获得人们手机里多少私人信息,
不服跑个分?人工智能也出现“刷分”乱象 不服跑个分?人工智能也出现“刷分2014年,人工智能领域突然爆发,成为了科研和科技创业的热门
相关文章
栏目更新
栏目热门