Drawing a pie chart with GD::Graph::pie - Page 21
November 12, 2001
Drawing a pie graph is basically as easy as drawing any other
type of graph with the GD::Graph module. The President would like
you to draw a pie chart that contains all of the sales categories
including online, catalog, store fronts, and resellers. He will
provide a CSV file that is exported from Excel each week to a
particular directory on the network. We will write a script that
can be automatically executed every week to import the CSV and
produce the graph.
On line 3 of the example below, we load the Text::CSV_XS
module, which is freely available on CPAN and is used to parse
CSV files. On line 7, we create a new instance of the module,
open the CSV file on line 8, and iterate over each row of the
file on lines 9-13. Line 10 calls the parse() method,
which splits the record into columns. We get the columns on line
11 with the fields() method and add the row to the
@data array on line 12. So instead of setting the
@data array manually, we can set it from an external file,
like a CSV file.
1 use strict;
2 use GD::Graph::pie;
3 use Text::CSV_XS;
4
5 my @data;
6
7 my $csv = new Text::CSV_XS;
8 open(FILE,"sales1.csv") || die "Cannot open sales1.csv: $!\n";
9 while (my $line = <FILE>) {
10 $csv->parse($line);
11 my @col = $csv->fields;
12 push(@data,\@col);
13 }
14
15 my $graph = new GD::Graph::pie(300, 300);
16
17 $graph->set(
18 title => 'Revenue by Category for 2001',
19 label => 'Category',
20 axislabelclr => 'black',
21 '3d' => 1,
22 start_angle => 90,
23 suppress_angle => 5,
24 )
25 or warn $graph->error;
26
27 $graph->set_title_font("/usr/share/fonts/ttf/windows/times.ttf", 18);
28 $graph->set_value_font("/usr/share/fonts/ttf/windows/times.ttf",12);
29 $graph->set_label_font("/usr/share/fonts/ttf/windows/times.ttf",14);
30
31 $graph->plot(\@data) or die $graph->error;
32
33 open(GRAPH,">graph5.jpg") || die "Cannot open graph5.jpg: $!\n";
34 print GRAPH $graph->gd->jpeg(100);
The 3d parameter on line 21 is set to 1, which tells the
module to draw a 3D pie chart rather than a 2D pie chart. With
pie charts, we also need select the first degree in which the
first pie slice will be drawn. The default is 0, but we set it to
90 on line 22 with the start_angle parameter. We also set
the suppress_angle to 5, which tells the module to ignore
a pie slice that will take up less than 5 degrees of the total
pie. We're also using True Type fonts in this graph whereas the
previous examples used builtin fonts. Lines 27-29 set the fonts
for the title, pie slices, and labels. Line and bar charts can
also use True Type Fonts for various text elements in the graph.
In addition the the font methods above, you can also use the
following functions to set fonts where font specification
is the path to the TTF file followed by a comma and a point size
(the same format as the methods above):
- $graph->set_x_label_font(font specification)
- $graph->set_y_label_font(font specification)
- $graph->set_x_axis_font(font specification)
- $graph->set_y_axis_font(font specification)
- $graph->set_values_font(font specification)
Conclusion
Well, another day, another problem solved with Perl. Thanks to
the author of the GD::Graph module, creating charts with Perl is
rather simple. There are many other possibilities and options for
drawing charts with this module, so I recommend downloading it
for yourself and reading through the documentation to get a
better feel for what it can do.
Drawing a line chart with GD::Graph::lines - Page 20
Weaving Magic With Regular Expressions
Creating An Index Image - Page 22
|