
In this episode we talk about everything from image manipulation, generating charts & graphs, classes and frameworks and even home invasions.
The charting libraries I discuss are Fusion Charts and pChart, both are very good libraries and have great documentation on how to get started.
The following is a code snippet that I wrote which takes in an associative array, parses it out for a data label ($a[0]) and a data value ($a[1]) and generates a Fusion compatible XML file.
function fnGenerateXML($scaption, $sxaxis, $syaxis, $adata)
{
if(empty($adata))
die("Empty data set passed to function.");
$sdata = "\n";
foreach($adata as $arow)
$sdata .= " \n";
$sdata .= "";
$sfile_xml = "./chart.xml";
file_put_contents($sfile_xml, $sdata);
return $sfile_xml;
}
The function is called in the following fashion:
$sfile_xml = fnGenerateXML("Caption", "X Lbl", "Y Lbl", $a);
The Fusion Chart is generated using the following method call:
echo renderChartHTML("./Column3D.swf", "$sfile_xml", "",
"ID", 650, 650, false);
Here is some sample code I wrote to generate a graph using pChart:
// Dataset definition
$pData = new pData;
$pData->AddPoint($a1, "s1");
$pData->AddPoint($a2, "s2");
$pData->AddPoint($alabels, "lbl");
$pData->AddSerie("s1");
$pData->AddSerie("s2");
$pData->SetSerieName("s1lbl", "s1");
$pData->SetSerieName("s2lbl", "s2");
$pData->SetYAxisName("Ylbl");
$pData->SetAbsciseLabelSerie("lbl");
// Init graph
$pGraph = new pChart(800, 340);
$pGraph->setFontProperties("./tahoma.ttf",8);
$pGraph->setGraphArea(50,30,680,200);
$pGraph->drawFilledRoundedRectangle(7,7,693,240,5,240,240,240);
$pGraph->drawRoundedRectangle(5,5,695,240,5,260,230,230);
$pGraph->drawGraphArea(255,255,255,TRUE);
$pGraph->drawScale($pData->GetData(),
$pData->GetDataDescription(),SCALE_ADDALL,150,150,150,TRUE,0,2,TRUE);
$pGraph->drawGrid(4,TRUE,230,230,230,50);
// Draw the 0 line
$pGraph->drawTreshold(0,143,55,72,TRUE,TRUE);
// Draw the graph
$pGraph->drawStackedBarGraph($pData->GetData(),
$pData->GetDataDescription(),TRUE);
// Finish
$pGraph->drawLegend(500,40,$pData->GetDataDescription(),255,255,255);
$pGraph->drawTitle(225,22,"Title",0,0,0);
$pGraph->Stroke();
As you can see pChart requires a bit more setup to get going but both are great libraries for getting the job done. Another free option for charting/graphing that we didn’t discuss on the podcast is the Google Chart API.
Check both Stephen and Myself out on Twitter and also subscribe to us on iTunes!
Enjoy the show and as always, stay geeky!
Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.
- Nicholas