iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > PHP编程 >在 PHP 中绘制图形
  • 455
分享到

在 PHP 中绘制图形

2024-02-27 21:02:00 455人浏览 独家记忆
摘要

本文介绍了如何在 PHP 中使用 pChart 创建图形。第一个是条形图,第二个是样条图,最后一个是来自 Mysql 的直方图。 设置你的环境 在使用 pChart 之前,你首先需要安装 ph

本文介绍了如何在 PHP 中使用 pChart 创建图形。第一个是条形图,第二个是样条图,最后一个是来自 Mysql 的直方图。


设置你的环境

在使用 pChart 之前,你首先需要安装 php5。你可以从 SourceForge 获得 PHP5 作为 XAMPP 5.5.28 的一部分。

当你有 XAMPP 5.5.28 时,从他们的官方网站下载 pChart。之后,将 pChart 提取到 XAMPP 5.5.28 的 htdocs 文件夹中。

打开 pChart 文件夹,其结构应如下图所示:

XAMPP 5.5.28 中的 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 对象为图表创建图像。
  • 设置图表的字体。
  • 使用 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'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'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 '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();
?>

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

使用 pChart 绘制的直方图

--结束END--

本文标题: 在 PHP 中绘制图形

本文链接: https://www.lsjlt.com/news/569103.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 在 PHP 中绘制图形
    本文介绍了如何在 PHP 中使用 pChart 创建图形。第一个是条形图,第二个是样条图,最后一个是来自 MySQL 的直方图。 设置你的环境 在使用 pChart 之前,你首先需要安装 PH...
    99+
    2024-02-27
  • PHP中Grafika如何实现图形绘制
    这篇文章主要介绍了PHP中Grafika如何实现图形绘制,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。1、绘制贝塞尔曲线贝塞尔曲线绘制,需要两个端点,一头一尾,还有两个控制点...
    99+
    2023-06-17
  • 如何在css中绘制特殊图形
    如何在css中绘制特殊图形?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。一、三角形border边框设置代码:width: 300px;height: 300...
    99+
    2023-06-08
  • HTML5中怎么绘制图形
    本篇内容主要讲解“HTML5中怎么绘制图形”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“HTML5中怎么绘制图形”吧! HTML5中...
    99+
    2024-04-02
  • 如何在Android中使用shape 绘制图形
    这篇文章给大家介绍如何在Android中使用shape 绘制图形,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。shape/* * 线行 圆形 矩形/android:shape=&...
    99+
    2023-05-30
    android shape
  • 怎么在python中使用opencv绘制图形
    这篇文章给大家介绍怎么在python中使用opencv绘制图形,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。实现方法1)画线段 cv.line在图片中绘制一段直线# 绘制线段# 参数1:图片#&nb...
    99+
    2023-06-14
  • 5-3 绘制图形
    5-3  绘制图形 本节学习目标: n绘制曲线基本要点 n图形类控件的使用 nSystem.Drawing.Drawing2D 5-3-1 绘制曲线 基本形状的绘制,我们可以从图形...
    99+
    2023-01-31
    图形
  • html5-Canvas如何在web中绘制各种图形
    这篇文章将为大家详细讲解有关html5-Canvas如何在web中绘制各种图形,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。 在html5中...
    99+
    2024-04-02
  • 怎么在Android中实现绘制各种图形
    怎么在Android中实现绘制各种图形?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。首先自定义一个View类,这个view类里面需要一个Paint对象来控制图形的属性,需要...
    99+
    2023-05-30
    android
  • html5如何绘制图形
    这篇文章主要介绍了html5如何绘制图形,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。html5中是怎么实现绘制图形?html5中可以实现绘...
    99+
    2024-04-02
  • java怎么绘制图形
    Java中可以使用AWT和Swing库来绘制图形。1. 使用AWT库绘制图形:- 创建一个继承自`java.awt.Canvas`的...
    99+
    2023-10-07
    java
  • OpenCV绘制图形功能
    本文实例为大家分享了OpenCV绘制图形功能的具体代码,供大家参考,具体内容如下 1、绘制直线 绘制直线函数是cv::line,函数完整形式如下 void line(InputOut...
    99+
    2024-04-02
  • Python绘制三维图形
    需要安装numpy和matplotlib库,我都是pip库安装,这样比较简单。 import numpy as np import matplotlib.pyplot as plt import mpl_toolkits.mpl...
    99+
    2023-01-31
    图形 Python
  • VB.NET如何绘制图形
    这篇文章主要介绍VB.NET如何绘制图形,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!1.创建Graphics对象VB.NET绘制图形需要指定绘图表面。其中,窗体和所有具有Text属性的控件都可以作为绘制图形的表面。...
    99+
    2023-06-17
  • CSS怎么绘制图形
    这篇文章主要介绍了CSS怎么绘制图形的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇CSS怎么绘制图形文章都会有所收获,下面我们一起来看看吧。正方形/长方形<!DOCTYPE html>&l...
    99+
    2023-06-27
  • C#Chart绘制简单图形波形
    本文实例为大家分享了C# Chart绘制简单图形波形的具体代码,供大家参考,具体内容如下 此次用C#绘制波形使用的是Chart控件 1、将Chart控件拖进主界面,然后设置属性。 ...
    99+
    2024-04-02
  • HTML5怎么在canvas中绘制矩形附效果图
    这篇文章主要讲解了“HTML5怎么在canvas中绘制矩形附效果图”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“HTML5怎么在canvas中绘制矩形附效果...
    99+
    2024-04-02
  • Python Pyecharts绘制象形柱图
    目录1.准备工作1.1 导入模块1.2 部分参数2.基础象形图3.自定义图例3.1 图片图例3.2 生成象形图在可视化展示过程中,为了达到更形象的展示效果,我们往往需要自定义一些直观...
    99+
    2024-04-02
  • C++ OpenCV绘制几何图形
    本文实例为大家分享了C++ OpenCV绘制几何图形的具体代码,供大家参考,具体内容如下 绘制几何图形 直线 矩形 多边形 圆形 椭圆 ...
    99+
    2024-04-02
  • Python+OpenCV实现在图像上绘制矩形
    话不多说,直接上代码 import copy import cv2 import numpy as np WIN_NAME = 'draw_rect' class Rec...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作