gregwar/gnuplot

GNUPlot 库

v1.0.2 2022-10-12 15:15 UTC

This package is auto-updated.

Last update: 2024-09-13 14:31:59 UTC


README

用于使用 GNUPlot 的 PHP 库

警告:此库会调用 gnuplot 命令行作为后端,可能导致任意代码执行。如果您打算使用此库处理用户提供的信息,请务必小心。有关更多信息,请参阅 这篇帖子

这是 demo/write.php 的输出

gnuplot

需求

您需要一个已安装 GNUPlot 并已禁用安全模式的 Web 服务器(以便能够运行 proc_open()

使用方法

demo/ 目录中有示例。

您可以根据以下方式创建图表并填充它

<?php

use Gregwar\GnuPlot\GnuPlot;

$plot = new GnuPlot;

// Setting the main graph title
$plot->setGraphTitle('Demo graph');

// Adding three points to the first curve
$plot
    ->setTitle(0, 'The first curve')
    ->push(0, 4)
    ->push(1, 5)
    ->push(2, 6)
    ;

// Adding three points on the other curve
// (with index 1)
$plot
    ->setTitle(1, 'The first curve')
    ->push(0, 8, 1)
    ->push(1, 9, 1)
    ->push(2, 10, 2)
    ;

然后您可以将其保存到文件中,例如查看 write.php

<?php

// Write the graph to out.png
$plot->writePng('out.png');

或者直接在浏览器中渲染它,例如尝试 out.php

<?php

header('Content-type: image/png');
echo $plot->get();

或者将其显示在屏幕上(适用于 CLI 脚本),例如运行 demo.php 脚本

<?php

$plot->display();

或者显示它,并实时重新馈入它(适用于 CLI 脚本),例如运行 realTime.php

<?php

$plot->refresh();

API

  • push($x, $y, $index=0),向第 $index 个曲线添加一个点
  • display(),在屏幕上渲染图表(假设您正在使用 CLI 并带有 X 服务器
  • refresh(),与 display() 相同,但在第一次调用后重新绘制图表
  • get(),获取图像的 PNG 数据
  • writePng($filename),将数据写入输出文件
  • setTitle($index, $title),设置第 $index 个曲线的标题
  • setGraphTitle($title),设置图表的主标题
  • setXTimeFormat($format),将 X 轴设置为时间轴并指定数据格式
  • setXTimeFormatString($format),指定 X 轴时间表示格式
  • setXLabel($text),设置 X 轴的标签
  • setYLabel($text),设置 Y 轴的标签
  • setYFormat($format),设置 Y 轴格式
  • setXRange($min, $max),设置 X 的最小值和最大值
  • setYRange($min, $max),设置 Y 的最小值和最大值
  • setWidth($width),设置图表的宽度
  • setHeight($height),设置图表的高度
  • addLabel($x, $y, $text),在某个点添加一些标签

许可证

Gregwar\GnuPlot 使用 MIT 许可证