我很难让 jpgraph 与 cakephp 一起工作。我有一个名为“Graphs”的控制器,它所做的只是显示视图。View/Graphs/index.ctp 很简单:
echo "This is an image of my report";
echo "<img src='/<projectbase>/reports/index'></img>";
我相信它试图从 ReportsController 中获取信息,然后是它的名为 index 的视图。然后我有一个 ReportsController:
<?php
class ReportsController extends AppController {
var $name = 'Reports';
function index() {
$this->layout='ajax';
}
}
它只是调用 Reports 中的索引视图,并返回 ajax 信息。然后我有 View/Reports/index.ctp:
App::import('Vendor', 'jpgraph/jpgraph');
App::import('Vendor', 'jpgraph/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();
现在基于这个链接View/Graphs/index.ctp 有一个图像链接,它调用 View/Reports/index.ctp 并告诉它返回我想要的 jpgraph。当我运行此代码时,我收到错误“资源解释为图像但使用 MIME 类型文本/html 传输”。如果我直接转到链接(localhost//reports/index),它会吐出很多时髦的字符,而 PNG 就在开头。我相信这是从 jpgraph 东西生成的二进制文件,所以我相信正在生成一些东西,但它没有被正确渲染,也没有正确地带入 View/Graps/index.ctp。
我觉得除非我遗漏了一些非常小的东西,否则我基本上是从问题中的链接中逐字窃取的,所以它很烦人,它不起作用。我错过了什么吗?有没有更简单的方法在 cakephp 中绘图?
我对此的理论是,我如何从视图中获取数据以及 App::Vendor() 调用在 cake php 中的工作方式有些奇怪。当我告诉图像在 jpgraph 的 cakephp 结构之外查看时,它会毫无问题地生成它:
echo "<img src='/jpgraph/Examples/example0.php'></img>";
当我直接进入这个页面时,它能够毫无问题地生成图表。
谢谢您的帮助!