Home arrow Perl Programming arrow Basic Charting with Perl

Basic Charting with Perl

In programming, data manipulation is an everyday task. Programmers must be able to take raw data and perform some operation, or a set of operations, on that data to modify it. However, presenting data is also very important, both in programming and in other tasks. This article will cover one of the ways you can present data in Perl

TABLE OF CONTENTS:
  1. Basic Charting with Perl
  2. Adding Data
  3. Modifying the Graph
  4. Modifying the Graph, Continued
By: Peyton McCullough
Rating: starstarstarstarstar / 2
May 20, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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. 



 
 
>>> More Perl Programming Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: