如何使用cakePHP设置GoogleCharts?

时间:2015-04-20 18:15:42

标签: cakephp charts

我尝试使用Google Chart在cakePHP中显示数据。 我在https://github.com/scottharwell/GoogleCharts下载了GoogleCharts插件并将其放在我的App / Plugin目录中。我使用

在我的应用程序的引导文件中加载所有插件
CakePlugin::loadAll(); 

在我的控制器中,我把

 App::uses('GoogleCharts', 'GoogleCharts.Lib'); 

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

,所以cakePHP能够检测到我将使用这个插件。当然,我在App / View / Helper中创建了GoogleChartHelper.php。

在处理我的数据之前,我想要的只是显示一个图表示例,看它是否正常工作。哪不是!我复制了上面提供的链接的例子(在github.com上),所以这是我的Controller类:

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

    class StatisticsController extends AppController{

    public $helpers = array('GoogleCharts.GoogleCharts');
    public $components = array (
            'Paginator',
            'Session'
        );

    public function index(){
            //Setup data for chart
            $chart = new GoogleCharts();

            $chart->type("LineChart");
            //Options array holds all options for Chart API
            $chart->options(array('title' => "Recent Scores"));
            $chart->columns(array(
                    //Each column key should correspond to a field in your data array
                    'event_date' => array(
                            //Tells the chart what type of data this is
                            'type' => 'string',
                            //The chart label for this column
                            'label' => 'Date'
                    ),
                    'score' => array(
                            'type' => 'number',
                            'label' => 'Score',
                            //Optional NumberFormat pattern
                            'format' => '#,###'
                    )
            ));


        //You can also manually add rows:
        $chart->addRow(array('event_date' => '1/1/2012', 'score' => 55));

        //Set the chart for your view
        $this->set(compact('chart'));
    }
    }

在我看来,我把代码

 <div id="chart_div"><?php $this->GoogleChart->createJsChart($chart);?></div>

(没有&#34; s&#34;到#34; GoogleChart&#34;(不像在下载页面上,在哪里写的&#34; GoogleCharts&#34;),这花了我3个小时到通知)

我的图表应显示在页面上,但我收到以下错误:

  

警告(512):方法GoogleChartHelper :: createJsChart不存在   [CORE \ Cake \ View \ Helper.php,第192行]

(如果我在视图中放置&#34; s&#34;我的页面显示没有任何错误但没有任何图表......)

我错过了什么吗?

N.B。 :我没有复制github页面上给出的第一个方法,因为它出现了一个新的最差错误:

  

错误:在null

上调用成员函数find()

该方法是:

 //Get data from model
    //Get the last 10 rounds for score graph
    $rounds = $this->Round->find(
        'all',
        array(
            'conditions' => array(
                'Round.user_id' => $this->Auth->user('id')
            ),
            'order' => array('Round.event_date' => 'ASC'),
            'limit' => 10,
            'fields' => array(
                'Round.score',
                'Round.event_date'
            )
        )
    );

请帮助我,我只想在图表中显示一些随机数据,它不应该很复杂(但是使用cakePHP,一切看起来都很复杂)...

1 个答案:

答案 0 :(得分:0)

问题在于缺少一步: 你必须写

<?php echo $this->fetch('script'); ?>

在View / Layouts / bootstrap.ctp中,如果您按照所有其他步骤操作,它应该可以正常工作!