Automating Image Manipulation with GD - Page 18
November 12, 2001
|
In the last article, we learned how to use the the GD library to
draw borders, add text, and create thumbnails. In this article,
we will learn how to create charts and graphs with the GD::Graph
module.
|
Drawing Charts and Graphs with GD::Graph
Creating a chart or graph within a spreadsheet like Excel is
simple enough, however, if the chart needs to be created on a
daily basis from a data set that changes regularly, this can be a
tedious repetitive task that should be automated. And let's say
that the charts and graphs need to be accessible from the Web.
Well, we just described a perfect scenario for using the
GD::GRaph module to automate this type of task. With it,
we can create several different types of graphs including line
charts, bar charts, pie charts, point charts, and area charts.
This can be useful for reporting on bits of data like system
status, network traffic, Web server traffic and revenue reports.
What the GD::GRaph doesn't do for you is grab and tabulate
the information that will be used to create the chart. You have
to do that yourself. You will need to create a multidimensional
array whose first array contains the list of labels that will be
used in the chart and whose subsequent arrays will contain the
data to graph. For the majority of the charts you will likely
generate, the first array will represent the X axis labels. In
the case of a pie chart, the labels are the pie slice labels.
Subsequent arrays contain the data that will be used in the
chart. For example, if we wanted to report monthly revenues for
January through September in a bar chart, we would need to create
a data structure like the one below:
my @data = (
["Jan-01","Feb-01","Mar-01","Apr-01","May-01",
"Jun-01","Jul-01","Aug-01","Sep-01"],
[21,25,33,39,49,48,40,45,15]
);
[The colored lines above are one line. They have been split
for formatting purposes.]
The GD::Graph module has too many features to list in this
article, however, in the examples below, you will discover that
the primary method for controlling chart output is the
set() method. The plot() method accepts a data
structure like @data above and creates the chart image, which can
then be saved to disk and otherwise manipulated with the GD
library. We will learn how to create 5 different types of charts
below using monthly sales information as the data set.
Draw a bar graph with GD::Graph::bars
The GD::Graph::bars class provides the capability for
creating bar graphs like the one below. The first thing we need
to do is create the data structure that will be used to draw the
chart. On lines 4 through 7, we create an array that contains two
anonymous arrays. The first anonymous array contains the X axis
labels that will identify the values in the second anonymous
array, which contains the online sales figures in thousands of
dollars.
On line 9, we create a new instance of the GD::Graph::bars
class and then set the chart parameters with the set()
method on lines 11 through 17. The x_label parameter on
line 12 sets the X axis label that describes the type of
information that we will display in the bars. The y_label
parameter on line 13 contains the description of the number
values in the Y axis, which in this case, is revenue in thousands
of dollars. The value of the title parameter on line 14 is
centered and drawn across the top of the image. The
bar_spacing parameter on line 15 is the number of pixels
that should exist between the bars in the image.
Once we've set the chart parameters and created the proper data
structure, we pass them to the plot() method on line 19,
which actually creates the image based upon the previous
settings. Then we can save the image as a JPEG on lines 21 and
22.
1 use strict;
2 use GD::Graph::bars;
3
4 my @data = (
5 ["Jan-01","Feb-01","Mar-01", "Apr-01","May-01",
"Jun-01","Jul-01","Aug-01","Sep-01"],
6 [21,25,33,39,49,48,40,45,15]
7 );
8
9 my $graph = new GD::Graph::bars;
10
11 $graph->set(
12 x_label => 'Month',
13 y_label => 'Revenue ($1,000s)',
14 title => 'Monthly Online Sales for 2001',
15 bar_spacing => 10
16 )
17 or warn $graph->error;
18
19 $graph->plot(\@data) or die $graph->error;
20
21 open(GRAPH,">graph1.jpg") || die "Cannot open graph1.jpg: $!\n";
22 print GRAPH $graph->gd->jpeg(100);
[The colored lines above are one line. They have been split
for formatting purposes.]
As you can see in the image above, our chart maps out monthly
online sales to date for 2001 given the information in the
@data array.
Creating Thumbnail Images - Page 17
Weaving Magic With Regular Expressions
Drawing A Comparative Bar Graph - Page 19
|