2012-03-30 74 views
1

我很難讓jpgraph與cakephp一起工作。我有一個名爲「Graphs」的控制器,它所做的只是顯示視圖。 View/Graphs/index.ctp非常簡單:cakephp 2.0 jpgraph

echo "This is an image of my report"; 
echo "<img src='/<projectbase>/reports/index'></img>"; 

我相信我試圖從ReportsController獲取信息,然後將其視圖稱爲索引。然後,我有一個ReportsController:

<?php 
class ReportsController extends AppController { 
    var $name = 'Reports'; 
    function index() { 
     $this->layout='ajax'; 
    } 
} 

它只是調用報告中的索引視圖,這將返回ajax信息。然後我查看/報告/ 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(); 

現在基於this link查看/圖形/ index.ctp具有調用查看/報告/ index.ctp並告訴它返回一個圖片鏈接我想要的jpgraph。當我運行這段代碼時,出現一個錯誤:「資源解釋爲圖像,但是使用MIME類型text/html傳輸」。如果我直接進入鏈接(localhost // reports/index),它會吐出許多時髦的字符,並且PNG接近開頭。我相信這是從jpgraph生成的二進制文件,所以我相信SOMETHING正在生成,但它沒有被正確渲染,也沒有正確引入View/Graps/index.ctp。

我覺得,除非我錯過了一些非常小的東西,否則我從這個問題的鏈接中偷取了這個基本上是逐字的,所以它討厭它不工作。我錯過了什麼嗎?有沒有更容易的方式來cakephp陰謀?

我的理論是關於我如何從視圖中抓取數據以及App :: Vendor()調用如何在Cake php中工作,這有點奇怪。當我告訴一個圖像CakePHP的結構外面去找JpGraph的就沒有問題產生的:

echo "<img src='/jpgraph/Examples/example0.php'></img>"; 

,當我去這個頁面直接就能夠產生沒有問題的曲線圖。

感謝您的幫助!

回答

2

好吧我相信我已經到了使用jpgraph的黑客解決方案。問題在於它如何流式傳輸。我做的是我有我下面的圖形控制器:

<?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(); 
} 
} 

我在哪裏調用圖形指數函數,然後調用視圖/圖表/ index.ctp。在上面的控制器中,我調用一個函數generateGraph(),該函數完成該操作並將該圖像存儲到app/webroot中的文件中。然後我有下面的視圖/圖形/ index.ctp:

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

它在我剛剛生成的圖像的app/webroot目錄中查找。我知道這是一種黑客行爲,如果有人知道如何更優雅地做到這一點,我願意嘗試,當我得到一些額外的時間!

0

您應該使用蛋糕Vendor結構,它的詳細信息是in the Cookbook。這將確保您可以訪問各種JpGraph功能。

因此,舉例來說,把你的文件app/Vendor/jpgraph並可以包括主JpGraph的文件(如果它被稱爲jpgrah.php)像這樣:

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

有蛋糕1.3可能適用於一些教程新的2.0情況,this articlethis one。我不能保證兩篇文章的質量,但它應該給你一些指導。如果出現任何問題,您可以參考2.02.1的遷移指南。

編輯:

關於不正確的內容類型;您可以設置內容類型using the RequestHandler in Cake。 Cake默認情況下將內容呈現爲text/html,因此您需要明確設置內容類型。在你的控制器方法中使用respondAs

$this->RequestHandler->respondAs(); 
+0

嗨Mensch,感謝您的快速回復。我曾嘗試過您建議的鏈接,但似乎仍然遇到問題。我編輯了這個問題,以更具體地說明我嘗試過的東西。謝謝您的幫助! – JCR 2012-04-02 03:52:39

+0

我認爲你的更新可能值得自己的問題,因爲它涉及另一個可能不相關的問題。 – mensch 2012-04-02 08:24:02

+0

我已經用一些額外的指針更新了我的答案。 – mensch 2012-04-02 08:35:59