jpgraph是基于PHP语言的图形引擎, 在CakePHP中也可以简单应用来扩展图形方面的编程能力。
比如服务器负载图、用户增长趋势图等等。
1、引入jpgraph组件
从http://www.aditus.nu/jpgraph/ 下载,放在你的cakephp工程的vendor目录下
2、编写绘图代码,如下示例曲线图
<?php
App::import('Vendor', 'jpgraph/src/jpgraph');
App::import('Vendor', 'jpgraph/src/jpgraph_line');
// Some data
$ydata = array(11,3,8,12,5,1,9,13,5,7);
// Create the graph. These two calls are always required
$graph = new Graph(350,250);
$graph->SetScale('textlin');
// Create the linear plot
$lineplot=new LinePlot($ydata);
$lineplot->SetColor('blue');
// Add the plot to the graph
$graph->Add($lineplot);
// Display the graph
$graph->Stroke();
?>
保存为jpline.php
3、在controller中添加jpline action
public function jpline() {
$this->layout = null;
}
4、在其他视图中使用该曲线图
<div class="map">
<h2><?php echo $title_for_layout; ?></h2>
<?php
if($map_engine == 'jpgraph') {
echo "<img src='/alarms/jpline'></img>";
}
?>
</div>
实现效果如下: