Draw graphics in PHP

WBOY
Release: 2024-02-29 17:04:01
forward
1266 people have browsed it

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!


Set up your environment

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:

在 PHP 中绘制图形

Notice:

  • The 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.


Using 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:

  • Define the PCART_PATH constant.
  • Use set_include_path() as the short directory name for the pChart class.
  • Use required_once() to include the pChart class.
  • Create a new pData object.
  • Create your data or import it.
  • Add data to the pData object using the addPoints method.
  • Use the pImage object to create an image for the chart.
  • Set the font of the chart.
  • Use the setGraphArea method of pData to set the graph area.
  • Use the drawScale and drawBarChart methods of pData to draw scales and bars.
  • Send header information to tell the browser that you are sending an image.
  • Render the image using the 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&#39;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);
?>
Copy after login

Output:

使用 pChart 绘制的条形图


Use 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&#39;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();
?>
Copy after login

Output:

用 pChart 绘制的样条图


Drawing a bar chart from mysql database in PHP

Drawing a histogram follows similar steps to bar and spline plots. However, there are some differences worth pointing out.

首先,直方图的数据将来自 Mysql。这意味着你应该有一个包含一些示例数据的数据库

其次,你将使用表列名称作为直方图上的轴。为此,你将使用一些 pData 方法,例如 setAbscissasetSeriesOnAxissetAxisName

现在,创建一个名为 weather_measurements 的数据库,然后使用以下命令创建一个表:

CREATE TABLE measures (
timestamp INT NOT NULL DEFAULT &#39;0&#39;,
temperature INT NOT NULL,
humidity INT NOT NULL
)
Copy after login

使用以下命令将样本数据插入 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);
Copy after login

确保样本数据在数据库中,然后使用以下命令创建直方图:

<?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 = "SELEC&#84; * FROM measures";
$mysql_result= mysqli_query($connect_to_mysql, $query_the_table);
// Declare the variables for the database
// records as empty strings. Later, we&#39;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, "&deg;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();
?>
Copy after login

输出(你的时间会有所不同):

使用 pChart 绘制的直方图

The above is the detailed content of Draw graphics in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:lsjlt.com
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!