JpGraph is an Object-Oriented Graph creating library for PHP >= 5.1 The library is completely written in PHP and ready to be used in any PHP scripts. In this tutorial, we will go through the basic steps on how to setup Jpgraph in CakePHP, and the final goal is to implement the very first example of Jpgraph in CakePHP.
<?php
class JpgraphsController extends AppController {
public $name = 'Jpgraphs';
public $uses = null;
public function index(){
$this->layout='default';
}
}
?>
<?php echo $content_for_layout; ?>
Create testing view page at "app/views/jpgraphs/index.ctp", take a close look at the code, in CakePHP we are using "App::import('Vendor', 'jpgraph/XXXX')" to include Jpgraph classes, whereas we are using "require_once()" statement when using it regularly. This is the only difference between regular use and using it in CakePHP.
<?php
App::import('Vendor', 'jpgraph/jpgraph');
App::import('Vendor', 'jpgraph/jpgraph_line');
$ydata = array(11,11,11);
// Create the graph.
$graph = new Graph(350,250);
$graph->SetScale("textlin");
$graph->img->SetMargin(30,90,40,50);
$graph->xaxis->SetFont(FF_FONT1,FS_BOLD);
$graph->title->Set("Example 1.1 same y-values");
// Create the linear plot
$lineplot=new LinePlot($ydata);
$lineplot->SetLegend("Test 1");
$lineplot->SetColor("blue");
$lineplot->SetWeight(5);
// Add the plot to the graph
$graph->Add($lineplot);
// Display the graph
$graph->Stroke();
?>
// ]]>
Now open your browser and type in "http://your-domain//jpgraphs", you should be able to see a chart created by Jpgraph as below: If you like our tutorial, please follow us on Twitter and help spread the word. We need your support to continue.