jqPlot is a plotting and charting plugin for the jQuery JavaScript framework. jqPlot produces beautiful line, bar and pie charts with many features. What is more, it is an open source project which means you can use it for free. In this tutorial, we will learn how to setup jqPlot in CakePHP. The goal of this tutorial is to show a simple linechart using jqPlot in CakePHP, during this process, you should gain the idea of using jqPlot in CakePHP. Please note the purpose of this tutorial is not to implement complex charts using jqPlot, you should look into jqPlot official site for that. Let us get started!
<?php
class JqplotsController extends AppController {
public $name = 'Jqplots';
public $uses = null; //1
public $helpers = array('Javascript','Html'); //2
public function index(){ //3
$this->layout='clean'; //4
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
<?php echo __('Star Tutorial'); ?>
</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<!-- content -->
<div id="content" >
<?php echo $content_for_layout; ?>
</div>
<!-- end content -->
</body>
</html>
Create view page at "app/views/jqplots/index.ctp", and copy code below into the file:
<!-- 1 -->
<!--[if lt IE 9]>
<?php echo $this->Javascript->link('excanvas.min.js');?>
<![endif]-->
<?php
echo $this->Javascript->link('jquery.min.js');
echo $this->Javascript->link('jquery.jqplot.min.js');
echo $this->Html->css('jquery.jqplot.min.css');
?>
<!-- 2 -->
<div id="chartdiv" style="height:400px;width:500px; margin:auto; "></div>
<!-- 3 -->
<script>
$(document).ready(function(){
$.jqplot('chartdiv', [[[1, 2],[3,5.12],[5,13.1],[7,33.6],[9,85.9],[11,219.9]]]);
});
</script>
Now open your browser and type in "http://your-domain/jqplots", you should be able to see a chart created by jqPlot as below: If you like our tutorial, please follow us on Twitter and help spread the word. We need your support to continue.