Home >Backend Development >PHP Tutorial >Draw graphics in PHP
php Editor Shinichi brings you a detailed guide on drawing graphics in PHP. Whether you are drawing basic geometric shapes or creating complex data visualization charts, PHP provides powerful graphics processing capabilities. This article will introduce how to use PHP's GD library and other tools to dynamically generate various graphics in web pages, and explore some practical tips and technologies. Let's explore the wonderful world of drawing graphics in PHP!
Before using pChart
, you first need to install php5. You can get PHP5 as part of XAMPP 5.5.28 from SourceForge.
When you have XAMPP 5.5.28, download pChart
from their official website. After that, extract pChart
into the htdocs
folder of XAMPP 5.5.28.
Open the pChart
folder, its structure should be as shown below:
Notice:
class
folder contains the class definitions we will use. fonts
folder contains font files that we can use in our charts.
After completing the pChart
setup, you can now start drawing.
pChart
to draw a bar chart in PHP
The PHP code to draw a bar chart using pChart
must contain three files in the class
folder. These files are:
pData.class.php
pImage.class.php
pDraw.class.php
Within these files, pData.class.php
allows you to load the data that will be used in the chart. You need pDraw.class.php
to draw the chart.
Next, pImage.class.php
will let you render the chart in your WEB browser. You must include these files using PHP required_once()
.
You can include them using relative paths or define a PCART_PATH
constant. Then using set_include_path()
you can use the short directory name for the pChart
class.
Having said that, we can create a bar chart with pChart
using the following steps:
PCART_PATH
constant. set_include_path()
as the short directory name for the pChart
class. required_once()
to include the pChart
class. pData
object. pData
object using the addPoints
method. pImage
object to create an image for the chart. setGraphArea
method of pData
to set the graph area. drawScale
and drawBarChart
methods of pData
to draw scales and bars. Render
method of pData
. Make sure to pass null
to the Render
method. Below is the implementation of these steps. Below is the output image in Firefox 101.0.
<?php // The definition of the PCHART_PATH assumes // you have pChart one directory above your // current working folder. define("PCHART_PATH", "../pChart"); set_include_path(get_include_path() . PATH_SEPARATOR . PCHART_PATH); // Since we have defined the path, and used // the get_include_path() function, we can // reference the class folder without writing // its full path. require_once "class/pDraw.class.php"; require_once "class/pImage.class.php"; require_once "class/pData.class.php"; // Create the pChart Object $pchart_data = new pData(); // Some sample data that we'll use to plot // the bar chart. $sample_data_set = [5, 4, 3, 2, 1, 9, 10, 12]; $pchart_data->addPoints($sample_data_set); // Create the pChart Image. The first two argument // to the pImage object are the width and height // of the rendered chart. $pchart_image = new pImage(500, 300, $pchart_data); // Set the font. $pchart_image->setFontProperties( ["FontName" => PCHART_PATH . "/fonts/ForGotte.ttf", "FontSize" => 16] ); // Define the graph area. The first two arguments // are the x-coordinates. While the last two are // the y-coordinates. $pchart_image->setGraphArea(35, 25, 475, 275); $pchart_image->drawScale(); $pchart_image->drawBarChart(); // Render the chart as a PNG image header("Content-Type: image/png"); $pchart_image->Render(null); ?>
Output:
pChart
to draw splines in PHP
The process of drawing a spline chart is the same as drawing a bar chart, except that you use the drawSplineChart
method to draw the spline chart. Additionally, you can choose not to send the chart as an image.
Instead, you can choose the Stroke
method of pData
to render the chart in a web browser.
The following code uses pChart
to draw a spline chart. Additionally, we are using the MankSans.ttf
font from the fonts
directory.
<?php // The definition of the PCHART_PATH assumes // you have pChart one directory above your // current working folder. define("PCHART_PATH", "../pChart"); set_include_path(get_include_path() . PATH_SEPARATOR . PCHART_PATH); // Since we have defined the path, and used // the get_include_path() function, we can // reference the class folder without writing // its full path. require_once "class/pDraw.class.php"; require_once "class/pImage.class.php"; require_once "class/pData.class.php"; // Create the pChart Object $pchart_data = new pData(); // Some sample data that we'll use to plot // the spline chart. $pchart_data->addPoints([4,2,1,4]); // Create the pChart Image. The first two argument // to the pImage object are the width and height // of the rendered chart. $pchart_image = new pImage(700, 220, $pchart_data); // Set the font. $pchart_image->setFontProperties( ["FontName" => PCHART_PATH . "/fonts/MankSans.ttf", "FontSize"=> 18] ); // Define the graph area. The first two arguments // are the x-coordinates. While the last two are // the y-coordinates. $pchart_image->setGraphArea(60, 40, 670, 190); $pchart_image->drawScale(); $pchart_image->drawSplineChart(); // Draw the chart as a stroke. $pchart_image->Stroke(); ?>
Output:
Drawing a histogram follows similar steps to bar and spline plots. However, there are some differences worth pointing out.
首先,直方图的数据将来自 Mysql。这意味着你应该有一个包含一些示例数据的数据库。
其次,你将使用表列名称作为直方图上的轴。为此,你将使用一些 pData
方法,例如 setAbscissa
、setSeriesOnAxis
和 setAxisName
。
现在,创建一个名为 weather_measurements
的数据库,然后使用以下命令创建一个表:
CREATE TABLE measures ( timestamp INT NOT NULL DEFAULT '0', temperature INT NOT NULL, humidity INT NOT NULL )
使用以下命令将样本数据插入 measures
表中:
INSERT INTO measures (timestamp, temperature, humidity) VALUES (UNIX_TIMESTAMP(), 20, 50); INSERT INTO measures (timestamp, temperature, humidity) VALUES (UNIX_TIMESTAMP(), 18, 44); INSERT INTO measures (timestamp, temperature, humidity) VALUES (UNIX_TIMESTAMP(), 19, 70);
确保样本数据在数据库中,然后使用以下命令创建直方图:
<?php // The definition of the PCHART_PATH assumes // you have pChart one directory above your // current working folder. define("PCHART_PATH", "../pChart"); set_include_path(get_include_path() . PATH_SEPARATOR . PCHART_PATH); // Since we have defined the path, and used // the get_include_path() function, we can // reference the class folder without writing // its full path. require_once "class/pDraw.class.php"; require_once "class/pImage.class.php"; require_once "class/pData.class.php"; // Create the pChart Object $pchart_data = new pData(); // Connect to MySQL $connect_to_mysql = new mysqli("localhost", "root", "", "weather_measurements"); // query the database and get the result $query_the_table = "SELECT * FROM measures"; $mysql_result= mysqli_query($connect_to_mysql, $query_the_table); // Declare the variables for the database // records as empty strings. Later, we'll // turn them into arrays for better perfORMance $timestamp = ""; $temperature = ""; $humidity = ""; while($row = mysqli_fetch_array($mysql_result, MYSQLI_ASSOC)) { $timestamp[] = $row["timestamp"]; $temperature[] = $row["temperature"]; $humidity[]= $row["humidity"]; } $pchart_data->addPoints($timestamp,"Timestamp"); $pchart_data->addPoints($temperature,"Temperature"); $pchart_data->addPoints($humidity,"Humidity"); // Put the table column on the appropriate axis $pchart_data->setAbscissa("Timestamp"); $pchart_data->setSerieOnAxis("Humidity", 1); $pchart_data->setXAxisName("Time"); $pchart_data->setXAxisDisplay(AXIS_FORMAT_TIME,"H:i"); // Dedicate the first and second axis to // Temperature and Humidity. $pchart_data->setAxisName(0, "Temperature"); $pchart_data->setAxisUnit(0, "°C"); $pchart_data->setAxisName(1, "Humidity"); $pchart_data->setAxisUnit(0, "%"); // Create the pChart Image. The first two argument // to the pImage object are the width and height // of the rendered chart. $pchart_image = new pImage(500, 300, $pchart_data); // Set the font. $pchart_image->setFontProperties( ["FontName" => PCHART_PATH . "/fonts/verdana.ttf", "FontSize"=> 11] ); // Set the graph area. $pchart_image->setGraphArea(55,25, 475,275); $pchart_image->drawScale(); $pchart_image->drawBarChart(); // Draw the chart as a stroke. $pchart_image->Stroke(); ?>
输出(你的时间会有所不同):
The above is the detailed content of Draw graphics in PHP. For more information, please follow other related articles on the PHP Chinese website!