Home arrow Perl Programming arrow Page 3 - Basic Charting with Perl

Modifying the Graph - 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

  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



Our graph isn't perfect, though. It has a number of pretty obvious flaws that need to be addressed in order for the graph to look good. For example, nothing is labeled, so it's unclear what the graph even represents. 

So, we'll need to modify our graph's properties a bit, which is actually fairly easy to do. The set method is used, whose arguments operate like a hash whose keys are the property names to be modified and whose values are the new property values. 

Let's start by giving the graph a title, so that viewers will know what they're looking at. The “title” element of the properties hash corresponds to the graph's title. Let's give the graph the title “Average Monthly Temperatures for Portland, OR:”


$chart->set('title' => 'Average Monthly Temperatures for Portland, OR');


Next, we should label each of the axes. For the x-axis, the “x_label” element is used, and for the y-axis, the “y-label” element is used. Here's the updated method call:


$chart->set('title' => 'Average Monthly Temperatures for Portland,OR', 'x_label' => 'Month', 'y_label' => 'Temperature (*F)', );


At this point, rather than stylizing an increasingly messy method call, let's create a hash. That way, each element can be set separately, and the hash can just be passed to the method by name. This will make the new properties easier to display in this article, anyway. Here's the rewritten code:


my %property;$property{'title' } = 'Average Monthly Temperatures for Portland, OR';$property{'x_label'} = 'Month';$property{'y_label'} = 'Temperature (*F)';$chart->set(%property);


The result is much neater for our purposes. 

We should probably change the minimum and maximum values on the y-axis too. Right now, since the range of values is so small, it looks like there is an abnormally large spike in the temperature. This can be fixed by setting a more appropriate minimum. Zero would work as a minimum, and it would put the temperatures in context. In order to set the minimum value, the “min_val” property is used:


$property{'min_val'} = 0;


Since we're setting it at zero, however, it's also possible to set “include_zero”:


$property{'include_zero'} = 'true';


The maximum value can also be adjusted to provide more room at the top. The “max_val” property is used for this:


$property{'max_val'} = 90;


Also, along the y-axis, the numbers are floating point numbers. The graph would look nicer (and there would be more room) if we were to make these numbers integers. This can be done by setting the “integer_ticks_only” property:


$property{'integer_ticks_only'} = 'true';


We should also set a proper interval for the ticks using the “skip_int_ticks” property:


$property{'skip_int_ticks'} = 5;


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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: