国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php如何實現報表類

2024-05-04 21:52:41
字體:
來源:轉載
供稿:網友
 
 
 
 

原代碼如下:

<?php

class ImageReport
{
   var $Image;
//圖片對像


   var $ImageName;
//報表名稱

   var $ImageType;
//報表類型

   
   var $ImageWidth;
//圖片寬

   var $ImageHeight;
//圖片高

   

   var $BgImage;
//背景圖片

   var $BgR;
//背景顏色R值

   var $BgG;
//背景顏色G值

   var $BgB;
//背景顏色B值


   var $FontSize;
//字體大小

   var $FontColor;
//字體顏色


   var $ItemArray;
//數據數組

   var $ItemInterval;
//兩項間的間隔

   var $XName;
//x軸的名稱

   var $YName;
//y軸的名稱

   var $isScale;
//是否顯示刻度值

   var $margin;
//邊距


   
//字體

   var $font = 'arial.ttf';
   
    function ImageReport()
    {
        return ;
    }

   
//設置圖片基本信息

   function setImage($ImageName,$ImageType,$ImageWidth=500,$ImageHeight=400)
   {
        $this->ImageName =$ImageName;
     $this->ImageType =$ImageType;
     $this->ImageWidth =$ImageWidth;
     $this->ImageHeight=$ImageHeight;
   }
   
   
//設置坐標

   function setItem($ItemArray,$ItemInterval="20",$XName="",$YName="",$isScale=true)
   {
        $this->ItemArray =$ItemArray;
     $this->ItemInterval =$ItemInterval;
     $this->XName =$XName;
     $this->YName =$YName;
     $this->isScale =$isScale;
   }
   
   
//設置圖片背景

   function setBg($BgR="235",$BgG="255",$BgB="255",$BgImage="")
   {
        $this->BgR =$BgR;
     $this->BgG =$BgG;
     $this->BgB =$BgG;
     $this->BgImage =$BgImage;
   }
   
   
//設置字體

   function setFont($FontSize)
   {
     $this->FontSize =$FontSize;
     $this->margin =$FontSize*2+5;
     
//$this->FontColor =$FontColor;

   }

   
//畫主體

   function drawReport()
   {
     
//建立畫布

     $this->Image=imagecreatetruecolor($this->ImageWidth,$this->ImageHeight);
     
//填充背景色

     $background=ImageColorAllocate($this->Image,$this->BgR,$this->BgG,$this->BgB);
          
     
//畫背景顏色

     ImageFilledRectangle($this->Image,0,0,$this->ImageWidth,$this->ImageHeight,$background);

     
//畫背景圖片

     if(!empty($this->BgImage))
     {
        $this->drawLogo();
     }
      
//跟距參數,畫不同的圖片

     switch($this->ImageType)
     {
         case "1":
           $this->drawColumn();
           break;
         case "2":
           $this->drawCurve();
           break;
         case "3":
           $this->drawCircle();
           break;
         default:
             break;
     }

   }
   
   
//畫柱狀圖

   function drawColumn()
   {
     
//調用畫標題

     $this->drawTitle();
     
//調用畫坐標

     $this->drawXY();

     
//獲取數組的量

     $num=count($this->ItemArray);
     
//獲取數組中的最大值

     
//最大值

     $max=$this->getArrayMax($this->ItemArray);
    

     for($i=0;$i<count($this->ItemArray);$i++)
     {
        
//隨機顏色

        srand((double)microtime()*1000000);
        $R=Rand(50,200);
        $G=Rand(50,200);
        $B=Rand(50,200);
      
        $color=ImageColorAllocate($this->Image,$R,$G,$B);
        
        $yColor=ImageColorAllocate($this->Image,$R-10,$G-10,$B-10);
        
//柱體高度

        
        $height=(($this->ImageHeight-$this->margin*2-$this->FontSize-5)*$this->ItemArray[$i]["value"])/$max;

        
//畫柱體

        ImageFilledRectangle($this->Image,$this->margin+$this->ItemInterval*(2*$i+1),$this->ImageHeight-$height+$this->FontSize-5-$this->margin,$this->margin+$this->ItemInterval*(2*$i+2),$this->ImageHeight-$this->margin,$color);
        
//畫柱體上的陰影

        imagefilledarc($this->Image, $this->margin+$this->ItemInterval*(2*$i+3/2), $this->ImageHeight-$height+$this->FontSize-5-$this->margin,$this->ItemInterval, $this->margin/2, 0, 360, $yColor, IMG_ARC_PIE);
        
        
//寫柱體上面的數值

        imagestring($this->Image,$this->FontSize,$this->margin+$this->ItemInterval*(2*$i+1),$this->ImageHeight-$height+$this->FontSize-5-$this->margin-$this->FontSize-10,$this->ItemArray[$i]["value"],$color);
        
//顏色

        $color=ImageColorAllocate($this->Image,255-$this->BgR,255-$this->BgG,255-$this->BgB);
    

        
//寫柱體的鍵值

        imagettftext($this->Image,7,0,$this->margin+$this->ItemInterval*(2*$i+1)-3,$this->ImageHeight-$this->margin+$this->FontSize+2,$color,$this->font,$this->ItemArray[$i]["key"]);
    
     }
     
//調用畫LOGO

    
// $this->drawLogo();

    

   }
   
//畫曲線圖

   function drawCurve()
   {
     
//調用畫標題

     $this->drawTitle();
     
//調用畫坐標

     $this->drawXY(true);
     
     
//最大值

     $max=$this->getArrayMax($this->ItemArray);
     
     
//顏色

     $color=ImageColorAllocate($this->Image,255-$this->BgR,255-$this->BgG,255-$this->BgB);
     
//背景刻度的顏色

     $bgSColor=ImageColorAllocate($this->Image,$this->BgR-10,$this->BgG-10,$this->BgB-10);
     
//線條顏色


     $linkColor=ImageColorAllocate($this->Image,51,51,153);
  

     
//畫右X軸

     ImageLine($this->Image,$this->margin,$this->margin+$this->FontSize+5,$this->ImageWidth-$this->margin,$this->margin+$this->FontSize+5,$color);
     
//畫右Y軸

     ImageLine($this->Image,$this->ImageWidth-$this->margin,$this->margin+$this->FontSize+5,$this->ImageWidth-$this->margin,$this->ImageHeight-$this->margin+2,$color);
     
     $tdCount=count($this->ItemArray);
     $tdWidth=($this->ImageWidth-$this->margin*2)/$tdCount;
     
    
     for($i=0;$i<$tdCount-1;$i++)
    {
        
//線條高度

        $startHeight=(($this->ImageHeight-$this->margin*2-$this->FontSize-5)*$this->ItemArray[$i]["value"])/$max;
        
//結束線線條高度

        $endHeight =(($this->ImageHeight-$this->margin*2-$this->FontSize-5)*$this->ItemArray[$i+1]["value"])/$max;
        
//畫背景X軸刻度

        ImageLine($this->Image,($i+1)*$tdWidth+$this->margin,$this->margin+$this->FontSize+6,($i+1)*$tdWidth+$this->margin,$this->ImageHeight-$this->margin,$bgSColor);
        
//畫線條

        ImageLine($this->Image,$i*$tdWidth+$this->margin,$this->ImageHeight-$startHeight+$this->FontSize-5-$this->margin,($i+1)*$tdWidth+$this->margin,$this->ImageHeight-$endHeight+$this->FontSize-5-$this->margin,$linkColor);
     }

     
//畫刻度的值

     for($i=0;$i<$tdCount;$i++)
     {
        
//顯示值

        
//ImageString($this->Image,5,$i*$tdWidth+$this->margin,$this->ImageHeight-$this->margin+1,$this->ItemArray[$i]["key"],$color);

        
//畫標題的字

        imagettftext($this->Image,7,0,$i*$tdWidth+$this->margin,$this->ImageHeight-$this->margin+1+10,$color,$this->font,$this->ItemArray[$i]["key"]);
     }

   }

   
//畫餅圖

   function drawCircle()
   {
      
//一個扇區的角度

      $degree=0;
      
//開始的角度

      $e=0;
      
//用于保存顏色的數組

      $colorArray;
      
//用于保存開始角度的數組

      $startDegree;
      
//用于保存結束角度的數組

      $endDegree;
      
      
//扇形的寬

      $width=($this->ImageWidth-$this->margin*2)-200;

         
//獲取數組的量

      $num=count($this->ItemArray);
      
//調用畫標題

      $this->drawTitle();
      
//各項之和

      $total=0;
      
      
//計算各項之和

      for($i=0;$i<$num;$i++)
      {
        $total+=$this->ItemArray[$i]["value"];
      }

      
//保扇區的各項值保存到數組

      for($i=0;$i<count($this->ItemArray);$i++)
      {
        
//隨機顏色

        srand((double)microtime()*1000000);

        $R=Rand(50,200);
        $G=Rand(50,200);
        $B=Rand(50,200);
 
        
//把顏色保存進數組

        $colorArray[$i]["R"]=$R;
        $colorArray[$i]["G"]=$G;
        $colorArray[$i]["B"]=$B;
        
        
//計算一個區塊的角度

        
//echo $this->ItemArray[$i]["value"];

        
//exit;

        $degree=$this->ItemArray[$i]["value"]/$total*360;
        $startDegree[$i]=$e;
        $endDegree[$i]=$e+$degree;
        $e+=$degree;
      }

       $j=$width/4+$this->margin+35;
       $mJ=$j-15;
       
//畫陰影

       for ($j;$j >$mJ; $j--)
       {
         for($i=0;$i<$num;$i++)
         {
          $color=ImageColorAllocate($this->Image,$colorArray[$i]["R"]-10,$colorArray[$i]["G"]-10,$colorArray[$i]["B"]-10);
          imagefilledarc($this->Image, $width/2+$this->margin, $j, $width, $width/2, $startDegree[$i], $endDegree[$i], $color, IMG_ARC_PIE);
         }
       }
       
//畫區面和右邊的標識字

      for($i=0;$i<$num;$i++)
      {
        
//獲取顏色

        $color=ImageColorAllocate($this->Image,$colorArray[$i]["R"],$colorArray[$i]["G"],$colorArray[$i]["B"]);
        
        
//畫區面

        imagefilledarc($this->Image, $width/2+$this->margin, $width/4+$this->margin+20, $width, $width/2, $startDegree[$i], $endDegree[$i], $color, IMG_ARC_PIE);
        

        
//說明方塊的上邊距

        $top=$this->margin+20+$i*10;
        
//畫右邊的距形

        ImageFilledRectangle($this->Image,$width+$this->margin+20,$top+($i*10),$width+$this->margin+30,$top+($i*10)+10,$color);
        
//字體顏色為黑色

        $fontColor=ImageColorAllocate($this->Image,255-$this->BgR,255-$this->BgG,255-$this->BgB);
        
        
//畫右邊的說明文字

        $rightStr=$this->ItemArray[$i]["key"].":".$this->ItemArray[$i]["value"]."(".round($this->ItemArray[$i]["value"]*100/$total,2)."%)";
        
//ImageString($this->Image,10,$width+$this->margin+35,$top+($i*10)-3,$rightStr,$fontColor);

        imagettftext($this->Image, 9,0,$width+$this->margin+35,$top+($i*10)+8,$fontColor,$this->font,$rightStr);
        
      }
      
//清空各數組

      unset($colorArray,$startDegree,$endDegree);

   }

   
//顯示圖片

   function showImage($ImageSavePath="")
   {
      
// echo $ImageSavePath;

       
//exit;

       if(empty($ImageSavePath))
       {
         ImagePNG($this->Image);
       }
       else
       {
         ImagePNG($this->Image,$ImageSavePath);
       }
      
       ImageDestroy($this->Image);
   }

   
//畫標題

   function drawTitle()
   {
        
//顏色

     $color=ImageColorAllocate($this->Image,255-$this->BgR,255-$this->BgG,255-$this->BgB);
  
     
//陰影色

     $black = imagecolorallocate($this->Image, 0, 0, 0);
     
     
//計算標題字體的寬度

     $fontWidth=strlen($this->ImageName)*($this->FontSize)-strlen($this->ImageName)*2;

     
//畫標題字的陰影

     imagettftext($this->Image, $this->FontSize+3,0,$this->ImageWidth/2-$fontWidth/2+1,$this->FontSize+11,$black,$this->font,$this->ImageName);
     
//畫標題的字

     imagettftext($this->Image, $this->FontSize+3,0,$this->ImageWidth/2-$fontWidth/2,$this->FontSize+10,$color,$this->font,$this->ImageName);
     
     
//畫標題下面的線條

     imageline($this->Image,$this->ImageWidth/2-$fontWidth/2-20,$this->FontSize+12,$this->ImageWidth/2-$fontWidth/2+1+$fontWidth+20,$this->FontSize+12,$color);
   }
   
   
//畫LOGO

   function drawLogo()
   {

      list($w,$h,$type) = getimagesize($this->BgImage);
      
      $source;
      switch($type)
      {
          
//圖片是GIF

         case 1:
          $source=imagecreatefromgif($this->BgImage);
          break;
          
//圖片是jPG

         case 2:
          $source = imagecreatefromjpeg($this->BgImage);
          break;
          
//圖片是PNG

         case 3:
          $source = imagecreatefrompng($this->BgImage);
      }
  
       imagecopymerge($this->Image,$source,0,0,0,0,$w,$h,20);
   }

   
//畫坐標

   function drawXY($bgScale=false)
   {
        
//最大值

     $max=$this->getArrayMax($this->ItemArray);
     
     
//顏色

     $color=ImageColorAllocate($this->Image,255-$this->BgR,255-$this->BgG,255-$this->BgB);
     
//背景刻度的顏色

     $bgSColor=ImageColorAllocate($this->Image,$this->BgR-10,$this->BgG-10,$this->BgB-10);

     
//畫X軸

     ImageLine($this->Image,$this->margin,$this->ImageHeight-$this->margin+2,$this->ImageWidth-$this->margin,$this->ImageHeight-$this->margin+2,$color);
     
//畫Y軸

     ImageLine($this->Image,$this->margin,$this->margin+$this->FontSize+5,$this->margin,$this->ImageHeight-$this->margin+2,$color);
       
          
     
//陰影色

     $black = imagecolorallocate($this->Image, 0, 0, 0);
     
      
//畫Y軸字的陰影

     imagettftext($this->Image, $this->FontSize, 90, $this->FontSize+2, intval($this->ImageHeight/2)-1, $black, $this->font, $this->YName);
     
//畫Y軸的字

     imagettftext($this->Image, $this->FontSize, 90, $this->FontSize+2, intval($this->ImageHeight/2), $color, $this->font, $this->YName);
    
      
//畫X軸字的陰影

     imagettftext($this->Image, $this->FontSize,0,intval($this->ImageWidth/2)-1,$this->ImageHeight-$this->FontSize+5,$black,$this->font,$this->XName);
     
//畫X軸的字

     imagettftext($this->Image, $this->FontSize,0,intval($this->ImageWidth/2),$this->ImageHeight-$this->FontSize+5,$color,$this->font,$this->XName);
    
      
     
//畫0點坐標

     imagettftext($this->Image,8,90,$this->margin/2+$this->FontSize,$this->ImageHeight-$this->margin,$color,$this->font,"0");
     
//畫最大值數據

    
// imagettftext($this->Image, $this->FontSize,90,$this->margin/2+7,$this->margin+20,$black,$font,$max);

     
     
//畫內容的實際高度

     $height=$this->ImageHeight-$this->margin*2-($this->FontSize+5);
     
//每一刻度的值

     $scale=intval($height/10);
    

     for($i=0;$i<10;$i++)
     {
       
//畫刻度,如果設置了背景刻度

       if($bgScale)
       {
         ImageLine($this->Image,$this->margin+1,$this->margin+$i*$scale+$this->FontSize+5+$i,$this->ImageWidth-$this->margin,$this->margin+$i*$scale+$this->FontSize+5+$i,$bgSColor);
       }
       else
       {
         ImageLine($this->Image,$this->margin-2,$this->margin+$i*$scale+$this->FontSize+5+$i,$this->margin,$this->margin+$i*$scale+$this->FontSize+5+$i,$color);
       }
       
//畫刻度上的值

       if($this->isScale)
       {
        $num=round($max-$max/10*$i,1);
        imagettftext($this->Image, 7,90,$this->margin/2+$this->FontSize,$this->margin+$i*$scale+15+$this->FontSize,$color,$this->font,$num);
       }
     }
     
//如果未畫刻度值,則畫最大值

     if(!$this->isScale)
     {
        imagettftext($this->Image, 7,90,$this->margin/2+$this->FontSize,$this->margin+15+$this->FontSize,$color,$this->font,$max);
     }

   }
   
   
//獲取數組中的最大值

   function getArrayMax($Array)
   {
     $temp=0;
        for($i=0;$i<count($Array);$i++)
     {
         if($temp<$Array[$i]["value"])
         {
             $temp=$Array[$i]["value"];
         }
     }
     return $temp;
 
}

?>

調用方法

 

 

require_once ("ImageReport.inc.php");
     $report=new ImageReport;
     $report->setImage($title,$type,$imgW,$imgH);//參數(標題,類型(1(柱圖),2(曲線圖),3(餅圖)),圖片寬,圖片高)
     
    //這個我用數據庫查出來的,要改一下.格式是多維數據.key=>value
     $temparray=$this->db->GetArray("select report_key as 'key',report_value as value from cms_r_report_data where report_id=$id order by report_dataId ASC");

     $report->setItem($temparray,$interval,$XName,$YName,$isScale);//參數(數組,間隔,X軸文字,Y軸文字,背景圖片)
     $report->setBg($BgR,$BgG,$BgB,"logo.jpg");//參數(背景顏色)
     $report->setFont(10);//字體大小1-10
     $report->drawReport();
     $report->showImage("../images/report/".$id.".png");//參數為保存的路徑

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 墨玉县| 桐梓县| 金沙县| 邵东县| 营山县| 马鞍山市| 油尖旺区| 拉萨市| 平南县| 涞源县| 东兴市| 施秉县| 从江县| 鹤峰县| 荥经县| 颍上县| 宽城| 论坛| 婺源县| 新安县| 邵阳县| 电白县| 长岭县| 绥江县| 姜堰市| 长寿区| 罗平县| 张家口市| 马鞍山市| 图木舒克市| 锦屏县| 德州市| 蓝田县| 中江县| 五莲县| 胶南市| 凤庆县| 沧源| 化德县| 美姑县| 吴川市|