1
votes

I'm trying to use HighCharts with ChakePHP 2.4.* and I don't find a good way to view my Chart in my Views.
I have install cakephp-high-charts-plugin via GitHub. It does run if I go to demo path, but when I use my controller outside the plugins directory with
<?php echo $this->HighCharts->render('ManageStates'); ?>

Cake php returns

Error: Chart: "1" could not be found. Ensure that Chart Name is the same string that is passed to $this->HighCharts->render() in your view. File: /var/www/armoire/app/Plugin/HighCharts/View/Helper/HighChartsHelper.php Line: 99

My controller :

class CreagraphsController extends AppController {
    public $name = 'Graphique etats';
    public $components = array('HighCharts.HighCharts');
    public $helpers = array('HighCharts.HighCharts');
    public $uses = array();
    public function Creagraphs() {
        $chartName = 'ManageStates';
    $mychart = $this->HighCharts->create( $chartName,'column' );
        $this->HighCharts->setChartParams
            (
                $chartName
                );
    }
}

Can I use my directory controller to create Charts, and how can I do this ?

2

2 Answers

0
votes

Like I already said on my own post right here; cakephp highchart fatal error while rendering

The plugin is not yet tested in any other version higher than 1.3 and therefor it doesn't work. The developer is creating a new version for 2.5 and it should be released quite soon. :)

0
votes

The CakePHP Highcharts plugin was built to support CakePHP versions 2.* and above and not versions prior to that. Additionally, the examples included in the plugin are meant to serve as a usage guide on how to set up your own controllers and views to render the charts in your own project.

The question was asked "can I use my directory controller to create Charts, and how can I do this?"

The answer is yes you should use your directory controller to create the charts. How do you go about doing this? Simple:

  1. In your DirectoriesController.php file, add the Highcharts plugin component to your controller components public $components = array('Highcharts.Highcharts');. This will also autoload the Highcharts plugin helper to your view (the component handles that for you).
  2. Create or modify a controller action for which you want to setup your chart to include the following (if you wanted a pie chart for eg.):

:

public function pie() {
        // N.B your $chartData array will be accessed from your model
        $chartData = array(
            array(
                'name' => 'Chrome',
                'y' => 45.0,
                'sliced' => true,
                'selected' => true
                 ),
            array('IE', 26.8),
            array('Firefox', 12.8),
            array('Safari', 8.5),
            array('Opera', 6.2),
            array('Others', 0.7)
        );
        $chartName = 'Pie Chart';
        $pieChart = $this->Highcharts->create( $chartName, 'pie' );
        $this->Highcharts->setChartParams(
                                            $chartName,
                                            array
                                            (
                                                'renderTo'  => 'piewrapper',  // div to display chart inside
                                                'chartWidth' => 1000,
                                                'chartHeight' => 750,
                                                'chartTheme' => 'gray',
                                                'title'  => 'Browser Usage Statistics',
                                                'plotOptionsShowInLegend' => TRUE,
                                                'creditsEnabled'  => FALSE
                                            )
        );
        $series = $this->Highcharts->addChartSeries();
        $series->addName('Browser Share')
            ->addData($chartData);
        $pieChart->addSeries($series);
    }
  1. Create or modify your Directories view folder to contain a file called pie.ctp (to match the name of your controller action) and add the following code:

    A Very Humble Pie Chart with legend
        <div id="piewrapper" style="display: block; float: left; width:90%; margin-bottom: 20px;"></div>
    
        <?php echo $this->Highcharts->render('Pie Chart'); ?>
    

Two things should be noted here:

  • Your view must contain a div ID matching the value you specified for the "renderTo" key in the array passed to $this->Highcharts->setChartParams() in your controller action.
  • The value set for $chartName in your controller must be identical to the value passed to the call $this->Highcharts->render() in your view.