PEAR's Image_Graph to draw graphs
At some point, most developers have a need to create graphs. I've known some to get frustrated and end up exporting the data to a spreadsheet, rather than relying upon the graphing capabilities of Open Office or Excel. That can be the way to go in certain cases, but not because there's no alternative. PHP has some great graphing libraries; JpGraph is probably the most well-known, but it can require a commercial licence. This article looks at PEAR's Image_Graph package, which is released under the Lesser GPL. It's a package that has very little documentation, but which deserves more recognition. It's helpful to have used PEAR before, but if this is the first PEAR package you'll use, you'll probably cope just fine.
Formerly a SourceForge package known as GraPHPite, it merged with and took the name of an older PEAR Image_Graph package. The graphs, charts and plots produced by Image_Graph are highly customizable, and can be any of area, band, bar, box and whisker, candlestick, impulse, map, line, pie, radar, scatter, smoothed line and step.
To install Image_Graph, you need:
* PHP 4.3.0 or later (it works with PHP 5)
* GD (GD 2 is recommended)
* PEAR support
* The Image_Canvas package
* The Image_Color package
Installing packages on PEAR is easy using the pear command.There are two main ways to install a PEAR package. You can install directly from the PEAR server, or you can download and install from your local machine. The mechanism is effectively the same. Some packages are still in alpha, including Image_Graph, so it won't appear on the main list of stable packages that are available (which you can view by running pear remote-list.
In this article we'll install by downloading. Assuming you have a default install, you'll need to download the following three packages: Image_Color, Image_Canvas and Image_Graph. Note that there may be newer versions out by the time you do this. It's unlikely, but you should also check to see if there're any new dependencies (they'll be listed on the download page). To install them, simply run the pear install command. Make sure you get the order right to avoid any dependency failures.
$ pear install Image_Color-1.0.2.tgz
install ok: Image_Color 1.0.2
$ pear install Image_Canvas-0.3.0.tgz
install ok: Image_Canvas 0.3.0
$ pear install Image_Graph-0.7.2.tgz
package `Numbers_Roman' is recommended to utilize some features.
package `Numbers_Words' is recommended to utilize some features.
install ok: Image_Graph 0.7.2
The Numbers_Roman and Numbers_Words packages are optional extras, but we're not going to use them for our purposes here.
Creating a bar chart is probably easiest of all. Let's start with some sample code to do just that. The examples are going to hardcode the data, but I'm sure you're more than capable of reading the data from a database or some other source, and that'll just complicate the graphing examples.
is all that's required to use the Image_Graph package. Include it first in any code that will need graphing. The next line,
$Graph =& Image_Graph::factory('graph', array(600, 300));
instantiates the graph object, and creates a canvas 600 pixels wide by 300 pixels high. It makes use of the Factory design pattern. You don't have to use this pattern, but it makes your life a bit easier. A discussion of the factory design pattern is beyond the scope of this article, but all it really means for our purposes is that you usually only need to have the one include statement (above). The next line:
$Dataset =& Image_Graph::factory('dataset');
creates a dataset, which will contain the data to use in creating the graph. There are two main types of datasets: the ordinary one, used here, which is basically an array of data points, and a more complex function-based one, which we'll look at later. Next, the data points are added:
Having defined the plotarea, as well as the data points, all that's needed is to define the plot that will appear on the plotarea.
$Plot =& $Plotarea->addNew('bar', &$Dataset);
There can be multiple plots, but in this case there's just one. There are also multiple plot types (as mentioned earlier), but we're just adding a simple bar graph for now. Finally, the done method renders the graph:
Try experimenting with different values, and different numbers of values. You'll notice that the largest value always fills up 100% of the space, and the others fill an area in proportion to this. If 10 is your highest value, a point with a value of 5 fills up 50%, while if 20 is your highest value, the 5 fills up 25%. All this should be expected with bar graphs.
With the default settings, your graph is hardly going to win any awards. Let's make it a little more readable with some colors.
Re: PEAR's Image_Graph to draw graphs
How can i directly link with database instead of adding value manually by addpoint(....) in dataset?
I am using mysql database.
|All times are GMT +5.5. The time now is 13:12.|