问题描述
我想在CakePHP中使用ghunti的封装,以便我可以在我的项目中使用它。
I am trying to use ghunti's HighchartsPHP wrapper in CakePHP so I can use it in my project.
在演示中,它说要编辑 config.php
并包含脚本,然后您可以访问 Highchart
类构建图表。这将工作一个平面的PHP项目,但在MVC它的工作有点不同我猜。
In the demo it says to edit the config.php
and include the script and you then have access to the Highchart
class to build charts. This would work for a flat php project but in MVC it works a bit differently I guess.
第一步是如何包括在我的应用程序的库。我把这些文件放在 Vendor
文件夹中的一个目录中,名为 HighchartsPHP
,在我的控制器文件中,
The first step is how to include the library in my app. I have placed the files in a directory in the Vendor
folder called HighchartsPHP
and in my controller file I have placed the code
App::import('HighchartsPHP', 'Highchart');
当我试图创建一个新的 Highchart
对象我遇到
When I attempt to instatiate a new Highchart
object I am met with
问题1 :在我的项目中包含此库的正确方法是什么?
Question 1: what's the correct way to include this library in my project?
假设上面的问题已解决,我假设我将从控制器中的数据构建实际图表,然后将图表对象传递给视图渲染?所以在我的一个页面动作在控制器我可能会把
Assuming the above is solved, I presume I would build the actual chart from its data in the controller and then pass the chart object to the view for rendering? So in one of my page actions in the controller I might put
$chart = new Highchart();
$chart->series[0]->name = 'Tokyo';
$chart->series[0]->data = array(7.0, 6.9, 9.5);
然后
$this->set( compact( 'chart' ) );
以传递到我将使用以下形式呈现图表的视图:
to pass to the view where I would render the chart with:
<?php echo $chart->render("chart"); ?>
问题2 :这是正确的,如果没有,
Question 2: Is this correct and if not how should I do this?
推荐答案
您的控制器应该是这样:
Your controller should be like this:
<?php
App::import('Vendor', 'HighchartsPHP/Highchart');
class ChartsController extends AppController {
public function index() {
$chart = new Highchart();
$chart->chart = array(
'renderTo' => 'container', // div ID
'type' => 'line'
);
$chart->series[0]->name = 'Tokyo';
$chart->series[0]->data = array(7.0, 6.9, 9.5);
$this->set( compact( 'chart' ) );
}
}
和index.ctp: p>
and index.ctp:
<?php $chart->printScripts(); ?>
<script type="text/javascript">
<?php echo $chart->render("chart");?>
</script>
这篇关于在CakePHP中使用HighchartsPHP库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!