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

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



The graph now looks a lot better than it did originally:



However, there are two changes I can think of which should be made before the chart is perfect. First, the lines need to be given names other than “Dataset 1” and “Dataset 2.” In order to name these lines, the “legend_labels” element must be set. The value of this element must be a reference to an array. The elements of this array correspond to the names of the datasets. Let's name the first line “High” and the second line “Low”:


$property{'legend_labels'} = ['High', 'Low'];


The default color of the first line is appropriate, since red is associated with heat. However, the default color of the second line is not appropriate. Blue would be a much more appropriate color, since blue is associated with coldness.

Fortunately, the color of anything in the graph can be changed using the “colors” element of the properties hash. The value of this element must be a hash. Within the colors hash, the colors of individual items can be changed. In order to change the color associated with a dataset, which is what we want to do, the “datasetN” property of this hash is set, where N is the zero-based number of the dataset we want to change. Let's go ahead and change the color of the second line, as well as explicitly specify the color of the first line:


$property{'colors'} = {'dataset0' => [255, 0, 0], 'dataset1' => [0, 0,255]};


Now the graph is perfect. It represents the data appropriately, which is the goal. The viewer can look at the graph and instantly tell what the graph represents, and he can see the general trend of the data. 

Before we end, however, there is one more interesting thing I'd like to show you. Remember how each graph type corresponded to the name of a class? Well, let's say that we don't like the average monthly temperature represented by a line graph. Let's say that we'd rather have a bar graph. In order for us to change the graph from a line graph to a bar graph, we'd only have to substitute Chart::Lines for Chart::Bars in two places:


use Chart::Bars;my $chart = new Chart::Bars(600, 400);


The result is a bar graph which conveys the exact same data, but in another format:



However, we only had to change two lines in the source code to achieve this. This consistency across multiple chart formats makes Chart particularly appealing. If you create a program that creates a chart of a particular type, and later want to change the type, then it's very easy to do so.

Of course, there is more to Chart, including a lot more properties that can be changed to meet your needs, but, as you can see, the basics are very simple, making Chart an indispensable tool for data representation in Perl.

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