1

我很难让 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>";

当我直接进入这个页面时,它能够毫无问题地生成图表。

谢谢您的帮助!

4

2 回答 2

2

好吧,我相信我已经找到了使用 jpgraph 的 hack 解决方案。问题在于它是如何流式传输的。我所做的是我在下面有我的图形控制器:

<?php
class GraphsController extends AppController {
var $name = 'Graphs';

function index() {
    // call Reports view to generate new graph
    //$var = ClassRegistry::init('Reports')->index();
    //$this->set(compact('var'));
    $this->generateGraph();
}

/*
 * This function generates the grph to be displayed.  It is a little bit of a hack:
 * I save the image to a file, then in the index.ctp I extract that image.  For now,
 * that is the only way I can get jpgraph to work.
 */
function generateGraph() {
    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);

    // Get the handler to prevent the library from sending the
    // image to the browser
    $gdImgHandler = $graph->Stroke(_IMG_HANDLER);

    // Stroke image to a file

    // Default is PNG so use ".png" as suffix
    $fileName = "imagefile.png";
    $graph->img->Stream($fileName);

    // Send it back to browser
    //$graph->img->Headers();
    //$graph->img->Stream();
}
}

我在这里调用 Graphs 索引函数,然后调用 View/Graphs/index.ctp。在上面的控制器中,我调用了一个函数 generateGraph(),它正是这样做的,并将图像存储到 app/webroot 中的一个文件中。然后我有下面的 View/Graphs/index.ctp:

<?php
echo "<img src='imagefile.png'></img>";
?>

它在 app/webroot 目录中查找我刚刚生成的图像。我知道这是一个 hack,如果有人知道如何更优雅地做到这一点,我愿意在有额外时间的时候尝试一下!

于 2012-04-02T18:51:30.923 回答
0

您应该使用 CakeVendor结构,它在 Cookbook 中有详细说明。这将确保您可以访问各种 JpGraph 功能。

因此,例如,将您的文件放入app/Vendor/jpgraph其中,您可以像这样包含主 JpGraph 文件(如果它被调用jpgrah.php):

App::import('Vendor', 'jpgraph/jpgraph');

有一些关于 Cake 1.3 的教程可能适用于新的 2.0 情况,这篇文章这篇文章。我不能保证这两篇文章的质量,但它应该会给你一些方向。如果出现任何问题,您可以查阅2.02.1的迁移指南。

编辑:

关于不正确的内容类型;您可以使用 Cake 中的 RequestHandler设置内容类型。Cake 默认将内容呈现为 text/html,因此您需要显式设置 content-type。respondAs在您的控制器方法中使用。

$this->RequestHandler->respondAs();
于 2012-03-30T08:58:57.540 回答