关于CakePHP和谷歌图表

时间:2013-11-04 13:56:23

标签: php cakephp google-visualization

我尝试使用http://bakery.cakephp.org/articles/ixu38/2010/04/30/googlechart-api-helper,但我还没有设法让它发挥作用。

  1. 我已使用代码在GoogleChartHelper上创建了帮助app-->View-->Helper

  2. 我在我的控制器上创建了这个功能:

    public function chart() {
        $color = array(
            '#687e9b',
            '#c4ccd8',
        );
    
        $dataMultiple = array(array(0,1,2,3), array(4,5,6,7));
    
        $googleChart->setChartAttrs( 
            array(
                'type'      => 'bar-vertical',
                'title'     => '',
                'data'      => $dataMultiple,
                'size'      => array( 400, 200 ),
                'color'     => $color,
                'labelsXY'  => true,
                'min'       => array(min(rray(0,1,2,3)),min(array(4,5,6,7))),
                'max'       => array(max(array(0,1,2,3)),max(array(4,5,6,7))),
                'legend'    => array('2008', '2009')
            )
        );
    
    //  Print chart
        echo $googleChart;
    
  3. 最后我把这个命令放在了视图上:

  4. <?php echo $this->Html->link(__('Chart'), '/users/chart'); ?>

    当我试图制作图表时,cakephp给了我这个错误:

      

    错误:在非对象

    上调用成员函数setChartAttrs()

    问题出在哪里?

1 个答案:

答案 0 :(得分:0)

Try this helper for google chart并将其粘贴到app/view/helper      并使用下面的代码

Controller:

public $helpers = array('GoogleChart');

View:

// example of bar chart
echo $this->GoogleChart->create()
    ->setType('bar', array('horizontal', 'grouped'))
    ->setSize(500, 400)
    ->setMargins(5, 5, 5, 5)
    ->addData(array(1200.48, 432.3, 647.21, 635.2))
    ->addMarker('value', array('format' => 'f1', 'placement' => 'c'))
    ->addData(array(20, 42.3, 65.21, 95.2))
    ->addMarker('value', array('size' => 14, 'color' => '000000'))
    ->addAxis('x', array('labels' => array('jan 2012', 'feb 2012')))
    ->addAxis('y', array('axis_or_tick' => 'l', 'size' => 12));

// example of pie chart
echo $this->GoogleChart->create()
    ->setTitle('CHART TITLE', array('size' => 14, 'color' => '000000'))
    ->setType('pie', array('3d'))
    ->setSize(600, 300)
    ->setMargins(10, 10, 10, 10)
    ->addData(array(20, 35, 50, 10))
    ->setPieChartLabels(array('first', 'second', 'third', 'and so on...'));