首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cakephp 2.0 jpgraph

cakephp 2.0 jpgraph
EN

Stack Overflow用户
提问于 2012-03-30 12:07:56
回答 2查看 2.8K关注 0票数 1

我很难让jpgraph与cakephp一起工作。我有一个叫做"Graphs“的控制器,它所做的就是显示视图。View/Graphs/index.ctp非常简单:

代码语言:javascript
复制
echo "This is an image of my report";
echo "<img src='/<projectbase>/reports/index'></img>";

我相信它试图从索引中获取信息,然后它的视图被称为ReportsController。然后我有了一个ReportsController:

代码语言:javascript
复制
<?php
class ReportsController extends AppController {
    var $name = 'Reports';
    function index() {
        $this->layout='ajax';
    }
}

它只调用报表中的索引视图,并返回ajax信息。然后我有了View/Reports/index.ctp:

代码语言:javascript
复制
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();

现在,基于this link的View/Graphs/index.ctp有一个图像链接,它调用View/Reports/index.ctp并告诉它返回我想要的jpgraph。当我运行这段代码时,我得到了一个错误“资源解释为图像,但使用MIME类型文本/html传输”。如果我直接转到链接(localhost//report/index),它会显示出许多时髦的字符,而PNG就在开头附近。我相信这是从jpgraph生成的二进制文件,所以我相信生成了一些东西,但它没有正确呈现,也没有正确地放入View/Graps/index.ctp中。

我觉得除非我遗漏了一些非常小的东西,否则我从问题中的链接中偷走了这个基本上是逐字的,所以它是不起作用的。我是不是遗漏了什么?在cakephp中有没有更简单的绘图方法?

我的理论是,我是如何从视图中获取数据的,以及App::Vendor()调用在蛋糕php中是如何工作的,这是有些奇怪的。当我告诉一个图像在cakephp结构之外寻找jpgraph时,它会毫无问题地生成它:

代码语言:javascript
复制
echo "<img src='/jpgraph/Examples/example0.php'></img>";

当我直接转到这个页面时,它能够毫无问题地生成图形。

谢谢你的帮助!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-03 02:51:31

好的,我相信我已经找到了一个使用jpgraph的黑客解决方案。问题在于它是如何流式传输的。我要做的是让我的Graphs Controller如下:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?php
echo "<img src='imagefile.png'></img>";
?>

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

票数 2
EN

Stack Overflow用户

发布于 2012-03-30 16:58:58

你应该使用蛋糕Vendor结构,它是详细的in the Cookbook。这将确保您可以访问各种JpGraph函数。

因此,例如,将您的文件放在app/Vendor/jpgraph中,您可以包括主JpGraph文件(如果它名为jpgrah.php),如下所示:

代码语言:javascript
复制
App::import('Vendor', 'jpgraph/jpgraph');

Cake 1.3有一些教程,可能适用于新的2.0版本,this articlethis one。我不能保证这两篇文章的质量,但它应该会给你一些指导。如果出现任何问题,您可以参考2.02.1的迁移指南。

编辑:

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

代码语言:javascript
复制
$this->RequestHandler->respondAs();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9936890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档