首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何用GoogleCharts设置cakePHP?

如何用GoogleCharts设置cakePHP?
EN

Stack Overflow用户
提问于 2015-04-20 18:15:41
回答 1查看 1.2K关注 0票数 2

我试图用谷歌图表在cakePHP中显示数据。我在GoogleCharts上下载了https://github.com/scottharwell/GoogleCharts插件,并将其放在App/Plugin目录中。我在应用程序的引导文件中加载所有插件

代码语言:javascript
复制
CakePlugin::loadAll(); 

在我的主计长,我把

代码语言:javascript
复制
 App::uses('GoogleCharts', 'GoogleCharts.Lib'); 

代码语言:javascript
复制
public $helpers = array('GoogleCharts.GoogleCharts');

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

在处理我的数据之前,我想要的只是显示一个图表示例,看看它是否有效。但事实并非如此!我复制了上面提供的链接的示例(在github.com上),下面是我的Controller类:

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

在我看来,我把代码

代码语言:javascript
复制
 <div id="chart_div"><?php $this->GoogleChart->createJsChart($chart);?></div>

(没有"s“到"GoogleChart”(不像下载页面上写的"GoogleCharts"),我花了3个小时才注意到)

我的图表应该显示在页面上,但是我得到了以下错误:

警告(512):方法GoogleChartHelper::createJs图表不存在CORE\Cake\View\Helper.php,第192行

(如果我把"s“放在视图中,我的页面显示得就像没有任何错误,但没有任何图表……)

我是不是遗漏了什么?

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

错误:在null上调用成员函数find()

这种方法是:

代码语言:javascript
复制
 //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,一切看起来都很复杂)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-21 13:58:14

问题是缺少了一个步骤:你必须写

代码语言:javascript
复制
<?php echo $this->fetch('script'); ?>

在View/Layouts/bootstrap.ctp中,如果您遵循了所有其他步骤,它应该可以工作!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29755390

复制
相关文章

相似问题

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