如何使用cakePHP设置GoogleCharts?
我试图用谷歌图表用cakePHP显示数据。 我下载了GoogleCharts插件,并将其放在我的App/plugin目录中。我用如何使用cakePHP设置GoogleCharts?,cakephp,charts,Cakephp,Charts,我试图用谷歌图表用cakePHP显示数据。 我下载了GoogleCharts插件,并将其放在我的App/plugin目录中。我用 CakePlugin::loadAll(); 在我的控制器中,我将 App::uses('GoogleCharts', 'GoogleCharts.Lib'); 及 ,因此cakePHP能够检测到我将使用此插件。当然,我在App/View/Helper中创建了GoogleChartHelper.php 在处理数据之前,我只想显示一个图表示例,看看它是否正常工
CakePlugin::loadAll();
在我的控制器中,我将
App::uses('GoogleCharts', 'GoogleCharts.Lib');
及
,因此cakePHP能够检测到我将使用此插件。当然,我在App/View/Helper中创建了GoogleChartHelper.php
在处理数据之前,我只想显示一个图表示例,看看它是否正常工作。但事实并非如此!我复制了上面提供的链接的示例(在github.com上),因此下面是我的控制器类:
<?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'));
}
}
请帮帮我,我只想在图表中显示一些随机数据,它不应该很复杂(但使用cakePHP,一切似乎都很复杂).问题是缺少一个步骤: 你必须写作
<?php echo $this->fetch('script'); ?>
在View/Layouts/bootstrap.ctp中,如果您遵循了所有其他步骤,它应该可以工作 必须使用
$this->GoogleCharts->createJsChart
(使用S)调用帮助程序。如果没有显示任何内容,可能是您的输入有问题,或者您的浏览器正在阻止(远程)脚本?谢谢,至少我知道如何正确调用它(使用“s”)。这不是我的浏览器阻止脚本,因为我可以在其他网站上显示其他谷歌图表,我的同事在他的计算机上尝试了我的代码,但遇到了同样的问题。。。你所说的“你的输入有问题”是什么意思?
<div id="chart_div"><?php $this->GoogleChart->createJsChart($chart);?></div>
//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'
)
)
);
<?php echo $this->fetch('script'); ?>