Using charts to analyze product data is currently the most common data management mode for large and medium-sized enterprises. using charts to analyze products not only makes a clear picture, but also makes timely decisions on the next product plan, it can be seen that the use of charts to analyze product data trends is the foundation of rapid development of enterprises. This article introduces charts to analyze product data!
Use the GD2 function to analyze product data using charts (PHP graphic image typical application tutorial 6)
Using charts to analyze product data is currently the most common data management mode for large and medium-sized enterprises. using charts to analyze products not only makes a clear picture, but also makes timely decisions on the next product plan, it can be seen that the use of charts to analyze product data trends is the foundation of rapid development of enterprises. This article introduces charts to analyze product data!
In the previous article "adding row and column labels to charts using the GD2 function (PHP graphic image typical application tutorial 5)", we introduced adding row and column labels to charts, therefore, using the GD2 function to analyze product data is inseparable from the previous article. to use the previous article, we will introduce it in detail!
Technical points
It is mainly applied to the array () function and the imagestring () function to add various product data information to the chart. here, the imagestring () function is described in detail in an article we have deleted, array () is also described in detail in the array topic. here we will not introduce it in detail. if you are not clear about it, you can refer to our previous articles and topics!
Implementation process
(1) load an image using the imagecreatepng () function and use an array to customize two constants. the code is as follows;
(2) after creating a background image, you can perform various operations on the background. before the operation, you must call the imagecolorallcate () function to define the color of the image. the specific code is as follows:
$ Black = imagecolorallocate ($ im, 255, 0, 0); // you can specify the color,
(3) after the color value is defined, use the imageline () function to draw the coordinates of the X and Y axes, and use the imagestring () function to output the characters X and Y. the specific code is as follows:
Imageline ($ im, 0,532, 0,437,585,437, $ black); // sets the y axis ordinate imageline ($ im, $ black); // sets the x axis ordinate imagestring ($ im, 10, 0, 5, "Y", $ black); // output character Yimagestring ($ im, 10,560,422, "X", $ black); // output character X
(4) after creating the coordinates, define four variables and assign the initial values to them. the for loop statement is used to output the label text cyclically. the output without the label text is called imagestring () the code for function implementation is as follows:
$ X = 30; $ y = 209; $ x_width = 61; $ y_ht = 0; for ($ I = 0; $ I <7; $ I ++) {imagestring ($ im, 5, $ X-1, $ y + 180, $ month [$ I], $ black ); // set the distance between the language and the X axis imagestring ($ im, 5, $ X-1, $ y + 200, $ data [$ I], $ black ); // set the distance between the language and the number $ x + = ($ x_width + 20); // set the width between the language and the number to 20 pixels}
(5) the image needs to be output to the browser. the common practice is to save the image to a file and output it to the browser. This example uses the imagepng () function to output the image code as follows:
Imagepng ($ im, "a.png"); echo ""; // output image
Note:
The imagepng () function sends the output content in png format to the browser. if you want to output the content in different formats, you should call the corresponding function. if you want to send the content in GIF format, you should call the imagegif () function.
(6) after image processing, call the imagedestroy () function to release image resources. the code is as follows:
Imagedestroy ($ im); // release image resources
After completing the preceding steps, output the address in the browser and press enter to obtain the table result:
We have finished the introduction of PHP image processing. if you have any supplements, you can leave a message and we will promptly add the content, so we will not scatter our next topic!
The preceding section describes how to use the GD2 function to analyze product data (PHP graphic image typical application tutorial 6). For more information, see other related articles in the first PHP community!