pChart: Free Online Chart Program
This article was published by ComputorEdge, issue #2720, , as a feature article, in both their PDF edition (on pages 13-17) and their website.
When you need to create a colorful chart or graph for personal or professional use, there are countless computer programs for getting the job done — with a wide range of capabilities and prices. For simple charts, your office productivity suite is usually sufficient, because most if not all such programs contain some sort of charting program. In Microsoft Excel, creating a chart is as simple as entering the data into a worksheet, and then firing up the chart wizard (from the Insert menu, choose Chart). In Google Docs, the process is quite similar, though it is all done on the Web through your browser, rather than on your own computer through a desktop application. OpenOffice.org's tool named Impress is mainly focused on creating presentations — as a direct competitor to Microsoft's PowerPoint — but can also be used for building attractive charts. Sun Microsystems's StarOffice version of Impress is similar in functionality, but is not free.
All of the aforementioned programs are components of much broader office productivity suites, and thus can lack the focus and richer functionality of programs devoted specifically to generating charts and graphs. Perhaps of greater importance, depending upon one's needs, all of those programs are fine for manually creating a static chart. But what if you need charts that are generated dynamically, perhaps based on data that a user is entering in on your website? The demand for such a capability is growing all the time, as desktop programs are rewritten to be run on the Web, and more people are developing personal and commercial websites that require — or are greatly enhanced by — the display of data in an attractive graphical format.
In these cases, what is called for is some sort of charting program or library that can work in conjunction with whatever program code is running your website. The Web scripting language that is most commonly used nowadays is PHP, and consequently the majority of these Web-based charting tools are designed for use with PHP code. Admittedly, nonprogrammers may be put off by the prospect of programming work being needed to implement charts and graphs on their websites, but it is not difficult to obtain professional assistance, if needed.
There are several quite usable online charting programs available, and in this article I will focus on one that is completely free.
Start Your Charts
pChart, hosted as a SourceForge project, is billed as "a PHP class oriented framework designed to create aliased charts." What this effectively means is that it is a library of computer code that can be used with your PHP-powered website to create charts, and the code is organized using the principles of object-oriented programming, which can greatly help to keep the code more organized and readable. The product was developed by Jean-Damien Pogolotti, whose name and personal site suggests that he is French. This is also reflected in some of the site copy and documentation, all of which is in English, but frequently has some misspellings and awkward phrasing. Do not let that deter you from trying the program.
To get started, confirm that the installation of PHP that you are using contains the GD Graphics Library, which is an open-source graphics library that makes it possible for Web applications to create images dynamically. With regards to a PHP installation on a local Web server — in other words, as part of your local Web development environment — you will have complete control over what libraries are available to your PHP. With regards to a remote Web server — such as a shared hosting account — you will need to check with the support staff of the hosting company to verify that the GD library is installed. Most if not all decent hosting firms will have the library installed and enabled by default.
Next, visit the pChart website, and go to the "Download" section of the site. The latest release, as of this writing, is 1.27, dated 12 August 2008. Click on the "pChart 1.27 - beta" link, which takes you to the pChart page on the SourceForge site.
The most recent revision is 1.27d, so click on the "pChart.1.27d.rar" link, and save the file to your computer. Some readers may be unfamiliar with the ".rar" file extension. It is used for the RAR file archive format, which is the one most commonly seen on Linux and Unix systems, similar to the Zip format on Windows systems.
If your version of Windows does not natively recognize the RAR file format and allow you to open the "pChart.1.27d.rar" file to access its contents, then you could download and install a file archiving utility that does recognize this format. Frequently recommended free choices are 7-Zip and IZArc.
Install the pChart files and directories where they can be seen by your local Web server. One of those directories is named "pChart", and it contains the PHP class files — specifically, pData.class and pChart.class — that you will need to reference within your code in order to generate your charts. Another essential directory is named "Fonts", and contains five TrueType fonts needed for inserting text into any charts.
Pie Charts without the Price Tag
Let's create an example chart, to illustrate the process. In this case, I will make use of some of the sample code included in the installation package, specifically, "Example13: A 2D exploded pie graph". The PHP code is listed below. Each section includes a brief comment, to explain what that section's commands are doing.
<?php // Standard inclusions include("pChart/pData.class"); include("pChart/pChart.class"); // Dataset definition $DataSet = new pData; $DataSet->AddPoint(array(10,2,3,5,3),"Serie1"); $DataSet->AddPoint(array("Jan","Feb","Mar","Apr","May"),"Serie2"); $DataSet->AddAllSeries(); $DataSet->SetAbsciseLabelSerie("Serie2"); // Initialise the graph $Test = new pChart(300,200); $Test->setFontProperties("Fonts/tahoma.ttf",8); $Test->drawFilledRoundedRectangle(7,7,293,193,5,240,240,240); $Test->drawRoundedRectangle(5,5,295,195,5,230,230,230); // Draw the pie chart $Test->AntialiasQuality = 0; $Test->setShadowProperties(2,2,200,200,200); $Test->drawFlatPieGraphWithShadow($DataSet->GetData(), $DataSet->GetDataDescription(),120,100,60,PIE_PERCENTAGE,8); $Test->clearShadow(); $Test->drawPieLegend(230,15,$DataSet->GetData(), $DataSet->GetDataDescription(),250,250,250); $Test->Render("example13.png"); ?>
In the second section of the code, you see the values for the five months, along with their labels. Those five lines that begin with "$DataSet" are all that are needed for defining the chart's data. The rest of the code creates a new chart object; sets the font, size, and other properties; and finally draws and renders the chart. The term "render" in this case refers to the process of creating a file on disk, named example13.png, containing the image of the pie chart.
The pie chart illustrated above is just one of many types of charts that can be created using pChart. The "Screenshots" section of the pChart site displays a collection of thumbnails showing the broad variety of charts that one can make using the program.
One of the advantages of all of those samples being included in the installation file, is that you don't even have to be a PHP programmer in order to generate new charts. Simply take an existing one that most closely matches what you wish to create, tweak the data and properties, and regenerate the chart image. That's all that is necessary to create your desired image file. The next logical step would be to reference that file within your PHP or HTML code, to display the image on a Web page.
As noted earlier, one can find a number of PHP charting programs on the Web. While all of them are affordable, pChart may be a good one to start with, since it entails no financial cost. You may in time find that it has all the capabilities that you are looking for, and you can use it for making all the online charts for your website.