我试图用谷歌图表在cakePHP中显示数据。我在GoogleCharts上下载了https://github.com/scottharwell/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::createJs图表不存在CORE\Cake\View\Helper.php,第192行
(如果我把"s“放在视图中,我的页面显示得就像没有任何错误,但没有任何图表……)
我是不是遗漏了什么?
N.B. : I没有复制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,一切看起来都很复杂)。
发布于 2015-04-21 13:58:14
问题是缺少了一个步骤:你必须写
<?php echo $this->fetch('script'); ?>在View/Layouts/bootstrap.ctp中,如果您遵循了所有其他步骤,它应该可以工作!
https://stackoverflow.com/questions/29755390
复制相似问题