Php image class that displays multiple graphical reports

Source: Internet
Author: User
Php image class that displays multiple graphical reports

  1. Class ImageReport {
  2. Var $ X; // Image Size X axis
  3. Var $ Y; // the Y axis of the image size.
  4. Var $ R; // R value of the background color
  5. Var $ G; //... G.
  6. Var $ B; //... B.
  7. Var $ TRANSPARENT; // whether TRANSPARENT 1 or 0
  8. Var $ IMAGE; // IMAGE
  9. //-------------------
  10. Var $ ARRAYSPLIT; // specifies the symbol used to separate numeric values.
  11. Var $ ITEMARRAY; // value
  12. Var $ REPORTTYPE; // Chart type. 1 is a vertical column, 2 is a horizontal column, and 3 is a line.
  13. Var $ BORDER; // Distance
  14. //-------------------
  15. Var $ FONTSIZE; // font size
  16. Var $ FONTCOLOR; // font color
  17. Var $ numX = 1; // Start scale value of the X axis
  18. Var $ stepX = 1; // The interval between each scale on the x axis
  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. // Set the x axis scale value
  38. Function setX ($ numX = 1, $ stepX = 1 ){
  39. $ This-> numX = $ numX;
  40. $ This-> stepX = $ stepX;
  41. }
  42. // ---------------- Subject
  43. Function PrintReport (){
  44. // Create the canvas size
  45. $ This-> IMAGE = ImageCreate ($ this-> X, $ this-> Y );
  46. // Set the canvas background color
  47. $ Background = ImageColorAllocate ($ this-> IMAGE, $ this-> R, $ this-> G, $ this-> B );
  48. If ($ this-> TRANSPARENT = "1 "){
  49. // Transparent Background
  50. Imagecolortransparent ($ this-> IMAGE, $ background );
  51. } Else {
  52. // The background color can be filled if it is not transparent.
  53. ImageFilledRectangle ($ this-> IMAGE, 0, 0, $ this-> X, $ this-> Y, $ background );
  54. }
  55. // Small integer 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 axis
  77. Function printXY (){
  78. $ RulerY = $ rulerX = "";
  79. // Draw the XY 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-> 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 up on the 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 bar 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 up on the x axis
  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. // Scale value
  106. If ($ this-> REPORTTYPE = 1) {// Vertical bar chart
  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) {// Line chart
  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. // -------------- Vertical bar chart
  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. // Draw a column chart
  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 = revert (50,200 );
  134. $ G = revert (50,200 );
  135. $ B = revert (50,200 );
  136. }
  137. $ Color = ImageColorAllocate ($ this-> IMAGE, $ R, $ G, $ B );
  138. // Column height
  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. // Used for interval
  143. $ Xx = $ xx + $ this-> BORDER * 2;
  144. }
  145. }
  146. // ----------- Horizontal column chart
  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. // Draw a column chart
  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 = revert (50,200 );
  164. $ G = revert (50,200 );
  165. $ B = revert (50,200 );
  166. }
  167. $ Color = ImageColorAllocate ($ this-> IMAGE, $ R, $ G, $ B );
  168. // Column length
  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. // Used for interval
  174. $ Yy = $ yy-$ this-> BORDER * 2;
  175. }
  176. }
  177. // -------------- Line chart
  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. // Draw a column chart
  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 = revert (50,200 );
  195. $ G = revert (50,200 );
  196. $ B = revert (50,200 );
  197. }
  198. $ Color = ImageColorAllocate ($ this-> IMAGE, $ R, $ G, $ B );
  199. // Column height
  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. // Used for interval
  206. $ Xx = $ xx + $ this-> BORDER;
  207. }
  208. }
  209. // -------------- Pie chart
  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. // Draw the shadow part of the pie chart
  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 = revert (50,200 );
  230. $ G = revert (50,200 );
  231. $ B = revert (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. // Circle
  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. // Used for interval
  244. $ Yy = $ yy-$ this-> BORDER * 2;
  245. }
  246. // Draw the surface of the pie chart
  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 = revert (50,200 );
  256. $ G = revert (50,200 );
  257. $ B = revert (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. // Circle
  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. // -------------- Print the image
  270. Function printAll (){
  271. ImagePNG ($ this-> IMAGE );
  272. ImageDestroy ($ this-> IMAGE );
  273. }
  274. // -------------- Debug
  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"
    Number of values: ". $ num ."
    Value :";
  281. For ($ I = 0; $ I <$ num; $ I ++ ){
  282. Echo"
    ". $ Item_array [$ I];
  283. }
  284. }
  285. }
  286. // $ Report-> debug (); // call for debugging
  287. /*
  288. Header ("Content-type: image/png ");
  289. $ Report = new ImageReport;
  290. $ Report-> setImage (600,500,255,255,255, 1); // parameter (long, high, back color R, G, B, whether transparent 1 or 0)
  291. $ Temparray = "0,260,400,124, 48,720,122,440,475"; // value, separated by a specified symbol
  292. $ Report-> setItem (',', $ temparray, 3, 23); // parameters (specify the separator between values, numerical variables, style 1 is a vertical column, a horizontal column, a broken line, a pie chart, and a distance)
  293. $ Report-> setFont (1); // font size: 1-10
  294. // $ Report-> setX (); // Set the x axis scale value (start scale value = 1, scale interval value = 1)
  295. $ Report-> PrintReport ();
  296. */
  297. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.