0

こんにちは、CakePHP アプリケーションに Google Charts プラグインを使用しています。

私のコントローラーでは、次のことを行います。2 つのグラフを返す 2 つの関数があります。

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

    }

私の見解(統計)では、私は次のことを行います:

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

しかし、私は単一のグラフを見ることができません。私は(個別に)それぞれをテストし、機能しています。同じビューに複数のチャートを配置したいと思います。

出来ますか?

ありがとう

4

1 に答える 1

1

コントローラーの統計アクションで何が起こっていますか? $thisはオブジェクトです。

プラグインを使用するには、いくつかの手順が必要です。

  1. データを取得します。検索またはその他のモデル メソッドを使用して、必要なデータを取得します。

  2. チャートを設定します。

    $chart->type("LineChart");

    $chart->options(array('title' => "Recent Scores"));

    $chart->columns(array( //各列キーは、データ配列内のフィールドに対応する必要があります 'event_date' => array( //これがどのタイプのデータであるかをチャートに伝えます 'type' => 'string',
    / /この列のチャート ラベル
    'label' => 'Date' ), 'score' => array( 'type' => 'number', 'label' => 'Score' ) ));

  3. データをループしてチャートに行を追加します。以下に例を示します

    foreach($model as $round){ $chart->addRow($round['Round']); }

  4. ビューのチャートを設定します。これは、ビューで呼び出す必要があるのと同じ名前です<div id="chart_div"><?php $this->GoogleChart->createJsChart($chart);?></div>

    $this->set(compact('chart'));

1 つのビューに複数のチャートを表示するにchart_divは、div の ID としてデフォルトを使用する必要はありません。2 つの異なる 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>
于 2013-05-26T02:11:30.623 に答える