phpcakephpgoogle-pie-chart

cakephp piechart by dynamic data


I want to create a pie chart in cakephp by dynamic data.I already create this but it is static. how can i make it dynamic. my code here.

<?php 
echo $this->GoogleChart->create ()
    ->setTitle ( 'Trips Request Graph', array ('size' => 26, 'color' => '000000' ) )
    ->setType ( 'pie', array ('3d' ) )
    ->setSize ( 1000, 300 )
    ->setMargins ( 10, 10, 10, 10, 10 )
    ->addData ( array (10,10,10) )
    ->setPieChartLabels ( array ('Riajul','Rafsan','Taukir' ) );
?>

I want to set data in ->addData() and ->setPieChartLabels section


Solution

  • Try this

    $arr = array ( 0 => "Riajul", 1 => "Rafsan", 2 => "Taukir"); // your data
    
    $nameString = implode (", ", $arr);  // create a comma separated string
    
    echo $this->GoogleChart->create()->setTitle ( 'Trips Request Graph', array ('size' => 26, 'color' => '000000' ) )
        ->setType ( 'pie', array ('3d' ) )
        ->setSize ( 1000, 300 )
        ->setMargins ( 10, 10, 10, 10, 10 )
        ->addData ( array (10,10,10) )
        ->setPieChartLabels ( array ($nameString) ); 
    ?>