Let’s Make Some Graphs!
The GD::Graph package was written by Martien Verbruggen. It should run on any operating system on which the GD module is available. GD::Graph uses the GD module for all of its drawing and file primitives, and also relies on the GD::TextUtil package. Both packages are available from CPAN at http://www.cpan.org/authors/id/MVERB/.
Graph construction with GD::Graph can be broken into three
phases. First you need to gather the data, parse it, and organize it
in a form that you can pass to the graph drawing routines. Then you
set the attributes of the graph such that it will come out the way you
want it. Finally, you draw the graph with the plot( ) method.
The data for the graph must be in a very particular form before you plot the graph. The plotting methods expect a reference to an array, where the first element is a reference to an anonymous array of the x-axis values, and any subsequent elements are data sets to be plotted against the y axis. A sample data collection looks like this:
#!/usr/bin/perl -w use GD; # for font names use GD::Graph::lines; my @data = ( [ qw(1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 ) ], # timespan of data # thousands of people in New York [ 2, 5, 16.8, 18, 19, 22.6, 26, 32, 34, 39, 43, 48, 49, 49, 54.2, 58, 68, 72, 79 ], # thousands of people in SF [ 11, 18, 29.4, 35.7, 36, 38.2, 36, 41, 45, 49, 50, 51, 51.4, 52.6, 53.2, 54, 67, 73, 78 ], # thousands of people in Peoria [ 5, 8, 24, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access