CakePp和Google图表插件 [英] CakePhp and Google Charts Plugin
问题描述
您在我的CakePHP应用程序中使用Google Charts插件。
在我的控制器中我有:
有两个函数返回两个图形。
function statistics(){
$ this-> timePerClient
$ this-> timePerProjectChart();
}
FUNCTION timePerProject
function timePerProjectChart(){
$ totalProjeto = $ this-> timePerProject();
$ tempoTotalGasto = $ this-> tempoTotalInvestido();
//图表的设置数据
$ timePerProjectChart = new GoogleChart();
$ timePerProjectChart-> type(PieChart);
$ timePerProjectChart-> options(array('title'=>Percentagem de Tempo(horas)investido por Projeto));
$ timePerProjectChart-> columns(array(
//每个列键应该对应于数据数组中的一个字段
'projects'=> array(
'type' =>'string',
'label'=>'Projeto'
),
'tempoGasto'=> array(
'type'=> ',
'label'=>'%horas'
)
));
//你也可以使用这种方式循环遍历数据并创建数据行:
foreach($ totalProjeto as $ row){
$ percentagemTempoGasto =($ this-> timeToHour row [0] ['tempogasto'])/ $ tempoTotalGasto [0] [0] ['tempogasto'])* 100;
$ timePerProjectChart-> addRow(array('tempoGasto'=> $ percentagemTempoGasto,'projects'=> $ row ['Project'] ['pname'])));
}
//为视图设置图表
$ this-> set('timePerProjectChart',$ timePerProjectChart);
}
在我看来>
< div id =chart_div><?php $ this-> GoogleChart-> createJsChart($ timePerProjectChart);
$ this-> GoogleChart-> createJsChart($ timePerClientChart);
?>< / div>
但我只看不到单个图表。我测试(单独)每个和功能。
我希望将多个图表放在同一个视图上。
有可能吗?
感谢
您的控制器中的统计操作发生了什么? $ this
是一个对象。
使用插件需要几个步骤:'
-
获取数据。
-
设置图表:
$ chart-> type(LineChart);
$ chart-> options(array('title'=>Recent Scores));
$ chart-> columns(array(
//每个列键应该对应数据数组中的一个字段
'event_date'=>数组(
//告诉图表这是什么类型的数据
'type'=>'string',
//此列的图表标签
'label'=>'Date'
),
'score'=> array(
'type'=>'number',
'label'=> Score'
)
)); -
通过循环遍历数据向图表添加行
foreach($ model as $ round){
$ chart-> addRow($ round ['Round']);
} -
为您的视图设置图表 - 这个名称必须在
< div id =chart_div><?php $ this-> createJsChart($ chart);?>< / div>
b
$ b$ this-> set(compact('chart'));
要在单个视图中显示多个图表,您不仅需要使用默认的 chart_div
作为div的id。您需要设置两个不同的div并相应地更新对象:
设置
<?php $ timePerProjectChart-> div('projectChart');?>
<?php $ timePerClientChart-> div('clientChart');?>
< div id =projectChart><?php $ this-> GoogleChart-> createJsChart($ timePerProjectChart);?>< / div&
< div id =clientChart><?php $ this-> GoogleChart-> createJsChart($ timePerClientChart);?>< / div&
Hello i'm using Google Charts Plugin to my CakePHP application.
In my Controller i do: There are two functions that return two graphs.
function statistics() {
$this->timePerClient();
$this->timePerProjectChart();
}
FUNCTION timePerProject
function timePerProjectChart() {
$totalProjeto = $this->timePerProject();
$tempoTotalGasto = $this->tempoTotalInvestido();
//Setup data for chart
$timePerProjectChart = new GoogleChart();
$timePerProjectChart->type("PieChart");
$timePerProjectChart->options(array('title' => "Percentagem de Tempo (horas) investido por Projeto"));
$timePerProjectChart->columns(array(
//Each column key should correspond to a field in your data array
'projects' => array(
'type' => 'string',
'label' => 'Projeto'
),
'tempoGasto' => array(
'type' => 'time',
'label' => '% horas'
)
));
//You can also use this way to loop through data and creates data rows:
foreach ($totalProjeto as $row) {
$percentagemTempoGasto = ($this->timeToHour($row[0]['tempogasto']) / $tempoTotalGasto[0][0]['tempogasto']) * 100;
$timePerProjectChart->addRow(array('tempoGasto' => $percentagemTempoGasto, 'projects' => $row['Project']['pname']));
}
//Set the chart for your view
$this->set('timePerProjectChart', $timePerProjectChart);
}
In my view (statistics) i do :
<div id="chart_div" ><?php $this->GoogleChart->createJsChart($timePerProjectChart);
$this->GoogleChart->createJsChart($timePerClientChart);
?></div>
but I just can not see a single graph. I tested (individually) each and are functioning. I'll wish to put multiple charts on the same view.
Is it possible?
thanks
What is happening in your statistics action in your controller? $this
is an object.
There are a couple steps required to use the plugin: '
Get data. Use a find or other model method to get the data you want.
Set up the chart:
$chart->type("LineChart");
$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' ) ));Add rows to your chart by looping through data, an example is given below
foreach($model as $round){ $chart->addRow($round['Round']); }
Set the chart for your view- this is the same name that must be then called in
<div id="chart_div"><?php $this->GoogleChart->createJsChart($chart);?></div>
in the view.$this->set(compact('chart'));
To display more than one chart in a single view, you need to not just use the default chart_div
as the id of your div. You need to set two different divs and update the objects accordingly:
set
<?php $timePerProjectChart->div('projectChart');?>
<?php $timePerClientChart->div('clientChart');?>
<div id="projectChart"><?php $this->GoogleChart->createJsChart($timePerProjectChart);?></div>
<div id="clientChart"><?php $this->GoogleChart->createJsChart($timePerClientChart);?></div>
这篇关于CakePp和Google图表插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!