2015-11-08 42 views
0

首先,對不起,如果我要求關於這個問題的幫助,但我已經在這一工作了近一天了。我搜索了這個網站,並能找到類似於我的問題的帖子,但我無法讓它工作。CakePHP 2.7 GoogleChart插件

無論如何,我正在使用CakePHP 2.x,並且需要通過圖表顯示報表。我看到CakePHP CakePHP GoogleCharts Plugin by Scott Harwell的這個插件。我做了一切一步一步,不知何故,我的觀點一直顯得空虛。我確定這不是因爲衝突的文件,所以我決定創建一個新的CakePHP項目,不幸的是,它不起作用。

這裏是我的代碼:

控制器:

<?php 
App::uses('AppController', 'Controller'); 
App::uses('GoogleCharts', 'GoogleCharts.Lib'); 

class ChartsController extends AppController { 

    public $uses = array('Student'); 
    public $helpers = array('GoogleCharts.GoogleCharts'); 


    public function test_chart(){ 
     $student= $this->Student->getStudentAges(); 

     $studentAgesChart= new GoogleCharts(); 
     $studentAgesChart->type('LineChart'); 
     $studentAgesChart->options(array('title' => "Student Ages")); 
     $studentAgesChart->columns(array(
      //Each column key should correspond to a field in your data array 
      'name' => array(
       'type' => 'string',   
       'label' => 'Student Name' 
      ), 
      'age' => array(
       'type' => 'number', 
       'label' => 'Student Age' 
      ) 
     )); 

     foreach($student as $row){ 
      $studentAgesChart->addRow(array('age' => $row['Student']['age'], 'name' => $row['Student']['name'])); 
     } 

     $this->set(compact('studentAgesChart')); 
     debug($studentAgesChart); 
    } 
} 
?> 

查看:

<div id="chart_div" > 
    <?php 
     $this->GoogleCharts->createJsChart($studentAgesChart); 

    ?> 
</div> 

調試($ studentAgesChart):

object(GoogleCharts) { 
    [private] type => 'LineChart' 
    [private] columns => array(
     'name' => array(
      'type' => 'string', 
      'label' => 'Student Name' 
     ), 
     'age' => array(
      'type' => 'number', 
      'label' => 'Student Age' 
     ) 
    ) 
    [private] rows => array(
     (int) 0 => array(
      (int) 0 => 'Student 1', 
      (int) 1 => '17' 
     ), 
     (int) 1 => array(
      (int) 0 => 'Student 2', 
      (int) 1 => '16' 
     ) 
    ) 
    [private] options => array(
     'width' => (int) 400, 
     'height' => (int) 300, 
     'title' => 'Student Ages', 
     'titleTextStyle' => array(
      'color' => 'red' 
     ) 
    ) 
    [private] callbacks => array() 
    [private] div => 'chart_div' 
} 

型號:

public function getStudentAges(){ 
     $student_ages = $this->find('all', 
      array(
       'order' => array('Student.name' => 'ASC'), 
       'limit' => 3, 
       'fields' => array(
        'Student.name', 
        'Student.age' 
       ) 
      ) 
     ); 

     return $student_ages; 
    } 

我看到它的方式,它不包含任何錯誤,但我的觀點是空的。

回答

0

@Scrappy,當您在頁面上查看源代碼時,您是否看到DOM中包含任何輸出(JavaScript或HTML)?或者,該插件是否根本不生成任何內容?如果您確實看到添加了JavaScript代碼,那麼您的頁面是否會報告控制檯中可能會阻止圖表正確加載的JS錯誤?如果你沒有看到JS代碼,那麼你可能還沒有在引導文件中加載插件。

該頁面是否可在公共Internet上查看?

這個插件在幾年內沒有看到我的更新,但也有一些開發者已經爲它分發並更新了CakePHP 3.0。但是,這個版本應該仍然有效,除非谷歌的一些事情發生了變化,如果確實如此,我肯定會聽說這個消息。