Many thanks to the CakePHP mailing list for pointing out some really dumb errors I made along the way getting this stuff to work. It turns out that case-sensitivity does actually matter at time.
Okay, for a project at work I was asked to add graphs to some of the reports I'd already created. So I looked around for solutions for graphing in PHP and stumbled upon PHP/SWF Charts as a solution. Basically, it comes in two parts: a Flash movie that generates the charts for you and a PHP library that sends data to the Flash movie to create those great charts.
So, first thing I did was download the package from the PHP/SWF Charts site. I then dumped everything into a /graphs directory in my web root. Why? Well, that's the only way I could get the thing working and I am a disciple who worships at the altar of Get It Done because there are other things I'd rather deal with. I'm sure one of the CakePHP people who runs across this tutorial can correct things for me so it's all inside the framework itself.
Now, that the library is unzipped and in it's own directory, we go and start altering the code I need to make this work. First thing I did was add a subdirectory to my app/views directory called graphs and added in users.thtml
-
<?php
-
vendor('charts');
-
$chart['chart_type'] = 'stacked area';
-
SendChartData($chart);
-
?>
This code will pass the info I've passed to my view to the flash component. This is the part that will spit out the chart, but I also had to add in some code to the main view for the report itself so the graph will show up
-
<div align = "center">
-
<br /><br />
-
Users Online For Entire Network For Last 24 Hours
-
<br /><br />
-
<?php vendor("charts") ?>
-
</div>
As you can tell, I omitted a step above. When I downloaded the charts package I stuck the charts.swf Flash movie in app/webroot/img so I can find it later. The uniqid stuff is to make sure it doesn't get cached by your browser.
Okay, so now here's the code I created to pull results from our database, munge the data and pass it over to the view:
-
class GraphsController extends AppController
-
{
-
var $name = 'Graphs';
-
-
public function users()
-
{
-
// create graph for charts based on users online for the past 24 hours
-
-
$this->OnlineUser->recursive = -1;
-
$temp = $this->OnlineUser->findBySQL("SELECT MAX(ou_date) as max_ou_date FROM online_users");
-
$max_ou_date = $temp[0][0]['max_ou_date'];
-
$result = $this->OnlineUser->findAll("DATE_SUB('$max_ou_date', INTERVAL 1 DAY) <= ou_date");
-
-
foreach ($result as $data) {
-
// Now we have to find the top value for each hour within
-
// all the data for an hour
-
$site_id = $data['OnlineUser']['site_id'];
-
-
$members[$site_id][$hour] = 0;
-
}
-
-
if ( $members[$site_id][$hour] <$data['OnlineUser']['members']) {
-
$members[$site_id][$hour] = $data['OnlineUser']['members'];
-
}
-
-
$visitors[$site_id][$hour] = 0;
-
}
-
-
if ( $visitors[$site_id][$hour] <$data['OnlineUser']['visitors']) {
-
$visitors[$site_id][$hour] = $data['OnlineUser']['visitors'];
-
}
-
-
}
-
-
// Phew, that's a lot of dating munging. Okay, now we need to cycle through
-
// the raw data and create totals for each hour
-
-
foreach ($members as $site_id => $member) {
-
foreach ($member as $hour => $count) {
-
-
-
$member_count[$hour] += $members[$site_id][$hour];
-
}
-
}
-
-
foreach ($visitors as $site_id => $visitor) {
-
foreach ($visitor as $hour => $count) {
-
-
$visitor_count[$hour] += $visitors[$site_id][$hour];
-
}
-
}
-
-
$header[] = "";
-
$member_data[] = "Members";
-
$visitor_data[] = "Visitors";
-
-
foreach ($member_count as $hour => $count) {
-
$header[] = $hour;
-
$member_data[] = $count;
-
$visitor_data[] = $visitor_count[$hour];
-
}
-
-
$this->set('header', $header);
-
$this->set('member_data', $member_data);
-
$this->set('visitor_data', $visitor_data);
-
$this->layout = 'ajax';
-
}
-
}
-
-
?>
So, I think that's all you need in order to get your code working and talking nicely with PHP/SWF charts. I'll go over this stuff again to look for any errors and clear up anything that looks confusing.


Looks Cool!
How about some screenshots though?
Yeah a screen shot would be cool. Or better yet a demo.
Chris Hartjes' Blog: Tutorial: Integrating PHP/SWF Charts with CakePHP
Okay, I added in a screenshot. Hope it makes things a little easier
Fantastic work! I played with SWF Charts for a few days trying to get it to work with Cake, and ran out of time. Your work saved me a ton of effort - thanks again!!
Great article. I think you should put this in the Bakery for other bakers!
http://bakery.cakephp.org
hi, good work, dude. thanks!
hey,
so i used this tutorial to get things going using graphs. but now i am trying to update graphs using Ajax and am experiencing problems. Has anyone been able to figure out how to update the graphs using Ajax in Cakephp? I am buildig a game and want to update the graph using ajax each time a player enters a number.
but anyway, thanks for the tutorial it helped alot.
Seen this? http://teethgrinder.co.uk/open-flash-chart/
Flash charts, but open source and has a nice PHP wrapper for making the chart.
monk.e.boy
[...] charts and graphs can be dynamically generated based on user input. I am using CakePHP, and found this really neat tutorial by Chris ‘Brian that shows you how to integrate PHP/SWF Charts library to a CakePHP application! It took an [...]
i love ur TUTORIAL,
thx again for this tut
i like to now where is the code to put in users.thtml
and where i put this line
InsertChart
found this tutorial very helpful chris ... thanks a lot.
however, the confusing part here is that u have created app/views/graphs because ur controller is GraphsController. i didnt understand what u mean by the 'main view' . could you kindly tell in which directory it is present in and its name.
@rakesh
By 'main view' I meant the view in which my report is supposed to show up. In this case, it would app/views/graphs/users.thtml
thanks for the reply chris ... but i m still not able to get this thing working . Needed your help.
I have a controller called WebappsController. The function results() in this controller is giving dynamic data to the graph. For this, I have created a view results.thtml in the app/views/webapps/ directory. This view is for inserting the chart using InsertChart(). I have another view results-graph.thtml in the same directory that is doing the job of getting the data from the controller and building the graph. I have given absolute path names for the charts.swf and charts_library and results-graph in results.thtml. Also I have copied the carts.swf and charts_library to app/views/webapps/ folder. They are also present in the folder in which I have cake installed. Is this correct or am I doing something wrong. It would be great if you could help.
also could you tell where exactly to put the 2 files charts.swf, charts.php and the directory charts_library in order for the code to work . My code works fine displaying the chart on php but not on cakephp. I guess there is some problem with where I have placed these 2 files and the directory. Or there is a problem in the pathname I have given.
hi chris ... i wanted to know if the code that spits out the chart also in the app/views/graphs/users.thtml. If not, which directory and file is it in . Could you kindly let me know this.
Not able to understand in which file have you written the 1st part of the code present on this page ( the code containing SendChartData()). It would be great if you could let us know that. Also how did u give the pathnames to the carts.swf , charts_library, and users in the InsertChart() function. The tutorial would be really helpful to noobies like me if you explaiin these two points.
@rekesh
As much as I would like to be dedicated tech support for you, I am not. The instructions are clear enough that all sorts of other people have gotten it to work. So, please, after this no more comments asking for help.
* the code that has SendChartData() in it goes in /app/views/graphs/users.html. I say as much in there
* I put charts.swf in the APP/webroot/img directory. I say that in the article as well (notice a trend?)
* chart_library is a directory inside the PHP/SWFCharts package, which I placed in /webroot/graphs. Again I SAID THIS IN THE ARTICLE ITSELF
* '/verticalscope_stats/graphs/users/' is a reference to the action 'users' in the controller 'graphs' in the subdirectory 'verticalscope_stats', which happens to be the root of that particular application
Short of me actually writing the code for you (which I won't do) this is EVERYTHING you need to get this to work. Believe me, it works.
Best of luck.
Can you give the link for the screenshots of PHP/SWF charts in CakePHP web application.
Chris,
I had been stuck in CakePHP for a while to get the graph drawn somehow. But your code helped alot, which saved time. Thanks to you.
Chris, the chart is drawn but the data is not sent so it gives a totally different data rerpresentation. I think the SendChartData() function code is missing. I wasn't able to find out in the article. If convenient, please let me know the code.
Thanks
Waqar
Hello Chris,
This is a wonderful tutorial from you! Many thanks for this. I have a similar requirement in my cakephp application but rather using the php/swf, I want to use the xml/swf version of the chart. Can you please guide me how do I do this? I am not asking for any code, I just want to know which file go where in the cakephp folder structure..
Any help would be greatly appreciated!
Thanks,
Vibhor