php小編新一為您帶來了關于在php中繪制圖形的詳細指南。無論是繪制基本的幾何圖形,還是創建復雜的數據可視化圖表,php都提供了強大的圖形處理功能。本文將介紹如何利用php的gd庫和其他工具來實現在網頁中動態生成各種圖形,并探討一些實用的技巧和技術。讓我們一起來探索在php中繪制圖形的奇妙世界吧!
設置你的環境
在使用 pChart 之前,你首先需要安裝 php5。你可以從 SourceForge 獲得 PHP5 作為 XAMPP 5.5.28 的一部分。
當你有 XAMPP 5.5.28 時,從他們的官方網站下載 pChart。之后,將 pChart 提取到 XAMPP 5.5.28 的 htdocs 文件夾中。
打開 pChart 文件夾,其結構應如下圖所示:
注意:
class 文件夾包含我們將使用的類定義。
fonts 文件夾包含我們可以在圖表中使用的字體文件。
完成 pChart 設置后,你現在可以開始繪圖了。
在 PHP 中使用 pChart 繪制條形圖
使用 pChart 繪制條形圖的 PHP 代碼必須包含 class 文件夾中的三個文件。這些文件是:
pData.class.php
pImage.class.php
pDraw.class.php
在這些文件中,pData.class.php 允許你加載將在圖表中使用的數據。你需要 pDraw.class.php 來繪制圖表。
接下來,pImage.class.php 將讓你在 WEB 瀏覽器中呈現圖表。你必須使用 PHP required_once() 包含這些文件。
你可以使用相對路徑包含它們或定義一個 PCART_PATH 常量。然后使用 set_include_path(),你可以為 pChart 類使用短目錄名稱。
話雖如此,我們可以使用以下步驟創建帶有 pChart 的條形圖:
定義 PCART_PATH 常量。
使用 set_include_path() 作為 pChart 類的短目錄名稱。
使用 required_once() 包含 pChart 類。
創建一個新的 pData 對象。
創建你的數據或將其導入。
使用 addPoints 方法將數據添加到 pData 對象。
使用 pImage 對象為圖表創建圖像。
設置圖表的字體。
使用 pData 的 setGraphArea 方法設置圖形區域。
使用 pData 的 drawScale 和 drawBarChart 方法繪制刻度和條形圖。
發送標頭信息以告訴瀏覽器你正在發送圖像。
使用 pData 的 Render 方法渲染圖像。確保將 null 傳遞給 Render 方法。
以下是這些步驟的實現。以下是 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);
?>
登錄后復制
輸出:
在 PHP 中使用 pChart 繪制樣條圖
繪制樣條圖的過程與繪制條形圖的過程相同,不同之處在于你使用 drawSplineChart 方法繪制樣條圖。此外,你可以選擇不將圖表作為圖像發送。
相反,你可以選擇 pData 的 Stroke 方法在 Web 瀏覽器中呈現圖表。
以下代碼使用 pChart 繪制樣條圖。此外,我們使用的是 fonts 目錄中的 MankSans.ttf 字體。
<?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();
?>
登錄后復制
輸出:
在 PHP 中從 mysql 數據庫中繪制柱狀圖
繪制直方圖遵循與條形圖和樣條圖類似的步驟。但是,有一些差異值得指出。
首先,直方圖的數據將來自 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();
?>
登錄后復制
輸出(你的時間會有所不同):






