All content: How to use PHP dynamically generated pie chart, line chart and histogram topic discussion area: http://hh.cc163.com/Form/ Home: http://hh.cc163.com/
table of Contents
Preface
Design Ideas Realization Process Usage Implementation Map
Designing Ideas Implementation Process Usage Realization Effects Column
Design Ideas Implementation Process Usage Real Reference Related Links
sequence
PHP performs very well in image operation, we only need to make a picture, and the table is checked. Hereinafter, the author's scorpion map, a line diagram, and a histogram, and the method of using them. The characteristics of these codes are not needed to copy them into your code, just need to calculate the data. Parameter is incorporated, you can get the corresponding graphics effect
Development Environment: PHP Version 4.3.6 GD Version bundled (2.0.22 compatible)
Because the author is limited, there is a mistake in the article, I will be very grateful to your point
For instructions for all functions in the code, see the PHP Development Document Click here to get the latest version.
pie chart
Design ideas
The cake graph is a good method for viewing a percentage of a total value. We use PHP to implement a pie chart. Its design ideas are: 1 accept parameters, get all values, get the ratio of each value accounting for the sum of the quotations. 2 Calculate the circumference of each color block according to the proportion of the circumferential angle 3 to generate a stereo effect, only need to draw a shadow with deep color.
Implementation process
// Parameter is incorporated with A as a parameter name, and a text shape of A should be "," segmented several digitally connected strings // here first determined whether there is IF ($ _ get ["a"] == "") DIE ("0"); // will be decomposed by data, deposit the array $ SHUJU $ SHUJU = Split (",", $ _ get ["a"]); // Returgate the legality again , Return error code if (count ($ shuju) == 0) DIE ("2"); // Defines the width and height of the entire graphic and height // readers can modify the value of these two variables as needed $ TUKUAN = 300; $ Tugao = 150;
/ / Define an array to store the angle range of each color block $ jIAODU = array (); // Define storage data and variable $ total = 0; // Traverse the number of components and for ($ I = 0; $ I // Create an image $ image = imagecreate ($ TUKUAN, $ TUGAO); // Defines a gray background color, this color is actually a very familiar page color 16 enrollment numbers represent # eeeeee $ white = imagecolorlocate ($ image 0xee, 0xee, 0xee); // Remail the color of 10 pairs of deep shadows, deposit 2D array $ yanse = array (imagecolroomlocate ($ image, 0x97, 0xbd, 0x00), ImageColorallocate ($ image, 0x00, 0x99, 0x00), ImageColorlocate ($ image, 0xcc, 0x33, 0x00), imagecolorallocate ($ image, 0xff, 0xcc, 0x00), imagecolorallocate ($ image, 0x33, 0x66, 0xcc), imagecolorallocate ($ image, 0x33, 0xcc, 0x33), imagecolorallocate ($ image, 0xff, 0x99, 0x33), imagecolorallocate ($ image, 0xcc, 0xcc, 0x99), imagecolorallocate ($ image, 0x99, 0xcc, 0x66), imagecolorallocate ($ image, 0x66, 0xff, 0x99)), array (imagecolorallocate ($ image , 0x4f, 0x66, 0x00), imagecolorallocate ($ image, 0x00, 0x33, 0x00), imagecolorallocate ($ image, 0x48, 0x10, 0x00), imagecolorallocate ($ image, 0x7d, 0x64, 0x00), imagecolorallocate ($ image, 0x17 , 0x30, 0x64), imagecolorallocate ($ image, 0x1a, 0x6a, 0x1a), imagecolorallocate ($ image, 0x97, 0x4b, 0x00), imagecolorallocate ($ image, 0x78, 0x79, 0x3c), imagecolorallocate ($ image, 0x55, 0x7e , 0x27), ImageColo Rallocate ($ image, 0x00, 0x93, 0x37))))); // 10 pixels from the bottom to top 5 pixels, as a shadow $ yuanxin_x = $ TUKUAN / 2; for ($ h = $ tugao / 2 5; $ H> $ 5 {$ kaishi = 0; $ jieshu = 0; for ($ I = 0; $ I ) {$ Kaishi = $ Kaishi 0; $ jieshu = $ kaishi $ jiaodu [$ I]; $ yanse_i = fmod ($ I, 10); ImageFilleDarc ($ Image, $ YUANXIN_XIN_X, $ H, $ TUKUAN, $ TUGAO-20, $ Kaishi $ Jieshu, $ Yanse [1] [$ yanse_i], IMG_ARC_PIE); $ Kaishi = $ JIAODU [$ I]; $ Jieshu = $ JIAODU [$ I];}} // Painted a light-color pie chart at the highest (that is, $ H), this light figure with the dark pie chart of the first painted, can produce a three-dimensional effect for FOR ($ I = 0; $ I Inserting the following code is inserted into the image A of the text format of "," connection, a string, GET method Incoming. Color maps are now, please arrange themselves: Achieve effect line chart Design ideas Seeing the change in a variation of a data in a line chart is a good choice. We use PHP to implement a dynamic line chart. Its design ideas are: 1 accept parameters, get all values, to obtain the maximum value of the data to determine the maximum scale value of the vertical axis determine the width of the image according to the number of data, and draw the horizontal axis and longitudinal axis coordinates and scale 3 Painting straight line connection, fill a 2 * 2 rectangle for each point, the position of the protrusion 4 labels the data value of each point at the top right of each point Implementation process $ IMG_GAO = 170; $ IMG_KUAN = 0; $ jiange = 30; // The interval between the horizontal punctuation point and the point, the generated picture width automatically changes $ zuo = 20 according to the incoming data; // Left left left blank $ YOU = 20; // Right left on the right side of the blank $ XIA = 20; // Lower Leave a blank $ zuidashujuzhi = 1; $ p_x = array (); // point Coordinate $ p_y = array (); // Note Coordinate $ y_name = split (",", $ _ get ["x_name"]); if ($ _Get ["a"] == "" Die ("Error ID: 0 "); $ SHUJU = Split (", ", $ _ get [" a "]); // Get the maximum value for the longitudinal axis for ($ I = 0; $); $ i ) {IF ($ SHUJU [$ I])) DIE ("Error ID: 1"); IF ($ SHUJU [$ I]> $ zuidashujuzhi) $ zuidashujuzhi = $ shuju [$ I];} // get Image Width $ IMG_KUAN = $ zuo $ YOU COUNT ($ SHUJU) * $ jiange; // then create an image resource $ image = imagecreate ($ IMG_KUAN, $ IMG_GAO); // Gray background $ white = imagecolorallocate ($ image, 0xee 0xee, 0xee); // The coordinate axis displays $ zuobiao_yanse = imagecolorlocate ($ image, 0x00, 0x00, 0x00); // Paste, 4 = ImageColorAllocate ($ image, 0x00, 0x00, 0xff); // Painted coordinate // Horizontal ImageLine ($ IMG_GAO- $ xia, $ IMG_KUAN- $ YOA, $ IMG_GAO- $ xia, $ zuobiao_yanse); // vertical axis imagelineline ($ image, Zuo, $ SHANG / 2, $ Zuo, $ IMG_GAO- $ xia, $ zuobiao_yanse; // Get each point of coordinate for ($ I = 0; $ SHUJU); $ I ) {Array_Push ( $ p_x, $ zuo $ i * $ jiange); A Rray_push ($ P_Y, $ SHANG ROUND ($ IMG_GAO- $ XIA) * (1- $ SHUJU [$ I] / $ zuidashujuzhi)));} // Longitudinal axis imaging ($ zuo 6, $ shang, $ zuobiao_yanse; imageString ($ Image, 1, $ Zuo / 4, $ SHANG, $ zuidashujuzhi, $ zuobiao_yanse; Imageline ($ Imageline ($ IMG_GAO- $ SHANG- $ xia) * 1/4, $ zuo 6, $ Shanghai ($ IMG_GAO- $ SHANG- $ xia) * 1/4, $ zuobiao_yanse ImageString ($ Image, 1, $ Zuo / 4, $ shang ($ IMG_GAO- $ SHANG- $ xia) * 1/4, $ zuidashujuzhi * 3/4, $ zuobiao_yanse; imageline ($ images, $ zuo, $ Shang ($ IMG_GAO- $ SHANG- $ xia) * 2/4, $ ZuO 6, $ SHANG ($ IMG_GAO- $ SHANG- $ xia) * 2/4, $ zuobiao_yanse; imagestring ($ image, 1, $ Zuo / 4, $ SHANG ($ IMG_GAO- $ SHANG- $ xia) * 2/4, $ zuidashujuzhi * 2/4, $ zuobiao_yanse; imageline ($ zuo, $ shang ($ IMG_GAO- $ SHANG- $ XIA) * 3/4, $ zuo 6, $ SHANG ($ IMG_GAO- $ SHANG- $ xia) * 3/4, $ zuobiao_yanse; ImagesTring ($ Image, 1, $ Zuo / 4, $ SHANG ($ IMG_GAO - $ SHANG- $ xia) * 3/4, $ zuidashujuzhi * 1/4, $ zuobiao_yanse); // Horizontal scale for ($ I = 0; $ 6); $ i ) {imagelineline $ IMAGE ($ IMG_GAO- $ xia, $ zuo $ I * $ JIANGE, $ IMG_GAO- $ xia-6, $ zuobiao_yanse; imageString ($ image, 1, $ zuo $ i * $ @ $ * Jiange- $ J IANGE / 4, $ SHANG ($ IMG_GAO- $ SHANG- $ xia) 2, $ y_name [$ I], $ zuobiao_yanse;} // Passer $ shuju_yanse_int = 0; for ($ I = 0; $ I Insert the following code of the value by you It is calculated that the text format of a A ",", ",", ",", GET, incorporated. Since writing in Chinese is required to write Chinese, there is more PHP environment configuration, so it will be given an HTML solution. It is also very flexible: everyone only needs to dynamically generate a table to place the horizontal axis coordinate scale according to the number of data. , Like this