Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Lukis grafik dalam PHP

Lukis grafik dalam PHP

WBOY
WBOYke hadapan
2024-02-29 17:00:471266semak imbas

PHP Editor Shinichi membawakan anda panduan terperinci tentang lukisan grafik dalam PHP. Sama ada anda melukis bentuk geometri asas atau mencipta carta visualisasi data yang kompleks, PHP menyediakan keupayaan pemprosesan grafik yang berkuasa. Artikel ini akan memperkenalkan cara menggunakan perpustakaan GD PHP dan alatan lain untuk menjana pelbagai grafik dalam halaman web secara dinamik, dan meneroka beberapa petua dan teknologi praktikal. Mari kita terokai dunia menarik grafik dalam PHP!


Sediakan persekitaran anda

Sebelum menggunakan pChart, anda perlu memasang php5 dahulu. Anda boleh mendapatkan PHP5 sebagai sebahagian daripada XAMPP 5.5.28 daripada SourceForge.

Apabila anda mempunyai XAMPP 5.5.28, muat turun dari laman web rasmi mereka pChart。之后,将 pChart 提取到 XAMPP 5.5.28 的 htdocs. Selepas itu, ekstrak

ke dalam folder htdocs XAMPP 5.5.28.

pChart Buka folder

, strukturnya sepatutnya kelihatan seperti imej di bawah:

在 PHP 中绘制图形

Nota:
  • class
  • Folder mengandungi definisi kelas yang akan kami gunakan.
  • fonts
  • Folder mengandungi fail fon yang boleh kami gunakan dalam gambar rajah kami.

pChart Selepas melengkapkan persediaan

, anda kini boleh mula melukis.

pChart Lukis carta bar menggunakan

dalam PHP

pChart 绘制条形图的 PHP 代码必须包含 class Kod PHP untuk melukis carta bar menggunakan

mesti mengandungi tiga fail dalam folder
    . Fail-fail ini ialah:
  • pData.class.php
  • pImage.class.php
  • pDraw.class.php

pData.class.php 允许你加载将在图表中使用的数据。你需要 pDraw.class.php

Dalam fail ini,

membolehkan anda memuatkan data yang akan digunakan dalam carta. Anda perlu pImage.class.php 将让你在 WEB 浏览器中呈现图表。你必须使用 PHP required_once() untuk melukis graf.

Seterusnya, PCART_PATH 常量。然后使用 set_include_path(),你可以为 pChart akan membenarkan anda memaparkan carta dalam penyemak imbas

WEB

anda. Anda mesti menggunakan PHP required_once() untuk memasukkan fail ini. pChart

Anda boleh memasukkannya menggunakan laluan relatif atau menentukan pemalar
    . Kemudian menggunakan set_include_path() anda boleh menggunakan nama direktori pendek untuk
  • kelas. PCART_PATH
  • Setelah berkata demikian, kita boleh mencipta carta bar dengan
  • menggunakan langkah berikut: set_include_path() 作为 pChart
  • Tentukan required_once() 包含 pChart pemalar.
  • Gunakan set_include_path() sebagai nama direktori pendek untuk kelas pData.
  • Gunakan required_once() untuk memasukkan kelas
  • .
  • addPoints 方法将数据添加到 pData Buat objek
  • baharu.
  • pImage Cipta data anda atau importnya.
  • Gunakan kaedah addPoints untuk menambah data pada objek
  • .
  • pDatasetGraphArea Gunakan
  • objek untuk mencipta imej untuk carta.
  • pDatadrawScaledrawBarChart Tetapkan fon carta.
  • Gunakan kaedah setGraphArea
  • untuk menetapkan kawasan graf.
  • pDataRender 方法渲染图像。确保将 null 传递给 Render Gunakan kaedah drawScale dan drawBarChart
  • untuk melukis skala dan bar.

Hantar maklumat pengepala untuk memberitahu penyemak imbas bahawa anda sedang menghantar imej.

Gunakan kaedah Render

untuk memaparkan imej. Pastikan untuk menghantar null kepada kaedah Render.

使用 pChart 绘制的条形图

Di bawah ialah pelaksanaan langkah-langkah ini. Di bawah ialah imej output dalam 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);
?>

Keluaran: pChart

drawSplineChart

Lukis spline menggunakan pDataStroke dalam PHP

Proses melukis spline adalah sama seperti melukis bar, kecuali anda menggunakan kaedah pChart 绘制样条图。此外,我们使用的是 fonts 目录中的 MankSans.ttf untuk melukis spline. Selain itu, anda boleh memilih untuk tidak menghantar carta sebagai imej.

Sebaliknya, anda boleh memilih kaedah Stroke

untuk memaparkan carta dalam penyemak imbas web.

用 pChart 绘制的样条图 Kod berikut menggunakan

untuk melukis spline. Selain itu, kami menggunakan fon MankSans.ttf daripada direktori
.

<?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();
?>
Keluaran:

Lukis carta bar daripada 🎜mysql🎜 🎜pangkalan data🎜 dalam PHP🎜 🎜 Melukis histogram mengikut langkah yang serupa dengan petak bar dan spline. Walau bagaimanapun, terdapat beberapa perbezaan yang patut ditunjukkan. 🎜

首先,直方图的数据将来自 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 绘制的直方图

Atas ialah kandungan terperinci Lukis grafik dalam PHP. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:lsjlt.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam