pChart is a PHP class oriented framework designed to create aliased charts. As we all know most of today’s chart libraries have a cost, but pChart is intended to be free according to its official site. This project is still under development and new features or fix are made every week. In this tutorial, we will learn how to setup pChart in CakePHP. The goal of this tutorial is to show a basic chart using pChart in CakePHP environment, during this process, you should gain the idea of using pChart in CakePHP. Please note the purpose of this tutorial is not to implement complex charts using pChart, you should look into pChart official site for that. Let us get started!
<?php
class PchartsController extends AppController {
public $name = 'Pcharts';
//1
public $uses = null;
public function index(){
//2
$this->autoRender = false;
//3
App::import('Vendor','pData', array('file' =>'pchart'.DS.'pData.class'));
App::import('Vendor','pChart', array('file' =>'pchart'.DS.'pChart.class'));
//4
$fontFolder = APP.'vendors'.DS.'pchart'.DS.'Fonts';
//5
// Dataset definition
$DataSet = new pData;
$DataSet->AddPoint(array(1,4,3,2,3,3,2,1,0,7,4,3,2,3,3,5,1,0,7));
$DataSet->AddSerie();
$DataSet->SetSerieName("Sample data","Serie1");
// Initialise the graph
$Test = new pChart(700,230);
$Test->setFontProperties($fontFolder.DS."tahoma.ttf",10);
$Test->setGraphArea(40,30,680,200);
$Test->drawGraphArea(252,252,252,TRUE);
$Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2);
$Test->drawGrid(4,TRUE,230,230,230,70);
// Draw the line graph
$Test->drawLineGraph($DataSet->GetData(),$DataSet->GetDataDescription());
$Test->drawPlotGraph($DataSet->GetData(),$DataSet->GetDataDescription(),3,2,255,255,255);
// Finish the graph
$Test->setFontProperties($fontFolder.DS."tahoma.ttf",8);
$Test->drawLegend(45,35,$DataSet->GetDataDescription(),255,255,255);
$Test->setFontProperties($fontFolder.DS."tahoma.ttf",10);
$Test->drawTitle(60,22,"My pretty graph",50,50,50,585);
$Test->Stroke();
}
}
?>
Now open your browser and type in "http://your-domain/pcharts", you should be able to see a chart created by pChart as below: If you like our tutorial, please follow us on Twitter and help spread the word. We need your support to continue. If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.