如何使用 cakePHP 设置 GoogleCharts?
How to set up GoogleCharts with cakePHP?
我正在尝试使用 Google 图表在 cakePHP 中显示数据。
我在 https://github.com/scottharwell/GoogleCharts 上下载了 GoogleCharts 插件并将其放在我的 App/Plugin 目录中。我使用
在应用程序的 bootstrap 文件中加载所有插件
CakePlugin::loadAll();
在我的控制器中,我把
App::uses('GoogleCharts', 'GoogleCharts.Lib');
和
public $helpers = array('GoogleCharts.GoogleCharts');
,所以 cakePHP 能够检测到我将使用这个插件。当然,我在 App/View/Helper.
中创建了 GoogleChartHelper.php
在处理我的数据之前,我只想显示一个图表示例以查看它是否有效。哪个不是!我复制了上面提供的例子link(在github.com),所以这是我的控制器class:
<?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个小时才注意到)
我的图表应该显示在页面上,但出现以下错误:
Warning (512): Method GoogleChartHelper::createJsChart does not exist
[CORE\Cake\View\Helper.php, line 192]
(如果我将 "s" 放在视图中,我的页面显示没有任何错误但没有任何图表...)
我是不是漏掉了什么?
N.B。 : 我没有复制 github 页面上给出的第一个方法,因为它出现了一个新的最严重的错误:
Error: Call to a member function find() on null
那个方法是:
//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,一切看起来都很复杂)...
问题是少了一步:
你必须写
<?php echo $this->fetch('script'); ?>
在 View/Layouts/bootstrap.ctp 中,如果您按照所有其他步骤操作,它应该可以工作!
我正在尝试使用 Google 图表在 cakePHP 中显示数据。 我在 https://github.com/scottharwell/GoogleCharts 上下载了 GoogleCharts 插件并将其放在我的 App/Plugin 目录中。我使用
在应用程序的 bootstrap 文件中加载所有插件CakePlugin::loadAll();
在我的控制器中,我把
App::uses('GoogleCharts', 'GoogleCharts.Lib');
和
public $helpers = array('GoogleCharts.GoogleCharts');
,所以 cakePHP 能够检测到我将使用这个插件。当然,我在 App/View/Helper.
中创建了 GoogleChartHelper.php在处理我的数据之前,我只想显示一个图表示例以查看它是否有效。哪个不是!我复制了上面提供的例子link(在github.com),所以这是我的控制器class:
<?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个小时才注意到)
我的图表应该显示在页面上,但出现以下错误:
Warning (512): Method GoogleChartHelper::createJsChart does not exist [CORE\Cake\View\Helper.php, line 192]
(如果我将 "s" 放在视图中,我的页面显示没有任何错误但没有任何图表...)
我是不是漏掉了什么?
N.B。 : 我没有复制 github 页面上给出的第一个方法,因为它出现了一个新的最严重的错误:
Error: Call to a member function find() on null
那个方法是:
//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,一切看起来都很复杂)...
问题是少了一步: 你必须写
<?php echo $this->fetch('script'); ?>
在 View/Layouts/bootstrap.ctp 中,如果您按照所有其他步骤操作,它应该可以工作!