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