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:
- 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).
- 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);
}
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.