• php gd 库绘图 正态分布


    <?php
     class Draw{
     private $im,$stu=FALSE;  //
     protected   $sets = array (    //可设置参数
             "size" => array (    //图像大小 
                 "width" => 900,
                 "height" => 700 
               ),               
             "position" => array (  //圆点位置
                 "x" =>50,
                 "y" =>50
              ),                 
             "lengths" => array (   //x,y轴长
                 "x" => 800,
                 "y" => 600
              ),                
             "avr" => array (   //x,y 轴刻度数量
                 "x" =>20,
                 "y" =>10
              ), 
             "max" => array (   //x,y 最大值
                 "x" =>10000,
                 "y" =>1000
              ),         
             "width" => 1,  //柱状图宽 
             "color" => array (   //x,y 轴刻度数量
                 "shapcolor" =>array (0,0,0),  //柱形图颜色
                 "xline" =>array (0,0,0),    //x轴颜色
                 "xlineK" =>array (0,0,0),    //x轴标记
                 "xlineW" =>array (0,0,0),    //x轴文字
                 "yline" =>array (0,0,0),    //y轴颜色
                 "ylineK" =>array (0,0,0),    //y轴标记
                 "ylineW" =>array (0,0,0),    //x轴文字
                 "background" =>array (255,255,255)  //背景色
              ),
              
              "words" => array (   //文字与xy轴距离
                 "down" =>40,  
                 "left" =>40
              ),       
          ) ;
       
       protected function drawArr($arr){      //取数组直画柱形图
             if ($this->stu==FALSE){
                   $this->prepare();
             }
             $shape1 = $this->sets['color']['shapcolor'];
             $shape = imagecolorallocate($this->im,$shape1[0],$shape1[1],$shape1[2]);
             $x = $this->sets['position']['x'];
             $y = $this->sets['size']['height'] - $this->sets['position']['y'];     
             $h = $this->sets['max']['y']/$this->sets['lengths']['y'];
          $counts = count($arr);
          
          $xlineW1 = $this->sets['color']['xlineW'];
          $xlineW = imagecolorallocate($this->im,$xlineW1[0],$xlineW1[1],$xlineW1[2]);
          for ($i=0;$i<$counts;$i++){  //循环绘画出柱状图
                 $x1 = $arr[$i][0]+$x;
                 
                 if(($arr[$i][1]/$h)<=$this->sets['lengths']['y']){
                 imagefilledrectangle($this->im,$x1,$y,($x1+$this->sets['width']),($y-$arr[$i][1]/$h),$shape);   
                 }
                 else {
                      $arr1 = $this->sets['lengths']['y'];
                      imagefilledrectangle($this->im,$x1,$y,($x1+$this->sets['width']),($y-$arr1),$shape);
                     // imagestring($this->im,2,$x1,$y-$num,$arr[$i][1],$ylineW); //越界写上值
                      imagestringup($this->im,2,$x1,($y-$arr1),$arr[$i][1],$xlineW);  //数值
                 }
              
          }             
       }
       
       protected function done($address,$name){  //输出 
            imagepng($this->im,$address."/".$name.".png");
          imagedestroy($this->im);
          return $address."/".$name.".png";     
       }
       
       private function prepare(){      //绘图准备
          $x = $this->sets['position']['x'];
          $y = $this->sets['size']['height']-$this->sets['position']['y'];
          $this->im = imagecreate($this->sets['size']['width'],$this->sets['size']['height']);    
          $black1 = $this->sets['color']['background'];    
          $black = imagecolorallocate($this->im,$black1[0],$black1[1],$black1[2]);  
          $xline1 = $shape1 = $this->sets['color']['xline'];
          $xline = imagecolorallocate($this->im, $xline1[0],$xline1[1],$xline1[2]);
          $yline1 = $shape1 = $this->sets['color']['yline'];
          $yline = imagecolorallocate($this->im,$yline1[0],$yline1[1],$yline1[2]);
          $yellow = imagecolorallocate($this->im,255,255,0);         
          imageline($this->im,$x,$y,($x+$this->sets['lengths']['x']),$y,$xline);  //画x轴         
          imageline($this->im,$x,$y,$x,($y-$this->sets['lengths']['y']),$yline);  //画y轴  
          $this->drawY();
          $this->drawX();
          $this->stu=TRUE;     
       }
       
       private function drawY(){  //循环画y轴刻度 
             $kedu = (int)($this->sets['lengths']['y']/$this->sets['avr']['y']);
             $kedu1 = (int)($this->sets['max']['y']/$this->sets['avr']['y']);
             $ylineW1 = $this->sets['color']['ylineW'];
             $ylineW = imagecolorallocate($this->im,$ylineW1[0],$ylineW1[1],$ylineW1[2]);
             $ylineK1 = $this->sets['color']['ylineK'];
             $ylineK = imagecolorallocate($this->im, $ylineK1[0],$ylineK1[1],$ylineK1[2]);
             $x = $this->sets['position']['x'];
             $x1 = $this->sets['position']['x']-$this->sets['words']['left'];
             $y = $this->sets['size']['height']-$this->sets['position']['y'];
          for ($i=0;$i <= $this->sets['avr']['y'];$i++) {
             $num =$i*$kedu;    
             imagestring($this->im,2,$x1,$y-$num,$kedu1*$i,$ylineW);     //数值
             imageline($this->im,$x+10,$y-$num,$x,$y-$num,$ylineK);  //画标号       
          }
       }
     
       private function drawX(){  //循环画x轴刻度 
             $kedu = (int)($this->sets['lengths']['x']/$this->sets['avr']['x']);
          $kedu1 = (int)($this->sets['max']['x']/$this->sets['avr']['x']);
          $xlineW1 = $this->sets['color']['xlineW'];
             $xlineW = imagecolorallocate($this->im,$xlineW1[0],$xlineW1[1],$xlineW1[2]); 
             $xlineK1 = $this->sets['color']['xlineK'];
             $xlineK = imagecolorallocate($this->im, $xlineK1[0],$xlineK1[1],$xlineK1[2]);
             $x = $this->sets['position']['x'];
             $y = $this->sets['size']['height']- $this->sets['position']['y'];
             $y1 = $y + $this->sets['words']['down'];
             
          for ($i=1;$i <= $this->sets['avr']['x'];$i++) {
             $num =$i*$kedu;    
             imagestringup($this->im,2,$num+$x,$y1,$kedu1*$i,$xlineW);  //数值
             imageline($this->im,$x+$num,$y-10,$x+$num,$y,$xlineK);  //画标号          
          }
       } 
     }
    ?>

    <?php
     include 'Draw.php';
     class Idraw extends Draw{    
         private $arr = array ();
         private $arr1 = array ();
         private $arrT = array ();
          
        function putIn($arr){
            $counts = count($arr);
            $avr = $this->sets['max']['x']/($this->sets['lengths']['x']/$this->sets['width']);    
            for ($i=0;$i < $counts;$i++){           
               $mod = (int)($arr[$i][0]/$avr);                 
               if(!in_array($mod, $this->arr)){           
                   array_push($this->arr, $mod);
                   array_push($this->arr1, $arr[$i][1]);
               } 
               else {  
                      $key = array_search($mod,$this->arr);
                      $this->arr1[$key] = ($this->arr1[$key]+$arr[$i][1]);     
               }                 
            }
            //echo $this->arr1[0];
        }
         
        function getOut($address, $name){
            $this->arrT();
            $this->drawArr($this->arrT);
            return $this->done($address, $name);
        }
        
        private function  arrT(){
            $counts = count($this->arr);
            for ($i=0;$i < $counts;$i++){
                $arr = array ($this->arr[$i],$this->arr1[$i]);
                array_push($this->arrT, $arr);          
            }     
        }
         
     }

    测试用代码:

    <?php

      include 'Idraw.php';
      
      $arr = array (
       array(130,100),
       array(131,100),
       array(465,105),
       array(9997,200),
       array(5000,600),
       array(7000,200),
       array(6000,600),
       array(8000,400),
       
    );

     $arr1 = array (
       array(130,100),
       array(8001,900),
       array(8500,1800),
     );

      
     $draw1 = new Idraw();
     $draw1->putIn($arr);
     $draw1->putIn($arr1);
     $draw1->getOut("image","yes1");
     echo "<img src='image/yes1.png'/>";
     ///home/m/Dev/cvs/ImRoBot5/spider2/newshive/admin
     
     
     
        ?>

  • 相关阅读:
    java枚举enum
    冒泡排序、选择排序、插入排序、二分法排序、快速排序、二叉树排序、堆排序总结
    Django-tinymce富文本的使用
    Redis-基本操作总结
    git-总结大全
    css-总结
    html-table布局
    html表单示例
    html总结
    python-浅拷贝、深拷贝实例以及讲解
  • 原文地址:https://www.cnblogs.com/wangkangluo1/p/2079166.html
Copyright © 2020-2023  润新知