Graphing Data with PHP/SWF Charts, Page 2
Want to turn the chart into a horizontal bar chart? Just add the following line to barchart.php following the array:
$chart['chart_type'] = "bar";
Execute barchart-output.php anew and you'll see the following chart:
Figure 2: Presenting the sales data using a horizontal bar chart
Building a 3D Column Chart
Bar charts are nice, but are lacking in excitement. To really spruce up things, you can add some dimension to the graphs. Modify barchart.php to turn the chart into a three-dimensional version:
<?php include "charts.php"; $chart['chart_data'] = array ( array ( "", "January", "February", "March" ), array ( "John Smith", 124198.42, 200088.34, 412128.99 ), array ( "Michael Rodgers", 119099.56, 247998.99, 476991.11 ) ); // Set the chart type $chart['chart_type'] = "3d column"; // Determine how much the chart should be rotated both // horizontally and vertically $chart['chart_pref'] = array ( 'rotation_x'=>25, 'rotation_y'=>15 ); // Set the chart offset, width and height, and shading $chart['chart_rect'] = array ( 'x'=>50, 'y'=>50, 'width'=>300, 'height'=>250, 'positive_alpha'=>55, 'negative_alpha'=>25 ); SendChartData($chart); ?>
Load barchart-output.php anew and you'll see the following chart:
Figure 3: Sprucing up charts by adding some dimension!
Automatically Refreshing the Chart
The quarter is rapidly coming to a close, and the corporate staff's eyes are glued to the chart. To avoid a slowdown due to employees' constant navigation to the chart, the sales manager has decided to project the chart onto the office wall. However, the chart won't be updated unless somebody regularly refreshes the browser. To remedy this problem, you can use PHP/SWF's "live update" feature, automatically updating the chart according to a specified time frame. For instance, to update barchart-output.php every 15 seconds, add the following array attribute to barchart.php:
$chart['live_update'] = array ( 'url'=>"barchart.php?time=".time(), 'delay'=>15);
To prove this is working as expected, once the chart loads change one of the array values to something significantly different from its original value, and save the script. You'll see that after 15 seconds the chart will reload with the new information.
PHP/SWF is an amazingly powerful charting library, and what I've demonstrated here only begins to scratch the surface of its capabilities. See the PHP/SWF reference documentation for a complete breakdown of what's available to you. Regardless, I hope this brief tutorial gives you some ideas for incorporating charting into your own applications!
About the Author
W. Jason Gilmore is co-founder of IT Enlightenment. He's the author of several books, including the best-selling Beginning PHP and MySQL 5: Novice to Professional, Second Edition (Apress, 2006. 913pp.). Jason loves receiving e-mail, so don't hesitate to write him at wjATwjgilmore.com.
Page 2 of 2