2013-05-25 33 views
0

你好,我使用谷歌圖表插件到我的CakePHP應用程序。CakePhp和谷歌圖表插件

在我的控制器我做: 有兩個函數返回兩個圖。

function statistics() { 
    $this->timePerClient(); 
$this->timePerProjectChart(); 
} 

功能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); 

    } 

在我看來(統計)我做的:

<div id="chart_div" ><?php $this->GoogleChart->createJsChart($timePerProjectChart); 
$this->GoogleChart->createJsChart($timePerClientChart); 
?></div> 

,但我不能看到一個圖。我測試(單獨)每個,並運作。 我希望在同一視圖中放置多個圖表。

可能嗎?

謝謝

+0

?這是一個嗎? https://github.com/segy/GoogleChart那一個沒有createJSChart方法 – swiecki

+0

我使用這個:https://github.com/scottharwell/cakephp-googlecharts-plugin – Jcbo

+0

沒關係,在這種情況下,你的視圖代碼看起來不錯。 – swiecki

回答

1

您的控制器中的統計操作發生了什麼? $this是一個對象。

有使用該插件需要幾個步驟:'

  1. 獲取數據。使用查找或其他模型方法來獲取所需的數據。

  2. 設置圖表:

    $ chart->類型( 「應用於LineChart」); (數組('title'=>「Recent Scores」));

    chart- $>列(陣列( //每一列密鑰應該對應於一個場的數據陣列中 「EVENT_DATE」 =>數組( //告訴圖表什麼類型的數據,這是 「型'=> '字串',
    //圖表標籤此列
    '標籤'=> '日期' ) '得分'=>數組( '類型'=> '編號', '標籤'=>'分數' ) ));

  3. 通過數據循環到圖表添加行,一個例子如下

    的foreach { $ chart-> addRow($輪[ '回合'])($模型作爲$輪)中給出; }

  4. 爲您的視圖設置圖表 - 這是必須在視圖中調用<div id="chart_div"><?php $this->GoogleChart->createJsChart($chart);?></div>的同一名稱。

    $ 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> 
您正在使用哪個插件
+0

does not work:/ 我使用了一個類似於你的代碼的回聲,它不起作用 – Jcbo

+0

我正在做所有這些。除了:$ this-> set('timePerSetorChart',$ timePerSetorChart);'。我不使用'compact'。 如果我使用compact,然後在我的函數'statistics'中調用具有圖形的函數,給我'Undefined variable:timePerClientChart' – Jcbo

+0

我已更新我的答案以解決如何在同一視圖上創建多個圖表。 – swiecki