Graphing Data with PHP/SWF Charts

Quarterly sales figures, industry contacts, profit margin trends... you name it. Data is the lifeblood of any company. All employees, from the intern preparing a Powerpoint presentation for the middle manager to the dogged sales representative trying to woo a major prospect, require easy and convenient access to all manner of corporate information. The first requirement, ease, has long since been resolved by the Internet. These days, corporate data can be accessed from practically anywhere, be it the company intranet, a laptop using a hotel wireless network, or from the back of a taxicab using a cell phone. However, the second requirement, convenience, isn't so straightforward.

Convenient data access is a tougher problem to solve because users are able to do little with raw data alone. Rather, it needs to be aggregated and massaged in a coherent fashion. Users need to be able to grasp its context easily, and understand how it all relates to the bigger picture. One of the most successful approaches to doing so is by providing users with a way to visualize the data, typically by using spreadsheets or charting (or a combination of both). But, what if you wanted to relieve your users of some of the tedium involved in formatting this data by doing it for them?

In this tutorial, I'll introduce you to PHP/SWF Charts, a great solution for building PHP- and Flash-driven charts. PHP/SWF supports a wide variety of charting formats, including bar, candlestick, 3D columns, pie, 3D pie, and advanced charts such as scatter, polar, and composites. See the maani.us PHP/SWF gallery for a great compilation of examples. I'll base the examples found throughout this article on one particular charting format: the bar chart. Although simple, I think you'll find PHP/SWF can produce particularly compelling bar charts nonetheless.

Although not technically open source, you're nonetheless able to use and distribute a free version. Paid versions are also available; they grant you additional benefits, including support. See the official PHP/SWF site for more information about your licensing options.

Installing PHP/SWF Charts

Installing PHP/SWF Charts is easy. Just navigate to the official website and download the latest version: http://www.maani.us/charts/index.php?menu=Download. Three formats are available (TAR, ZIP, and SIT), allowing you to choose the archive format most suitable to your operating system (Unix/Linux, Windows, and OS X, respectively).

Next, decompress the download to a directory located somewhere within your web server's root directory, for instance /usr/local/apache/htdocs/charting/. That's it! You're ready to begin using PHP/SWF.

Using PHP/SWF

To create charts using PHP/SWF, you'll need to create two scripts. The first script is entirely responsible for building the chart. The second is responsible for displaying that chart within the browser. Although seemingly tedious, and certainly a point of confusion among PHP/SWF beginners, this approach results in a clean separation of often complex logic and the display markup used to format the web page and any corresponding charts. How this is done will become clear as you create your first PHP/SWF bar chart.

Building a Bar Chart

Suppose your company was home to a competitive sales team, and a contest was put together to award the quarter's top sales representative with a trip to Las Vegas. To further ratchet up the pressure, the sales manager wants to make a real-time graph available to the entire company via the corporate intranet. By using PHP/SWF, you easily can create a bar chart displaying the results.

Because PHP/SWF creates charts based on data passed in using arrays, you can create a multi-dimensional array containing each sales representative's results. In the following script (barchart.php), you'll also see two other requisite tasks: including the charts.php file, and calling the SendChartData() function, passing in the array you've created: