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
In order for the graph to be created, it needs some real data. We'll be working with average monthly temperatures. Let's use data from Portland, Oregon, and let's just use the high values for now. (This data will be taken from The Weather Channel).
As has already been said, data is stored in an array. The array is actually a multidimensional array, however. The first element of the array should be a reference to an array of labels. In our case, the labels would be the month names. Subsequent elements should contain references to arrays containing the actual values corresponding with those labels. Each of these data elements is called a dataset.
So, if we want to graph the average monthly high temperatures for Portland, @data should be defined like this:
If we call the png method now, then an image will be created named temps.png, containing the resulting chart. The image should look like this:
Now we should plot the average monthly low temperatures. If we do this, though, then the @data definition is going to start looking really messy. Fortunately, there is another way to add data which, in some cases, may make things easier and neater. Using the add_dataset method, it is possible to add datasets directly, without the need to create arrays that will only be used once. You just need to pass in the values as arguments. Let's go ahead and get rid of @data by using the add_dataset method, and then add data for low temperatures: