Drawing A Comparative Bar Graph - Page 19
November 12, 2001
Let's say that you've deployed the script above so that your
sales manager can track online sales. That's great, but now the
President of the company wants to be able to track online sales
in comparison to catalog sales. "Of course" you say. With
GD::Graph, adding an addition row is only a matter of
adding a new array to our @data array on line 7 below. We
also change the title on line 15 to reflect the contents of this
new chart. We'll also need to create a color coded legend to
identify which bar represents online sales and which represents
catalog sales. The set_legend() method on line 22 assigns
a title for the bar in the order that they occurred in the
@data array. The first data array (the array after the
labels array ) represents online sales, while the last array
represents catalog sales.
We've also added two additional settings on lines 17 and 18. When
the long_ticks parameter is set to a positive value, it
draws a grid behind the graph to make it easier to match a value
in the y axis with the height of the bar. The show_values
parameters writes the value directly above each point or bar as
can be seen in the image below.
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 [58,55,62,48,45,44,41,35,43]
8 );
9
10 my $graph = new GD::Graph::bars;
11
12 $graph->set(
13 x_label => 'Month',
14 y_label => 'Revenue ($1,000s)',
15 title => 'Monthly Online vs. Catalog Sales for 2001',
16 bar_spacing => 3,
17 long_ticks => 1,
18 show_values => 1,
19 )
20 or warn $graph->error;
21
22 $graph->set_legend( 'Online', 'Catalog' );
23
24 $graph->plot(\@data) or die $graph->error;
25
26 open(GRAPH,">graph2.jpg") || die "Cannot open graph2.jpg: $!\n";
27 print GRAPH $graph->gd->jpeg(100);
[The colored lines above are one line. They have been split
for formatting purposes.]
This graph provides the information that the President of the
company was looking for. The online sales increased the first
half of the year while catalog sales remained stagnant and even
decreased a bit. This could have been a result of sagging sales
or could represent a certain portion of the customers who have
switched to online ordering over catalogs. It also shows a heavy
drop-off in online sales in September.
Drawing a stacked bar graph
The sales manager and President have been impressed with your
ability to develop dynamic these dynamic sales graphs for the
company. Now that they know you have this skill, they would like
you to create a new graph that represents combined online and
catalog sales. We can do this with a stacked bar chart. The code
for this graph looks almost identical to the previous example
with the exception of line 18, the cumulate parameter,
which stacks the bars one on top of another.
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 [58,55,62,48,45,44,41,35,43]
8 );
9
10 my $graph = new GD::Graph::bars;
11
12 $graph->set(
13 x_label => 'Month',
14 y_label => 'Revenue ($1,000s)',
15 title => 'Monthly Revenue Totals for 2001',
16 bar_spacing => 3,
17 long_ticks => 1,
18 cumulate => 1
19 )
20 or warn $graph->error;
21
22 $graph->set_legend( 'Online', 'Catalog' );
23
24 $graph->plot(\@data) or die $graph->error;
25
26 open(GRAPH,">graph3.jpg") || die "Cannot open graph3.jpg: $!\n";
27 print GRAPH $graph->gd->jpeg(100);
[The colored lines above are one line. They have been split
for formatting purposes.]
The stacked graph above shows us the total sales for each month,
but also color codes the portion of each bar based upon the
portion of the total that represents online or catalog sales.
Automating Image Manipulation with GD - Page 18
Weaving Magic With Regular Expressions
Drawing a line chart with GD::Graph::lines - Page 20
|