【发布时间】:2015-06-20 23:01:04
【问题描述】:
我正在尝试使用 Google 图表在 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>
(没有“s”到“GoogleChart”(不像下载页面上写的“GoogleCharts”),这花了我3个小时才注意到)
我的图表应该显示在页面上,但出现以下错误:
警告 (512):方法 GoogleChartHelper::createJsChart 不存在 [CORE\Cake\View\Helper.php,第 192 行]
(如果我把“s”放在视图中,我的页面显示就像没有任何错误但没有任何图表......)
我错过了什么吗?
注意:我没有复制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,一切似乎都很复杂)...
【问题讨论】:
-
助手绝对应该用
$this->GoogleCharts->createJsChart(带S)调用。如果这没有显示任何内容,可能是您的输入有问题,或者您的浏览器正在阻止(远程)脚本? -
谢谢,至少我知道如何正确调用它(使用“s”)。这不是我的浏览器阻止脚本,因为我可以在其他网站上显示其他谷歌图表,而我的同事在他的计算机上尝试使用我的代码并遇到了同样的问题......你所说的“你的输入有问题”是什么意思?