PHP 怎么PHP 调用图

wen PHP项目 1

本文目录导读:

PHP 怎么PHP 调用图

  1. 使用 PHP 图表库生成静态图表(图片)
  2. 使用 Composer 安装现代 PHP 图表库
  3. 前端图表库 + PHP 提供数据(最流行方式)
  4. 调用系统命令行绘图工具(如 gnuplot/Graphviz)
  5. 使用 GD / Imagick 图形库(极简绘图)
  6. 选择建议

在 PHP 中调用图表(生成或渲染图表)通常有以下几种常见方式,具体取决于你的需求(是生成图片、前端交互图表,还是调用外部绘图工具)。


使用 PHP 图表库生成静态图表(图片)

这些库纯 PHP 生成,不依赖前端 JavaScript,适合生成报表、PDF 或邮件中的图片。

常用库:

  • JpGraph – 老牌 PHP 图表库,支持柱状图、折线图、饼图等
  • pChart – 轻量级,支持多种图表类型
  • PHPlot – 简单易用,适合快速开发

示例(JpGraph):

require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');
$data = [20, 35, 30, 40, 25]; // 数据
$graph = new Graph(400, 300);
$graph->SetScale("textlin");
$bar = new BarPlot($data);
$bar->SetFillColor("blue");
$graph->Add($bar);
// 输出图片
$graph->Stroke();

注意:JpGraph 需要下载并引入,最新版已改为 Composer 安装。


使用 Composer 安装现代 PHP 图表库

推荐通过 composer 安装,更方便管理。

推荐库:

  • Chartisan – 后端生成图表配置,前端用 Chart.js 渲染
  • CConsole (Console支持图表,主要用于 CLI)

示例(使用 Chartisan + Chart.js):

// 后端 PHP (Laravel 或原生)
$chart = new \Chartisan\Chartisan();
$chart->labels(['Jan', 'Feb', 'Mar']);
$chart->dataset('Sales', [100, 200, 150]);
return response()->json($chart->toObject());
// 前端 HTML + JS
// <canvas id="myChart"></canvas>
// new Chart(document.getElementById('myChart'), chartData);

前端图表库 + PHP 提供数据(最流行方式)

PHP 只作为 API 提供 JSON 数据,前端使用 JavaScript 图表库渲染。

常用前端库:

  • Chart.js – 轻量、易用
  • ECharts (百度) – 功能强大,适合复杂图表
  • Highcharts – 商业库,但免费供非商业项目
  • D3.js – 高度自定义,学习曲线陡

示例流程:

  1. PHP 接口返回 JSON 数据

    // api.php
    header('Content-Type: application/json');
    $data = [
     'labels' => ['一月', '二月', '三月'],
     'datasets' => [
         [
             'label' => '销售额',
             'data' => [120, 200, 150],
             'backgroundColor' => 'rgba(54, 162, 235, 0.5)'
         ]
     ]
    ];
    echo json_encode($data);
  2. 前端 HTML 调用并渲染

    <!-- index.html -->
    <canvas id="myChart" width="400" height="200"></canvas>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script>
    fetch('api.php')
     .then(res => res.json())
     .then(data => {
         new Chart(document.getElementById('myChart'), {
             type: 'bar',
             data: data
         });
     });
    </script>

调用系统命令行绘图工具(如 gnuplot/Graphviz)

PHP 无法直接生成某些特殊图表,可以调用外部工具。

示例(调用 gnuplot):

$data = "1 2\n2 4\n3 6\n";
file_put_contents('/tmp/data.txt', $data);
// 生成 gnuplot 脚本
$script = "set terminal png\nset output 'chart.png'\nplot '/tmp/data.txt' with lines\n";
file_put_contents('/tmp/plot.gp', $script);
// 执行命令
exec('gnuplot /tmp/plot.gp 2>&1', $output, $return_var);
if ($return_var === 0) {
    echo '<img src="chart.png" />';
} else {
    echo '生成失败: ' . implode("\n", $output);
}

使用 GD / Imagick 图形库(极简绘图)

适合简单柱状图、折线图等不需要第三方库的场景。

示例(GD 绘制柱状图):

$width = 400;
$height = 300;
$img = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($img, 255, 255, 255);
$blue = imagecolorallocate($img, 0, 0, 255);
$red = imagecolorallocate($img, 255, 0, 0);
imagefill($img, 0, 0, $white);
$data = [50, 120, 80, 200, 150];
$barWidth = 50;
$startX = 30;
foreach ($data as $i => $value) {
    $x = $startX + $i * ($barWidth + 10);
    $h = $value;
    imagefilledrectangle($img, $x, $height - $h, $x + $barWidth, $height - 10, $blue);
    imagestring($img, 3, $x + 10, $height - 25, $value, $red);
}
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);

选择建议

场景 推荐方案
需要静态图片(PDF/邮件) JpGraph / pChart / GD
动态 Web 页面图表 PHP 提供 JSON + Chart.js / ECharts
复杂专业图表 ECharts / Highcharts
需要自定义渲染 D3.js
服务器端无前端 Chartisan + Chart.js 渲染

如果你能详细说明你的使用场景(比如生成报表图片、嵌入网页、移动端等),我可以给出更具体的代码示例。

抱歉,评论功能暂时关闭!