Home >Backend Development >PHP Tutorial >PHP picture class that can display a variety of graphical reports

PHP picture class that can display a variety of graphical reports

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-25 09:11:071083browse
  1. class ImageReport{
  2. var $X;//Picture size X-axis
  3. var $Y;//Picture size Y-axis
  4. var $R;//Back color R value
  5. var $G;//...G.
  6. var $B;//...B.
  7. var $TRANSPARENT;//Whether it is transparent 1 or 0
  8. var $IMAGE;//Image object
  9. //- ------------------
  10. var $ARRAYSPLIT;//Specify the symbol used to separate values ​​
  11. var $ITEMARRAY;//Value
  12. var $REPORTTYPE;//Chart type, 1 is a vertical column, 2 is a horizontal column, 3 is a polyline shape
  13. var $BORDER;//distance
  14. //-------------------
  15. var $FONTSIZE;/ /Font size
  16. var $FONTCOLOR;//Font color
  17. var $numX = 1;//X-axis starting scale value
  18. var $stepX = 1;//X-axis each scale interval value
  19. //-- ------Parameter setting function
  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-axis scale value setting
  38. function setX($numX = 1 , $stepX = 1){
  39. $this->numX = $numX;
  40. $this->stepX = $stepX;
  41. }
  42. //---------------- Main body
  43. function PrintReport(){
  44. //Create canvas size
  45. $this->IMAGE=ImageCreate($this->X,$this->Y);
  46. //Set canvas background color
  47. $background= ImageColorAllocate($this->IMAGE,$this->R,$this->G,$this->B);
  48. if($this->TRANSPARENT=="1"){
  49. // The background is transparent
  50. Imagecolortransparent($this->IMAGE,$background);
  51. }else{
  52. //If you don’t want to make it transparent, you can fill it with the background color
  53. ImageFilledRectangle($this->IMAGE,0,0,$this-> X,$this->Y,$background);
  54. }
  55. //Parameter font size and color
  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. //-------- ---Print the XY coordinate axis
  77. function printXY(){
  78. $rulerY = $rulerX = "";
  79. //Draw the XY coordinate axis*/
  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 axis
  84. ImageLine($this->IMAGE,$this->BORDER,$this->Y-$this->BORDER,$this->X-$this- >BORDER,$this->Y-$this->BORDER,$color);//y-axis
  85. imagestring($this->IMAGE, $this->FONTSIZE, $this->BORDER- 2, $this->Y-$this->BORDER+5, "0", $color);
  86. //Scale on Y axis
  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){//Horizontal column chart
  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. //Scale on the while($rulerX<($this->X-$this->BORDER*2)){
  99. $rulerX=$rulerX+$this->BORDER;
  100. //ImageLine($this->IMAGE,$ this->BORDER,10,$this->BORDER+10,10,$color);
  101. ImageLine($this->IMAGE,$rulerX,$this->Y-$this->BORDER, $rulerX,$this->Y-$this->BORDER+2,$color);
  102. //Scale value
  103. if($this->REPORTTYPE == 1){//Vertical column chart
  104. imagestring ($this->IMAGE, $this->FONTSIZE, $rulerX-2+$this->BORDER*($i+.5), $this->Y-$this->BORDER+5, $this->numX, $color);
  105. $this->numX += $this->stepX;
  106. }else if($this->REPORTTYPE == 3){//折线图
  107. imagestring($this->IMAGE, $this->FONTSIZE, $rulerX-2, $this->Y-$this->BORDER+5, $this->numX, $color);
  108. $this->numX += $this->stepX;
  109. }
  110. $i++;
  111. }
  112. }
  113. //--------------竖柱形图
  114. function imageColumnS(){
  115. $item_array=Split($this->ARRAYSPLIT,$this->ITEMARRAY);
  116. $num=Count($item_array);
  117. $item_max=0;
  118. for ($i=0;$i<$num;$i++){
  119. $item_max=Max($item_max,$item_array[$i]);
  120. }
  121. $xx=$this->BORDER*2;
  122. //画柱形图
  123. for ($i=0;$i<$num;$i++){
  124. srand((double)microtime()*1000000);
  125. if($this->R!=255 && $this->G!=255 && $this->B!=255){
  126. $R=Rand($this->R,200);
  127. $G=Rand($this->G,200);
  128. $B=Rand($this->B,200);
  129. }else{
  130. $R=Rand(50,200);
  131. $G=Rand(50,200);
  132. $B=Rand(50,200);
  133. }
  134. $color=ImageColorAllocate($this->IMAGE,$R,$G,$B);
  135. //柱形高度
  136. $height=($this->Y-$this->BORDER)-($this->Y-$this->BORDER*2)*($item_array[$i]/$item_max);
  137. ImageFilledRectangle($this->IMAGE,$xx,$height,$xx+$this->BORDER,$this->Y-$this->BORDER,$color);
  138. ImageString($this->IMAGE,$this->FONTSIZE,$xx,$height-$this->BORDER,$item_array[$i],$this->FONTCOLOR);
  139. //用于间隔
  140. $xx=$xx+$this->BORDER*2;
  141. }
  142. }
  143. //-----------横柱形图
  144. function imageColumnH(){
  145. $item_array=Split($this->ARRAYSPLIT,$this->ITEMARRAY);
  146. $num=Count($item_array);
  147. $item_max=0;
  148. for ($i=0;$i<$num;$i++){
  149. $item_max=Max($item_max,$item_array[$i]);
  150. }
  151. $yy=$this->Y-$this->BORDER*2;
  152. //画柱形图
  153. for ($i=0;$i<$num;$i++){
  154. srand((double)microtime()*1000000);
  155. if($this->R!=255 && $this->G!=255 && $this->B!=255){
  156. $R=Rand($this->R,200);
  157. $G=Rand($this->G,200);
  158. $B=Rand($this->B,200);
  159. }else{
  160. $R=Rand(50,200);
  161. $G=Rand(50,200);
  162. $B=Rand(50,200);
  163. }
  164. $color=ImageColorAllocate($this->IMAGE,$R,$G,$B);
  165. //柱形长度
  166. $leight=($this->X-$this->BORDER*2)*($item_array[$i]/$item_max);
  167. $leight = $leight < $this->BORDER ? $this->BORDER : $leight;
  168. ImageFilledRectangle($this->IMAGE,$this->BORDER,$yy-$this->BORDER,$leight,$yy,$color);
  169. ImageString($this->IMAGE,$this->FONTSIZE,$leight+2,$yy-$this->BORDER,$item_array[$i],$this->FONTCOLOR);
  170. //用于间隔
  171. $yy=$yy-$this->BORDER*2;
  172. }
  173. }
  174. //--------------折线图
  175. function imageLine(){
  176. $item_array=Split($this->ARRAYSPLIT,$this->ITEMARRAY);
  177. $num=Count($item_array);
  178. $item_max=0;
  179. for ($i=0;$i<$num;$i++){
  180. $item_max=Max($item_max,$item_array[$i]);
  181. }
  182. $xx=$this->BORDER;
  183. //画柱形图
  184. for ($i=0;$i<$num;$i++){
  185. srand((double)microtime()*1000000);
  186. if($this->R!=255 && $this->G!=255 && $this->B!=255){
  187. $R=Rand($this->R,200);
  188. $G=Rand($this->G,200);
  189. $B=Rand($this->B,200);
  190. }else{
  191. $R=Rand(50,200);
  192. $G=Rand(50,200);
  193. $B=Rand(50,200);
  194. }
  195. $color=ImageColorAllocate($this->IMAGE,$R,$G,$B);
  196. //柱形高度
  197. $height_now=($this->Y-$this->BORDER)-($this->Y-$this->BORDER*2)*($item_array[$i]/$item_max);
  198. if($i!="0")
  199. ImageLine($this->IMAGE,$xx-$this->BORDER,$height_next,$xx,$height_now,$color);
  200. ImageString($this->IMAGE,$this->FONTSIZE,$xx+2,$height_now-$this->BORDER/2,$item_array[$i],$this->FONTCOLOR);
  201. $height_next=$height_now;
  202. //用于间隔
  203. $xx=$xx+$this->BORDER;
  204. }
  205. }
  206. //--------------饼状图
  207. function imageCircle(){
  208. $total = 0;
  209. $item_array=Split($this->ARRAYSPLIT,$this->ITEMARRAY);
  210. $num=Count($item_array);
  211. $item_max=0;
  212. for ($i=0;$i<$num;$i++){
  213. $item_max=Max($item_max,$item_array[$i]);
  214. $total += $item_array[$i];
  215. }
  216. $yy=$this->Y-$this->BORDER*2;
  217. //画饼状图的阴影部分
  218. $e=0;
  219. for ($i=0;$i<$num;$i++){
  220. srand((double)microtime()*1000000);
  221. if($this->R!=255 && $this->G!=255 && $this->B!=255){
  222. $R=Rand($this->R,200);
  223. $G=Rand($this->G,200);
  224. $B=Rand($this->B,200);
  225. }else{
  226. $R=Rand(50,200);
  227. $G=Rand(50,200);
  228. $B=Rand(50,200);
  229. }
  230. $s=$e;
  231. $leight=$item_array[$i]/$total*360;
  232. $e=$s+$leight;
  233. $color=ImageColorAllocate($this->IMAGE,$R,$G,$B);
  234. $colorarray[$i]=$color;
  235. //画圆
  236. for ($j = 90; $j > 70; $j--) imagefilledarc($this->IMAGE, 110, $j, 200, 100, $s, $e, $color, IMG_ARC_PIE);
  237. //imagefilledarc($this->IMAGE, 110, 70, 200, 100, $s, $e, $color, IMG_ARC_PIE);
  238. //ImageFilledRectangle($this->IMAGE,$this->BORDER,$yy-$this->BORDER,$leight,$yy,$color);
  239. //ImageString($this->IMAGE,$this->FONTSIZE,$leight+2,$yy-$this->BORDER,$item_array[$i],$this->FONTCOLOR);
  240. //用于间隔
  241. $yy=$yy-$this->BORDER*2;
  242. }
  243. //画饼状图的表面部分
  244. $e=0;
  245. for ($i=0;$i<$num;$i++){
  246. srand((double)microtime()*1000000);
  247. if($this->R!=255 && $this->G!=255 && $this->B!=255){
  248. $R=Rand($this->R,200);
  249. $G=Rand($this->G,200);
  250. $B=Rand($this->B,200);
  251. }else{
  252. $R=Rand(50,200);
  253. $G=Rand(50,200);
  254. $B=Rand(50,200);
  255. }
  256. $s=$e;
  257. $leight=$item_array[$i]/$total*360;
  258. $e=$s+$leight;
  259. //$color=$colorarray[$i];
  260. $color=ImageColorAllocate($this->IMAGE,$R,$G,$B);
  261. //画圆
  262. //for ($j = 90; $j > 70; $j--) imagefilledarc($this->IMAGE, 110, $j, 200, 100, $s, $e, $color, IMG_ARC_PIE);
  263. imagefilledarc($this->IMAGE, 110, 70, 200, 100, $s, $e, $color, IMG_ARC_PIE);
  264. }
  265. }
  266. //--------------完成打印图形
  267. function printAll(){
  268. ImagePNG($this->IMAGE);
  269. ImageDestroy($this->IMAGE);
  270. }
  271. //--------------调试
  272. function debug(){
  273. echo "X:".$this->X."
    Y:".$this->Y;
  274. echo "
    BORDER:".$this->BORDER;
  275. $item_array=split($this->ARRAYSPLIT,$this->ITEMARRAY);
  276. $num=Count($item_array);
  277. echo "
    数值个数:".$num."
    数值:";
  278. for ($i=0;$i<$num;$i++){
  279. echo "
    ".$item_array[$i];
  280. }
  281. }
  282. }
  283. //$report->debug();//调式之用
  284. /*
  285. Header( "Content-type:image/png");
  286. $report=new ImageReport;
  287. $report->setImage(600,500,255,255,255,1);//参数(长,高,背影色R,G,B,是否透明1或0)
  288. $temparray="0,260,400,124,48,720,122,440,475";//数值,用指定符号隔开
  289. $report->setItem(',',$temparray,3,23);//参数(分隔数值的指定符号,数值变量,样式1为竖柱图2为横柱图3为折线图4为饼图,距离)
  290. $report->setFont(1);//字体大小1-10
  291. //$report->setX(1,1);//设置X轴刻度值(起始刻度值=1,刻度间隔值=1)
  292. $report->PrintReport();
  293. */
  294. ?>
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn