Perl Programming Page 4 - Basic Charting with Perl |
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.
blog comments powered by Disqus |
|
|
|
|
|
|
|