Use GD2 function to implement chart analysis of product data (Typical application tutorial of PHP graphics and images 6)

黄舟
Release: 2023-03-07 16:20:02
Original
2075 people have browsed it

Use the GD2 function to implement chart analysis of product data (Typical application tutorial 6 of PHP graphics and images)

Using charts to analyze product data information is currently large, medium and small The most commonly used data management model for enterprises. Analyzing products through charts can not only make it clear at a glance, but also make timely decisions on the next step of product planning, quickly improving the economic benefits of the enterprise. It can be seen that using charts to analyze product data trends is an important step for enterprises to quickly The foundation of development, this article is to introduce chart analysis of product data!

In the previous article "Using the GD2 function to add row and column labels to the chart (Typical application tutorial of PHP graphics images 5)" we introduced adding row and column labels to the chart, Then using the GD2 function to implement chart analysis of product data is inseparable from the content of the previous article. To use the content of the previous article, let’s introduce it in detail below!

Technical points

It is mainly used in the array() function and imagestring() function to add various product data information to the chart, among which, We have introduced the imagestring() function in detail in an article we have deleted. Array() is also introduced in detail in the array topic. We will not introduce it in detail here. Friends who are not sure can go and take a look. Our previous articles and features!

Implementation process

(1) Load an image through the imagecreatepng() function, and use the array to customize two constants, the specific code As follows;

<?php
header("Content-Type:text/html; charset=utf-8");
$im = imagecreatefrompng("upfile/2.png"); //载入一张 png 格式图片
$data = array(40,50,60,70,80,90,100);
$month = array("vb","vc","jsp","JS","C++","JAVA","PHP");
?>
Copy after login

(2) After creating the background image, you can perform various operations on the background. Before performing the operation, you must call the imagecolorallcate() function to define the color of the drawn image. The specific code is as follows:

$black = imagecolorallocate($im,255,0,0); //设置颜色值,
Copy after login

(3) After defining the color value, use the imageline() function to draw the coordinates of the X-axis and Y-axis, and then use the imagestring() function to output the characters X and Y. The specific code is as follows:

imageline($im,0,20,0,532,$black);         //设置Y轴纵坐标
imageline($im,0,437,585,437,$black);      //设置X轴纵坐标
imagestring($im,10,0,5,"Y",$black);       //输出字符Y
imagestring($im,10,560,422,"X",$black);   //输出字符X
Copy after login

(4) After creating the coordinates, define 4 variables and assign initial values ​​to them. Use the for loop statement to loop out the label text. The output without label text is achieved by calling the imagestring() function. The specific code 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); //设置语言与 X 轴之间的距离
    imagestring($im,5,$x-1,$y+200,$data[$i],$black);  //设置语言与数量之间的距离
    $x +=($x_width+20);                               //设置语言与语言,数量之间的宽度为20像素 
}
Copy after login

(5) It is necessary to output the graphics to the browser. The usual method is to save it 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  "<img  src=&#39;a.png&#39; alt="Use GD2 function to implement chart analysis of product data (Typical application tutorial of PHP graphics and images 6)" >";                //输出图像
Copy after login

Note:

imagepng() function is The output content is sent to the browser in png format. If the user requires output in a different format, the corresponding function should be called. If the content is sent in GIF format, the imagegif() function should be called.

(6) After completing the image processing, you need to call the imagedestroy() function to release the image resources. The specific code is as follows:

imagedestroy($im);                       //释放图像资源
Copy after login

Complete the above steps, and then Output the address in the browser and press the Enter key to get the following chart result:

Use GD2 function to implement chart analysis of product data (Typical application tutorial of PHP graphics and images 6)

We have finished introducing PHP graphics and image processing here. If you have anything to add, please add Leave a message on the article and we will add the content in time, so see you on the next topic!

The above is the detailed content of Use GD2 function to implement chart analysis of product data (Typical application tutorial of PHP graphics and images 6). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!