首頁 > 後端開發 > php教程 > 可顯示多種圖形報表的php圖片類

可顯示多種圖形報表的php圖片類

WBOY
發布: 2016-07-25 09:11:07
原創
1058 人瀏覽過
  1. class ImageReport{
  2. var $X;//圖片大小X軸
  3. var $Y;//圖片大小Y軸
  4. var $R;//背影色R值
  5. var $G;//...G.
  6. var $B;//...B.
  7. var $ TRANSPARENT;//是否透明1或0
  8. var $IMAGE;//圖片物件
  9. //-------------------
  10. var $ ARRAYSPLIT;//指定用於分隔數值的符號
  11. var $ITEMARRAY;//數值
  12. var $REPORTTYPE;//圖表類型,1為豎柱形2為橫柱形3為折線形
  13. var $BORDER;//距離
  14. //-------------------
  15. var $FONTSIZE;//字體大小
  16. var $FONTCOLOR;/ /字體顏色
  17. var $numX = 1;//X軸起始刻度值
  18. var $stepX = 1;//X軸每一個刻度間隔值
  19. //- -------參數設定函數
  20. function setImage($SizeX,$SizeY,$R,$G,$B,$Transparent){
  21. $this->X=$SizeX;
  22. $this->Y=$SizeY;
  23. $this->R=$R;
  24. $this->G=$G;
  25. $this->B=$B;
  26. $this ->TRANSPARENT=$Transparent;
  27. }
  28. function setItem($ArraySplit,$ItemArray,$ReportType,$Border){
  29. $this->ARRAYSPLIT=$ArraySplit;
  30. $this-this>ITEMARRAY =$ItemArray;
  31. $this->REPORTTYPE=$ReportType;
  32. $this->BORDER=$Border;
  33. }
  34. function setFont($FontSize){
  35. $this->FONTSI =$FontSize;
  36. }
  37. //X軸刻度值設定
  38. function setX($numX = 1, $stepX = 1){
  39. $this->numX = $numX;
  40. $this->stepX = $stepX;
  41. }
  42. //----------------主體
  43. function PrintReport(){
  44. //建立畫布大小
  45. $this->IMAGE=ImageCreate($this->X,$this->Y);
  46. //設定畫布背景色
  47. $background=ImageColorAllocate($this->IMAGE,$ this->R,$this->G,$this->B);
  48. if($this->TRANSPARENT=="1"){
  49. //背影透明
  50. Imagecolortransparent($this- >IMAGE,$background);
  51. }else{
  52. //如不要透明時可填充背景色
  53. ImageFilledRectangle($this->IMAGE,0,0,$this->X,$this- >Y,$background);
  54. }
  55. //參數字體文字小及顏色
  56. $this->FONTCOLOR=ImageColorAllocate($this->IMAGE,255-$this->R,255-$ this->G,255-$this->B);
  57. Switch ($this->REPORTTYPE){
  58. case "0":
  59. break;
  60. case "1":
  61. $this->imageColumnS();
  62. break;
  63. case "2":
  64. $this->imageColumnH();
  65. break;
  66. case "3":
  67. $this $this ->imageLine();
  68. break;
  69. case "4":
  70. $this->imageCircle();
  71. break;
  72. }
  73. $this->printXY();
  74. $this->printAll();
  75. }
  76. //-----------印出XY座標軸
  77. function printXY(){
  78. $rulerY = $ rulerX = "";
  79. //畫XY座標軸*/
  80. $color=ImageColorAllocate($this->IMAGE,255-$this->R,255-$this->G,255-$this ->B);
  81. $xx=$this->X/10;
  82. $yy=$this->Y-$this->Y/10;
  83. ImageLine($this->IMAGE, $this->BORDER,$this->BORDER,$this->BORDER,$this->Y-$this->BORDER,$color);//X軸
  84. ImageLine($this->IMAGE,$ this->BORDER,$this->Y-$this->BORDER,$this->X-$this->BORDER,$this->Y-$this->BORDER,$color);//y軸
  85. imagestring($this->IMAGE, $this->FONTSIZE, $this->BORDER-2, $this->Y-$this->BORDER+5, "0", $color);
  86. / /Y軸上刻度
  87. $rulerY=$this->Y-$this->BORDER;
  88. $i = 0;
  89. while($rulerY>$this->BORDER*2){
  90. $rulerY=$rulerY-$this->BORDER;
  91. ImageLine($this->IMAGE,$this->BORDER,$rulerY,$this->BORDER-2,$rulerY,$color);
  92. if($this->REPORTTYPE == 2){//橫柱圖
  93. imagestring($this->IMAGE, $this->FONTSIZE, $this->BORDER-10, $rulerY-2 -$this->BORDER*($i+.5), $this->numX, $color);
  94. $this->numX += $this->stepX;
  95. }
  96. $i++;
  97. }
  98. //X軸上刻度
  99. $rulerX=$rulerX+$this->BORDER;
  100. $i = 0;
  101. while($rulerXX- $this->BORDER*2)){
  102. $rulerX=$rulerX+$this->BORDER;
  103. //ImageLine($this->IMAGE,$this->BORDER,10,$this->BORDER +10,10,$color);
  104. ImageLine($this->IMAGE,$rulerX,$this->Y-$this->BORDER,$rulerX,$this->Y-$this->BORDER+ 2,$color);
  105. //刻度值
  106. if($this->REPORTTYPE == 1){//豎柱圖
  107. imagestring($this->IMAGE, $this- >FONTSIZE, $rulerX-2+$this->BORDER*($i+.5), $this->Y-$this->BORDER+5, $this->numX, $color);
  108. $this ->numX += $this->stepX;
  109. }else if($this->REPORTTYPE == 3){//折線圖
  110. imagestring($this->IMAGE, $this->FONTSIZE, $rulerX-2, $this->Y-$ this->BORDER +5, $this->numX, $color);
  111. $this->numX += $this->stepX;
  112. }
  113. $i++;
  114. }
  115. }
  116. //---------------- 垂直圖
  117. function imageColumnS(){
  118. $item_array=Split($this- >ARRAYSPLIT,$this- >ITEMARRAY);
  119. $num=Count($item_array);
  120. $item_max=0;
  121. for ($i=0;$i $item_max= Max($item_max,$item_array[$i]);
  122. }
  123. $xx=$這->邊框*2;
  124. //畫長條圖
  125. for ($i=0; $i srand((double)microtime()*1000000);
  126. if($這個->R!=255 && $這個->G!=255 && $這個->B! $this->B,200);
  127. }其他{
  128. $R=蘭特(50,200);
  129. $G=蘭特(50,200);
  130. $B=蘭特(50,200) ;
  131. }
  132. $color=ImageColorAllocate($this->IMAGE,$R,$G,$B);
  133. // 柱形高度
  134. $height=($this->Y- $this->BORDER)-($this->Y-$this->BORDER*2)*($ item_array[$i]/$item_max);
  135. ImageFilledRectangle($this->IMAGE,$xx,$ height,$xx+$this->BORDER,$this->Y-$this->BORDER,$color);
  136. ImageString($this->IMAGE,$this->FONTSIZE,$xx,$height-$ this->BORDER,$item_array[$i],$this->FONTCOLOR);
  137. //用於間隔
  138. $xx=$xx+$this->BORDER*2;
  139. }
  140. }
  141. //------------橫柱形圖
  142. function imageColumnH(){
  143. $item_array=Split($this->ARRAYSPLIT ,$this->ITEMARRAY) ;
  144. $num=Count($item_array);
  145. $item_max=0;
  146. for ($i=0;$i $item_max=Max($ item_max,$item_array[$i]);
  147. }
  148. $yy=$這個->Y-$這個->BORDER*2;
  149. //畫長條圖
  150. for ($i =0;$i srand((double)microtime()*1000000);
  151. if($這個->R! =255 && $這個->G! =255 && $這個->B! =255){
  152. $R=Rand($this->R,200);
  153. $G=Rand($this->G,200);
  154. $B=Rand($this-> B,200);
  155. }其他{
  156. $R=蘭特(50,200);
  157. $G=蘭特(50,200);
  158. $B=蘭特(50,200);
  159. }
  160. $color=ImageColorAllocate($this->IMAGE,$R,$G,$B);
  161. // 柱形長度
  162. $leight=($this->X-$this-> BORDER*2)*($item_array[$i]/$item_max);
  163. $leight = $leight 邊框? $this->BORDER : $leight;
  164. ImageFilledRectangle($this->IMAGE,$this->BORDER,$yy-$this->BORDER,$leight,$yy,$color);
  165. ImageString( $this->IMAGE,$this->FONTSIZE,$leight+2,$yy-$this->BORDER,$item_array[$i],$this->FONTCOLOR);
  166. //用於間隔
  167. $yy=$yy-$this->BORDER*2;
  168. }
  169. }
  170. //----------------折線圖
  171. function imageLine(){
  172. $item_array=Split($this->; ARRAYSPLIT,$this->ITEMARRAY);
  173. $num=Count($item_array);
  174. $item_max=0;
  175. for ($i=0;$i $item_max=Max($item_max,$item_array[$i]);
  176. }
  177. $xx=$這->邊框;
  178. //畫長條圖
  179. for ($i=0;$i srand((double)microtime()*1000000);
  180. if( $這個->R! =Rand($this->G,200);
  181. $B=Rand($this->B,200);
  182. }其他{
  183. $R=蘭特(50,200);
  184. $G=蘭特(50,200);
  185. $B=蘭特(50,200);
  186. }
  187. $color=ImageColorAllocate($this->IMAGE,$R,$G,$B);
  188. // 柱形高度
  189. $height_now=($this->Y-$this->BORDER)-($this->Y-$this->BORDER*2)*($ item_array[$i] /$item_max);
  190. if($i!="0")
  191. ImageLine($this->IMAGE,$xx-$this->BORDER,$height_next,$xx,$height_now,$color) ;
  192. ImageString($this->IMAGE,$this->FONTSIZE,$xx+2,$height_now-$this->BORDER/2,$item_array[$i],$this-> ;字體顏色);
  193. $height_next=$height_now;
  194. //用於間隔
  195. $xx=$xx+$this->BORDER;
  196. }
  197. }
  198. //--- -------------餅狀圖
  199. function imageCircle(){
  200. $total = 0;
  201. $ item_array=Split($this->ARRAYSPLIT,$this-> ITEMARRAY);
  202. $num=Count($item_array);
  203. $item_max=0;
  204. for ($i=0;$i $item_max=Max ($item_max,$item_array[$i]);
  205. $total += $item_array[$我];
  206. }
  207. $yy=$this->Y-$this->BORDER*2;
  208. //畫餅狀圖的陰影部分
  209. $e=0;
  210. for ($i=0;$i srand((double )microtime()*1000000);
  211. if($this->R!=255 && $this->G!=255 && $this->B!=255){
  212. $R=蘭德( $這個->R,200);
  213. $G=Rand($this->G,200);
  214. $B=Rand($this->B,200);
  215. }其他{
  216. $R=蘭特(50,200);
  217. $G=蘭特(50,200);
  218. $B=蘭特(50,200);
  219. }
  220. $s=$e;
  221. $leight=$item_array[$i]/$total*360;
  222. $e=$s+$leight;
  223. $color=ImageColorAllocate( $this->IMAGE,$R,$G,$B);
  224. $colorarray[$i]=$color;
  225. //畫圓
  226. for ($j = 90; $j > 70; $j--) imagefilledarc($this->IMAGE, 110, $j, 200, 100, $s, $ e, $顏色,IMG_ARC_PIE);
  227. //imagefilledarc($this->IMAGE, 110, 70, 200, 100, $s, $e, $color, IMG_ARC_PIE);
  228. //ImageFilledRectangle($this->IMAGE,$this->BORDER,$yy-$this->BORDER,$leight,$yy,$color);
  229. //ImageString($this->IMAGE,$this->FONTSIZE,$leight+2,$yy-$this->BORDER,$item_array[$i],$this->FONTCOLOR) ;
  230. //用於間隔
  231. $yy=$yy-$this->BORDER*2;
  232. }
  233. //畫餅狀圖的表面部分
  234. $e=0;
  235. for ($i=0;$i srand((double)microtime()*1000000);
  236. if($this->R!=255 && $this->G!=255 && $this->B!=255){
  237. $R =蘭特($這個->R,200);
  238. $G=Rand($this->G,200);
  239. $B=Rand($this->B,200);
  240. }其他{
  241. $R=蘭特(50,200);
  242. $G=蘭特(50,200);
  243. $B=蘭特(50,200);
  244. }
  245. $s=$e;
  246. $leight=$item_array[$i]/$total*360;
  247. $e=$s+$leight;
  248. //$color= $colorarray[$i];
  249. $color=ImageColorAllocate($this->IMAGE,$R,$G,$B);
  250. //畫圓
  251. //for ($j = 90; $j > 70; $j--) imagefilledarc($this->IMAGE, 110, $j, 200, 100, $s , $ e, $color, IMG_ARC_PIE);
  252. imagefilledarc($this->IMAGE, 110, 70, 200, 100, $s, $e, $color, IMG_ARC_PIE);
  253. }
  254. }
  255. //----------------列印完成圖形
  256. function printAll(){
  257. ImagePNG($this->IMAGE ) ;
  258. ImageDestroy($this->IMAGE);
  259. }
  260. //-------------- 除錯
  261. function debug(){
  262. echo "X:".$this->X."Y:".$this->Y;
  263. echo "
    BORDER:".$this->BORDER;
  264. $item_array=split($this->ARRAYSPLIT,$this->ITEMARRAY);
  265. $num=Count($item_array);
  266. echo "
    數值:".$num."
    數值:";
  267. for ($i=0;$i echo "
    ".$item_array[$i];
  268. }
  269. }
  270. }
  271. //$report->debug();//調式之用
  272. /*
  273. Header( "Content-type:image/png" ) ;
  274. $report=new ImageReport;
  275. $report->setImage(600,500,255,255,255,1);//參數(長,高,背影顏色R,G,B,是否透明1或0)
  276. $ temparray="0,260,400,124,48,720,122,440,475";//數值,用指定符號隔開
  277. $report->setItem(',',$temparray,3,23);//參數(次數值的值指定數字,樣式1為垂直柱圖2為橫柱圖3為折線圖4為圓餅圖,距離)
  278. $report->setFont(1);//字體大小1-10
  279. / /$report - >setX(1,1);//設定X軸初始值(初始初始值=1,初始初始值=1)
  280. $report->PrintReport();
  281. * /
  282. ?>
複製程式碼


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板