首頁  >  文章  >  後端開發  >  在 PHP 中繪製圖形

在 PHP 中繪製圖形

WBOY
WBOY轉載
2024-02-29 17:00:471266瀏覽

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 資料夾,其結構應如下圖所示:

在 PHP 中绘制图形

注意:

  • 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 物件為圖表建立影像。
  • 設定圖表的字體。
  • 使用 pDatasetGraphArea 方法設定圖形區域。
  • 使用 pDatadrawScaledrawBarChart 方法繪製刻度和長條圖。
  • 發送標頭訊息以告訴瀏覽器你正在發送圖像。
  • 使用 pDataRender 方法渲染影像。確保將 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&#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);
?>

輸出:

使用 pChart 绘制的条形图


在 PHP 中使用 pChart 繪製樣條圖

繪製樣條圖的過程與繪製長條圖的過程相同,不同之處在於你使用 drawSplineChart 方法繪製樣條圖。此外,你可以選擇不將圖表作為圖像發送。

相反,你可以選擇 pDataStroke 方法在 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&#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();
?>

輸出:

用 pChart 绘制的样条图


在 PHP 中從 mysql 資料庫中繪製長條圖

繪製直方圖遵循與長條圖和樣條圖類似的步驟。但是,有一些差異值得指出。

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

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

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

CREATE TABLE measures (
timestamp INT NOT NULL DEFAULT &#39;0&#39;,
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 = "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();
?>

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

使用 pChart 绘制的直方图

以上是在 PHP 中繪製圖形的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:lsjlt.com。如有侵權,請聯絡admin@php.cn刪除