本文目录导读:

PHP 流程可视化主要有以下几种实现方式,我为你整理了从简单到复杂的方案:
流程图生成库
Graphviz
最经典的流程图生成工具,通过代码描述图形结构。
<?php
// 使用 graphviz/php-graphviz 库
use Graphviz\Digraph;
$graph = new Digraph('G');
$graph->node('start', ['label' => '开始']);
$graph->node('process', ['label' => '处理数据']);
$graph->node('end', ['label' => '结束']);
$graph->edge('start', 'process');
$graph->edge('process', 'end');
echo $graph->render('svg'); // 输出可视化结果
Mermaid.js
通过简单的文本描述生成流程图,非常适合快速可视化。
<?php
// 生成 Mermaid 代码
$mermaidCode = "
graph TD
A[开始] --> B{判断条件}
B -->|是| C[处理1]
B -->|否| D[处理2]
C --> E[结束]
D --> E
";
// 在前端渲染
echo "<div class='mermaid'>" . $mermaidCode . "</div>";
// 引入 Mermaid JS 库
echo "<script src='https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js'></script>";
echo "<script>mermaid.initialize({startOnLoad: true});</script>";
PhpSpreadsheet + 前端图表库
结合表格数据和前端可视化:
<?php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
// 处理数据
$data = [
['步骤', '状态', '耗时'],
['开始', '完成', 1],
['数据处理', '进行中', 3],
['验证', '等待', 2]
];
// 转换为 JSON 供前端使用
$jsonData = json_encode($data);
?>
工作流引擎可视化
PHP Workflow Engine + 第三方库
<?php
use Symfony\Component\Workflow\Workflow;
use Symfony\Component\Workflow\DefinitionBuilder;
use Symfony\Component\Workflow\MarkingStore\MethodMarkingStore;
// 定义工作流
$definitionBuilder = new DefinitionBuilder();
$definition = $definitionBuilder
->addPlaces(['draft', 'review', 'approved', 'rejected'])
->addTransition('submit', 'draft', 'review')
->addTransition('approve', 'review', 'approved')
->addTransition('reject', 'review', 'rejected')
->build();
// 生成可视化图表
$workflow = new Workflow($definition, new MethodMarkingStore());
复杂的交互式流程图
使用 D3.js / Cytoscape.js
<?php
// 后端生成数据
$nodes = [
['id' => 'start', 'label' => '开始', 'type' => 'start'],
['id' => 'process1', 'label' => '处理数据', 'type' => 'process'],
['id' => 'decision', 'label' => '是否有效?', 'type' => 'decision'],
['id' => 'end', 'label' => '结束', 'type' => 'end']
];
$edges = [
['source' => 'start', 'target' => 'process1'],
['source' => 'process1', 'target' => 'decision'],
['source' => 'decision', 'target' => 'end', 'label' => '是'],
['source' => 'decision', 'target' => 'process1', 'label' => '否']
];
// 输出 JSON 格式数据
header('Content-Type: application/json');
echo json_encode(['nodes' => $nodes, 'edges' => $edges]);
?>
实时执行状态可视化
WebSocket + 流程图
<?php
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class FlowVisualizer implements MessageComponentInterface {
public function onMessage(ConnectionInterface $from, $msg) {
$data = json_decode($msg, true);
// 更新节点状态
$currentStep = $data['current_step'];
$status = $data['status'];
// 广播状态更新
$response = [
'type' => 'status_update',
'node_id' => $currentStep,
'status' => $status
];
$from->send(json_encode($response));
}
}
?>
完整的示例项目
<?php
// flow-visualizer.php
class FlowVisualizer {
private $steps = [];
private $transitions = [];
public function addStep($id, $name, $type = 'process') {
$this->steps[$id] = [
'id' => $id,
'name' => $name,
'type' => $type,
'status' => 'pending'
];
}
public function addTransition($from, $to) {
$this->transitions[] = [
'from' => $from,
'to' => $to
];
}
public function renderSVG() {
// 生成 SVG 格式的流程图
$y = 50;
$svg = '<svg width="800" height="600">';
foreach ($this->steps as $step) {
$svg .= "
<g id='node_{$step['id']}'>
<rect x='50' y='{$y}' width='100' height='40' fill='1e90ff'/>
<text x='100' y='" . ($y + 25) . "' text-anchor='middle' fill='white'>
{$step['name']}
</text>
</g>
";
$y += 80;
}
$svg .= '</svg>';
return $svg;
}
public function updateStatus($stepId, $status) {
$this->steps[$stepId]['status'] = $status;
// 广播状态更新
}
}
// 使用示例
$flow = new FlowVisualizer();
$flow->addStep('start', '开始', 'start');
$flow->addStep('process', '数据处理');
$flow->addStep('decision', '验证', 'decision');
$flow->addTransition('start', 'process');
$flow->addTransition('process', 'decision');
echo $flow->renderSVG();
?>
推荐的前端库
| 库名称 | 特点 | 适用场景 |
|---|---|---|
| Mermaid.js | 简单易用,代码转换为图形 | 快速建模、文档 |
| Flowchart.js | 轻量级,专为流程图设计 | 简单流程展示 |
| AntV X6 | 功能强大,支持交互 | 复杂业务流程图 |
| Cytoscape.js | 适合网络图和复杂图形 | 复杂关系可视化 |
| Drawflow.js | 可视化编辑器,支持拖拽 | 流程图编辑 |
总结建议:
- 快速原型:使用 Mermaid.js(3分钟上手)
- 集成到现有系统:考虑 Graphviz(成熟稳定)
- 复杂交互流程:使用 D3.js 或 Cytoscape.js
- 实时监控:WebSocket + 图标库组合
选择建议:
- 如果只是展示静态流程 → 用 Mermaid.js
- 如果需要交互操作(拖拽、编辑)→ 用 AntV X6 或 Drawflow
- 如果要嵌入到完整的项目管理系统中 → 考虑商业组件如 GoJS
有问题可以继续追问,我可以提供具体的实现代码示例。