abbadon1334/gnuplot

GnuPlot 库 - 由 gregwar/gnuplot 分支而来

0.1.5 2015-03-12 09:29 UTC

This package is auto-updated.

Last update: 2024-09-04 03:39:33 UTC


README

一个用于使用 GnuPlot 的 PHP 库

这是 demo/write.php 的输出

gnuplot

要求

您需要有一个已安装 gnuplot 的服务器,并且安全模式已禁用(以便能够运行 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 and drawing it as a line of connected points, colored in red and smoothed
// (with index 1)
$plot
    ->setTitle(1, 'The first curve')
    ->setLineType(1, 'rgb #ff0000')
    ->setLineMode(1, 'lp')
    ->setLineSmooth(1, GnuPlot::SMOOTH_CSPLINE)
    ->push(0, 8, 1)
    ->push(1, 9, 1)
    ->push(2, 10, 1)
    ;

// Drawing the area between the two curves in blue
$plot
    ->setLineMode(2, GnuPlot::LINEMODE_FILLEDCURVES)
    ->setLineType(2, 'rgb #0000ff')
    ->setTitle(2, 'Area')
    ->push(0, [4, 8], 2)
    ->push(1, [5, 9], 2)
    ->push(2, [6,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 个曲线添加一个点(如果 linemode 是 GnuPlot::LINEMODE_FILLEDCURVES,则 $y 可以是数组)
  • display(),在屏幕上渲染图表(假设您正在使用 CLI 与 X 服务器一起使用)
  • refresh(),与 display() 相同,但在第一次调用后重新绘制图表
  • get(),获取图像的 PNG 数据
  • writePng($filename),将数据写入输出 PNG 文件
  • writePDF($filename),将数据写入输出 PDF 文件
  • writeEPS($filename),将数据写入输出 EPS 文件
  • setTitle($index, $title),设置第 $index 个曲线的标题
  • setLineWidth($index, $width),设置第 $index 个曲线的宽度
  • setLineMode($index, $mode),设置第 $index 个曲线的线模式(设置为 GnuPlot::LINEMODE_FILLEDCURVES 以填充两条线之间的区域)
  • setLinePoint($index, $point),设置第 $index 个曲线的线点
  • setLineType($index, $type),设置第 $index 个曲线的线类型
  • setLineColor($index, $color),设置第 $index 个曲线的颜色
  • setLineSmooth($index, $smooth),设置第 $index 个曲线的平滑类型。可用的平滑类型为 SMOOTH_NONESMOOTH_BEZIERSMOOTH_CSPLINE,在 GnuPlot 类上定义为常量。
  • setGraphTitle($title),设置图表的主要标题
  • setXTimeFormat($format),将 X 轴设置为时间轴并指定数据格式
  • setTimeFormatString($format),指定 X 轴时间表示格式
  • setXLabel($text),设置 X 轴的标签
  • setYLabel($text),设置 Y 轴的标签
  • setYFormat($format),设置 Y 轴格式
  • setXRange($min, $max),设置 X 的最小值和最大值
  • setYRange($min, $max),设置 Y 的最小值和最大值
  • setXTics($tics),设置 X 标记
  • setYTics($tics),设置 Y 标记
  • setMXTics($tics),设置微 X 标记
  • setMYTics($tics),设置微 Y 标记
  • setMinorGrid($status),启用/禁用微标记的网格
  • setGridPlacement($layer),设置网格的位置,可以是 GnuPlot::GRID_DEFAULTGnuPlot::GRID_FRONTGnuPlot::GRID_BACK
  • setWidth($width),设置图表的宽度
  • setHeight($height),设置图表的高度
  • setCanvasWidth($width),设置画布的宽度(如果没有设置,则使用宽度值)
  • setCanvasHeight($height),设置画布的高度(如果没有设置,则使用高度值)
  • setOrigin($x, $y),设置图表的起点
  • setSleepTime($sleepTime),设置保存文件后的睡眠时间
  • addLabel($x, $y, $text),在指定点添加标签
  • flush(),完全清除内部状态并将对象重置为其初始状态

许可证

Gregwar\GnuPlot 使用 MIT 许可证