Draw Google diagrams from a Perl script

Warping Time

During my measurements, I read off the battery discharge values at irregular intervals that do not coincide for the two memory chips. To juxtapose the two discharge curves for a direct comparison, the data-normalize script (Listing 1) pushes the times onto a common time axis. Both sets of measurements then start at a virtual hour 0.

Starting with absolute points in time, the script calculates relative times by subtracting the virtual starting time.

For example, if the first set includes measurements at 8:00 and 9:00 and the second set has values for 11:00 and 11:30, the shared virtual time axis would start at 0:00 and include a second data point for set 2 at 0:30 and for set 1 at 1:00.

To do so, the data-normalize script uses the $data structure, which contains a reference to a hash with keys "2gb" and "0.5gb", each referencing an array containing points with pairs of time and battery capacity.

Normalize Between 2 and 98

The goal of this approach is to start the two graphs at a common point and normalize both the time values on the X axis and the measured battery capacity values on the Y axis within an integer range of 0 through 100, as the Chart service expects of the data.

The script uses the CPAN DateTime module for these calculations and sets the time of the first, randomly selected, series of measurements as the starting point of the common time axis as $day_start. It stores the time of the current measurement as $time_current, whereas the day on which the last measurement was performed is stored as $day_current.

If the script finds out that a day has passed between two measurements (for example if a measurement at 23:00 is followed by a measurement at 8:00), lines 65 and 67 add a day to the counters. Lines 74 to 77 ascertain the number of minutes since the last measurement and stores the value in the variable $x (for X axis values), where it is picked up in line 80 and dumped into the @results array, along with the name of the set of measurements and the current battery value.

Lines 85 and 91 keep the highest X and Y values and stores them in the $max->{x} and $max->{y} hash entries, respectively. They are used later in the for loop in lines 98ff. to normalize all the X/Y values within a range of 0 to 100. Additionally, the $margin variable creates a margin with a width of 2 on the right and left before data-normalize finally normalizes the X/Y values within a range of 2 and 98. The reason for this becomes apparent in the chart later on; it just looks better if the graphs don't touch the horizontal and vertical axes. The print statement in line 101 sends the results to STDOUT (Figure 5).

Figure 5: Results are sent to STDOUT.

Type XY

While looking for a suitable chart format, I found the "lxy" type [3], which expects a set of X and Y coordinates for any drawn line. This makes it fundamentally different from other formats, which assume that the measured values are on the same X coordinates for any records they visualize. Line 26 in graph-discharge thus defines 'XY' for the type option in the constructor call for a new Google::Chart type object.

Listing 2 first reads the output from the data-normalize script (line 10) and sorts the X and Y values into the $data structure. At the end of the while loop in line 21, $data->{"0.5gb"}->{x} contains all the normalized time stamps for the 512MB configuration, and $data->{"0.5gb"}->{y} is an array of the corresponding battery states. This makes it easy to pass a pointer to an array of X/Y records to the Google::Chart object's data parameter in line 28, just as the XY chart type expects.

The size parameter (line 35) sets the dimensions of the chart: 750 by 400 is about the maximum; if you try for larger dimensions, Google objects and returns a "Bad Request" message.

The chart title, which is displayed at the top of the chart, is set by the title option in line 39.

Listing 2

graph-discharge

001 #!/usr/local/bin/perl -w
002 use strict;
003
004 use Google::Chart;
005 use Google::Chart::Marker;
006
007 my $data = {};
008
009 open PIPE,
010   "./data-normalize |"
011   or die;
012 while (<PIPE>) {
013   chomp;
014   my ( $symbol, $x, $y ) =
015     split ' ', $_;
016   next unless $y;
017   push @{ $data->{$symbol}
018       ->{x} }, $x;
019   push @{ $data->{$symbol}
020       ->{y} }, $y;
021 }
022 close PIPE or die;
023
024 my $graph =
025   Google::Chart->new(
026   type => 'XY',
027
028   data => [
029     $data->{"0.5gb"}->{x},
030     $data->{"0.5gb"}->{y},
031     $data->{"2gb"}->{x},
032     $data->{"2gb"}->{y},
033   ],
034
035   size => '750x400',
036
037   title => {
038     text =>
039 "Dell Mini Standby Discharge"
040   },
041
042   fill => {
043     module =>
044       "LinearGradient",
045     args => {
046       target  => "c",
047       angle   => 45,
048       color1  => "abbaab",
049       offset1 => 1,
050       color2  => "FFFFFF",
051       offset2 => 0,
052     }
053   },
054
055   grid => {
056     x_step_size => 33,
057     y_step_size => 20,
058   },
059
060   axis => [
061     {
062       location => 'x',
063       labels => [ 1 .. 36 ],
064     },
065     {
066       location => 'y',
067       labels   => [
068         0, 25, 50, 75,
069         100
070       ],
071     },
072   ],
073
074   color =>
075     [ 'E6E9FD', '4D89F9' ],
076
077   legend =>
078     [ '0.5gb', '2gb' ],
079
080   margin => [
081     50,  50, 50, 50,
082     100, 100
083   ],
084
085   marker =>
086     Google::Chart::Marker
087     ->new(
088     markerset => [
089       {
090         marker_type => 'x',
091         color   => 'FFCC33',
092         dataset => 0,
093         datapoint => -1,
094         size      => 15,
095         priority  => 1,
096       },
097       {
098         marker_type => 'x',
099         color   => 'FF0000',
100         dataset => 1,
101         datapoint => -1,
102         size      => 15,
103         priority  => 1,
104       },
105       {
106         marker_type => 'D',
107         color => 'E6E9FD',
108             # light blue
109         dataset   => 0,
110         datapoint => -1,
111         size      => 4,
112         priority  => -1,
113       },
114       {
115         marker_type => 'D',
116         color =>
117           '4D89F9',    # blue
118         dataset   => 1,
119         datapoint => -1,
120         size      => 4,
121         priority  => -1,
122       },
123     ]
124     ),
125   );
126
127 $graph->render_to_file(
128   filename => "chart.png" );
129 system("eog chart.png");
130

Read full article as PDF:

Related content

  • Google Chart

    The Google Chart API lets you draw custom graphs, charts, maps, and barcodes through a simple web interface.

  • Perl: Jawbone UP Data

    The Jawbone UP electronic bracelet measures the wearer's daily activity and nocturnal sleep patterns. If you are bored by the official smartphone app, you can create your own Perl scripts and access your data via the unofficial web API.

  • Perl: Pinpointing Spammers

    To identify the geographic regions from which link spam originated, a database locates IP addresses and the Google Charts service puts them onto a world map.

  • Tapping Data with the RaspPi

    If you have a weather station that allows you to access data via a USB port, you can use your Raspberry Pi to analyze the data and publish the results via a web application.

  • In-cell Charting in Calc
comments powered by Disqus

Direct Download

Read full article as PDF:

News

njobs Europe
What:
Where:
Country:
Njobs Netherlands Njobs Deutschland Njobs United Kingdom Njobs Italia Njobs France Njobs Espana Njobs Poland
Njobs Austria Njobs Denmark Njobs Belgium Njobs Czech Republic Njobs Mexico Njobs India Njobs Colombia