Perl Programming Basic Charting with Perl |
One way of presenting data is in a chart. There are a variety of chart types, each of which has its particular strength. Perl, not surprisingly, offers a wealth of modules for creating charts to present data. In this article, we'll examine a collection of modules, appropriately named Chart, that allows for the creation of charts. We'll take a look at using the module to create some basic charts. Getting Started There's very little to explain about charts in general that you don't already know, so let's jump right into the module by creating a basic chart. The Chart module is, of course, available from CPAN, so obtaining it is simple:
$ cpan Chart::Base
Remember that Chart is actually a collection of modules, rather than a single module. Chart's usage reflects this. Instead of importing a single base module, as one might expect, we must actually import the module associated with the particular chart we want to create. Chart supports a number of chart types, but let's start with something simple. Say we want to graph average monthly temperatures for a particular city. A line graph would work well for this. So, let's use the Chart::Lines module:
use Chart::Lines;
Next, we have to instantiate a Chart::Lines object. Every operation is conducted through this object. The only parameters required are the desired width and height of the chart image. Let's make the chart 600 pixels wide and 400 pixels high:
my $chart = new Chart::Lines(600, 400);
Once a Chart::Lines object is instantiated, we can generate a complete chart with only a single method call. All we need is the name of the resulting image, and a reference to the data to be represented by the chart. The name of the image is a string, and the reference is to an array. Let's assume that we already have data inside an array named @data, and that we want to create a chart named temps.png. To do this, we'd call the png method, like this:
$chart->png('temps.png', @data);
As you can see, there's not much to it. Of course, now we need some data.
blog comments powered by Disqus |
|
|
|
|
|
|
|