欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

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