当前位置:Gxlcms > PHP教程 > 可显示多种图形报表的php图片类

可显示多种图形报表的php图片类

时间:2021-07-01 10:21:17 帮助过:4人阅读

  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->ITEMARRAY=$ItemArray;
  31. $this->REPORTTYPE=$ReportType;
  32. $this->BORDER=$Border;
  33. }
  34. function setFont($FontSize){
  35. $this->FONTSIZE=$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->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($rulerX<($this->X-$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<$num;$i++){
  122. $item_max=Max($item_max,$item_array[$i]);
  123. }
  124. $xx=$this->BORDER*2;
  125. //画柱形图
  126. for ($i=0;$i<$num;$i++){
  127. srand((double)microtime()*1000000);
  128. if($this->R!=255 && $this->G!=255 && $this->B!=255){
  129. $R=Rand($this->R,200);
  130. $G=Rand($this->G,200);
  131. $B=Rand($this->B,200);
  132. }else{
  133. $R=Rand(50,200);
  134. $G=Rand(50,200);
  135. $B=Rand(50,200);
  136. }
  137. $color=ImageColorAllocate($this->IMAGE,$R,$G,$B);
  138. //柱形高度
  139. $height=($this->Y-$this->BORDER)-($this->Y-$this->BORDER*2)*($item_array[$i]/$item_max);
  140. ImageFilledRectangle($this->IMAGE,$xx,$height,$xx+$this->BORDER,$this->Y-$this->BORDER,$color);
  141. ImageString($this->IMAGE,$this->FONTSIZE,$xx,$height-$this->BORDER,$item_array[$i],$this->FONTCOLOR);
  142. //用于间隔
  143. $xx=$xx+$this->BORDER*2;
  144. }
  145. }
  146. //-----------横柱形图
  147. function imageColumnH(){
  148. $item_array=Split($this->ARRAYSPLIT,$this->ITEMARRAY);
  149. $num=Count($item_array);
  150. $item_max=0;
  151. for ($i=0;$i<$num;$i++){
  152. $item_max=Max($item_max,$item_array[$i]);
  153. }
  154. $yy=$this->Y-$this->BORDER*2;
  155. //画柱形图
  156. for ($i=0;$i<$num;$i++){
  157. srand((double)microtime()*1000000);
  158. if($this->R!=255 && $this->G!=255 && $this->B!=255){
  159. $R=Rand($this->R,200);
  160. $G=Rand($this->G,200);
  161. $B=Rand($this->B,200);
  162. }else{
  163. $R=Rand(50,200);
  164. $G=Rand(50,200);
  165. $B=Rand(50,200);
  166. }
  167. $color=ImageColorAllocate($this->IMAGE,$R,$G,$B);
  168. //柱形长度
  169. $leight=($this->X-$this->BORDER*2)*($item_array[$i]/$item_max);
  170. $leight = $leight < $this->BORDER ? $this->BORDER : $leight;
  171. ImageFilledRectangle($this->IMAGE,$this->BORDER,$yy-$this->BORDER,$leight,$yy,$color);
  172. ImageString($this->IMAGE,$this->FONTSIZE,$leight+2,$yy-$this->BORDER,$item_array[$i],$this->FONTCOLOR);
  173. //用于间隔
  174. $yy=$yy-$this->BORDER*2;
  175. }
  176. }
  177. //--------------折线图
  178. function imageLine(){
  179. $item_array=Split($this->ARRAYSPLIT,$this->ITEMARRAY);
  180. $num=Count($item_array);
  181. $item_max=0;
  182. for ($i=0;$i<$num;$i++){
  183. $item_max=Max($item_max,$item_array[$i]);
  184. }
  185. $xx=$this->BORDER;
  186. //画柱形图
  187. for ($i=0;$i<$num;$i++){
  188. srand((double)microtime()*1000000);
  189. if($this->R!=255 && $this->G!=255 && $this->B!=255){
  190. $R=Rand($this->R,200);
  191. $G=Rand($this->G,200);
  192. $B=Rand($this->B,200);
  193. }else{
  194. $R=Rand(50,200);
  195. $G=Rand(50,200);
  196. $B=Rand(50,200);
  197. }
  198. $color=ImageColorAllocate($this->IMAGE,$R,$G,$B);
  199. //柱形高度
  200. $height_now=($this->Y-$this->BORDER)-($this->Y-$this->BORDER*2)*($item_array[$i]/$item_max);
  201. if($i!="0")
  202. ImageLine($this->IMAGE,$xx-$this->BORDER,$height_next,$xx,$height_now,$color);
  203. ImageString($this->IMAGE,$this->FONTSIZE,$xx+2,$height_now-$this->BORDER/2,$item_array[$i],$this->FONTCOLOR);
  204. $height_next=$height_now;
  205. //用于间隔
  206. $xx=$xx+$this->BORDER;
  207. }
  208. }
  209. //--------------饼状图
  210. function imageCircle(){
  211. $total = 0;
  212. $item_array=Split($this->ARRAYSPLIT,$this->ITEMARRAY);
  213. $num=Count($item_array);
  214. $item_max=0;
  215. for ($i=0;$i<$num;$i++){
  216. $item_max=Max($item_max,$item_array[$i]);
  217. $total += $item_array[$i];
  218. }
  219. $yy=$this->Y-$this->BORDER*2;
  220. //画饼状图的阴影部分
  221. $e=0;
  222. for ($i=0;$i<$num;$i++){
  223. srand((double)microtime()*1000000);
  224. if($this->R!=255 && $this->G!=255 && $this->B!=255){
  225. $R=Rand($this->R,200);
  226. $G=Rand($this->G,200);
  227. $B=Rand($this->B,200);
  228. }else{
  229. $R=Rand(50,200);
  230. $G=Rand(50,200);
  231. $B=Rand(50,200);
  232. }
  233. $s=$e;
  234. $leight=$item_array[$i]/$total*360;
  235. $e=$s+$leight;
  236. $color=ImageColorAllocate($this->IMAGE,$R,$G,$B);
  237. $colorarray[$i]=$color;
  238. //画圆
  239. for ($j = 90; $j > 70; $j--) imagefilledarc($this->IMAGE, 110, $j, 200, 100, $s, $e, $color, IMG_ARC_PIE);
  240. //imagefilledarc($this->IMAGE, 110, 70, 200, 100, $s, $e, $color, IMG_ARC_PIE);
  241. //ImageFilledRectangle($this->IMAGE,$this->BORDER,$yy-$this->BORDER,$leight,$yy,$color);
  242. //ImageString($this->IMAGE,$this->FONTSIZE,$leight+2,$yy-$this->BORDER,$item_array[$i],$this->FONTCOLOR);
  243. //用于间隔
  244. $yy=$yy-$this->BORDER*2;
  245. }
  246. //画饼状图的表面部分
  247. $e=0;
  248. for ($i=0;$i<$num;$i++){
  249. srand((double)microtime()*1000000);
  250. if($this->R!=255 && $this->G!=255 && $this->B!=255){
  251. $R=Rand($this->R,200);
  252. $G=Rand($this->G,200);
  253. $B=Rand($this->B,200);
  254. }else{
  255. $R=Rand(50,200);
  256. $G=Rand(50,200);
  257. $B=Rand(50,200);
  258. }
  259. $s=$e;
  260. $leight=$item_array[$i]/$total*360;
  261. $e=$s+$leight;
  262. //$color=$colorarray[$i];
  263. $color=ImageColorAllocate($this->IMAGE,$R,$G,$B);
  264. //画圆
  265. //for ($j = 90; $j > 70; $j--) imagefilledarc($this->IMAGE, 110, $j, 200, 100, $s, $e, $color, IMG_ARC_PIE);
  266. imagefilledarc($this->IMAGE, 110, 70, 200, 100, $s, $e, $color, IMG_ARC_PIE);
  267. }
  268. }
  269. //--------------完成打印图形
  270. function printAll(){
  271. ImagePNG($this->IMAGE);
  272. ImageDestroy($this->IMAGE);
  273. }
  274. //--------------调试
  275. function debug(){
  276. echo "X:".$this->X."
    Y:".$this->Y;
  277. echo "
    BORDER:".$this->BORDER;
  278. $item_array=split($this->ARRAYSPLIT,$this->ITEMARRAY);
  279. $num=Count($item_array);
  280. echo "
    数值个数:".$num."
    数值:";
  281. for ($i=0;$i<$num;$i++){
  282. echo "
    ".$item_array[$i];
  283. }
  284. }
  285. }
  286. //$report->debug();//调式之用
  287. /*
  288. Header( "Content-type:image/png");
  289. $report=new ImageReport;
  290. $report->setImage(600,500,255,255,255,1);//参数(长,高,背影色R,G,B,是否透明1或0)
  291. $temparray="0,260,400,124,48,720,122,440,475";//数值,用指定符号隔开
  292. $report->setItem(',',$temparray,3,23);//参数(分隔数值的指定符号,数值变量,样式1为竖柱图2为横柱图3为折线图4为饼图,距离)
  293. $report->setFont(1);//字体大小1-10
  294. //$report->setX(1,1);//设置X轴刻度值(起始刻度值=1,刻度间隔值=1)
  295. $report->PrintReport();
  296. */
  297. ?>

人气教程排行