2
votes

I'm trying to display data in cakePHP with a Google Chart. I downloaded the GoogleCharts plugin on https://github.com/scottharwell/GoogleCharts and put it in my App/Plugin directory. I load all plugins in my application's bootstrap file with

CakePlugin::loadAll(); 

In my Controller, I put

 App::uses('GoogleCharts', 'GoogleCharts.Lib'); 

and

public $helpers = array('GoogleCharts.GoogleCharts');

, so cakePHP is able to detect I will work with this plugin. Of course, I created the GoogleChartHelper.php in App/View/Helper.

Before working with my data, what I want is just to display an example of chart to see if it is working. Which is not! I copied the example of the above-provided link (on github.com), so here is my Controller class:

<?php
App::uses ( 'AppController', 'Controller');
App::uses ('GoogleCharts', 'GoogleCharts.Lib');

    class StatisticsController extends AppController{

    public $helpers = array('GoogleCharts.GoogleCharts');
    public $components = array (
            'Paginator',
            'Session'
        );

    public function index(){
            //Setup data for chart
            $chart = new GoogleCharts();

            $chart->type("LineChart");
            //Options array holds all options for Chart API
            $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',
                            //Optional NumberFormat pattern
                            'format' => '#,###'
                    )
            ));


        //You can also manually add rows:
        $chart->addRow(array('event_date' => '1/1/2012', 'score' => 55));

        //Set the chart for your view
        $this->set(compact('chart'));
    }
    }

In my View, I put the code

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

(without "s" to "GoogleChart" (not like on the download page, where is it written "GoogleCharts"), which took me 3 hours to notice)

My charts should be displayed on the page, but I got the following error:

Warning (512): Method GoogleChartHelper::createJsChart does not exist [CORE\Cake\View\Helper.php, line 192]

(and if I put the "s" in the View, my page displays like without any error but without any chart...)

Am I missing something?

N.B. : I didn't copy the first method given on the github page, because with it a new worst error appears:

Error: Call to a member function find() on null

That method is:

 //Get data from model
    //Get the last 10 rounds for score graph
    $rounds = $this->Round->find(
        'all',
        array(
            'conditions' => array(
                'Round.user_id' => $this->Auth->user('id')
            ),
            'order' => array('Round.event_date' => 'ASC'),
            'limit' => 10,
            'fields' => array(
                'Round.score',
                'Round.event_date'
            )
        )
    );

Please help me, I just want to display some random data in a chart and it should not be complicated (but with cakePHP, everything seems complex)...

1
The helper should definitely called with $this->GoogleCharts->createJsChart (with the S). If that doesn't show anything, maybe something is off in your input or maybe your browser is blocking (remote) scripts?Oldskool
Thanks, at least I know how to call it correctly (with the "s"). It is not my browser blocking scripts, because I can display other Google charts on other websites and my collegue tried with my code on his computer and had the same problem... What do you mean by "something is off in your input"?user3507737

1 Answers

0
votes

The problem is that one step was missing: you have to write

<?php echo $this->fetch('script'); ?>

in View/Layouts/bootstrap.ctp and if you followed all other steps, it should work!