Draw Google diagrams from a Perl script

Painting by Data

Author(s):

A CPAN module passes drawing instructions in object-oriented Perl to Google Chart, which draws visually attractive diagrams.

Very few people are seen with Windows laptops at open source conferences nowadays, unless, that is, they really want to be the center of attraction as stone age cave dwellers. For a while, I had been looking around to replace my old laptop when an offering by Dell caught my eye: a cute Mini 9 Ubuntu netbook at an unbeatable price of US$ 230 (Figure 1). So I finally made the move. Leif, a guy from work, even gave the cute gadget a funny nickname, "Mini-Me," after the tiny clone of Dr. Evil in the second Austin Powers movie.

Figure 1: The tiny Dell netbook with Ubuntu.

My first impression was exhilarating; aside from some weird issues with ssh and the wireless driver, which I could resolve online, it actually worked! I then went on to replace the meager 512MB RAM with 2GB from a no-name supplier for just US$ 9.95. But soon after, I got suspicious: Would the netbook now consume more power in suspend mode and prematurely discharge the battery? Being an engineer by trade, I had to investigate.

From Notes to Diagrams

First, I refitted the old memory module, suspended the computer, and read the battery status of the reanimated machine at irregular intervals in the course of the next 36 hours. Figure 2 shows my hand-written notes: a list of discharge percentages and times.

Figure 2: Notes about battery status.

One and a half days later, I repeated this procedure with the 2GB chip reinstated. The two sets of data use irregular and different measuring intervals because of the slightly unorthodox approach. To juxtapose the data graphically, as shown in Figure 3, I first had to run the script in Listing 1 to normalize the data before running the graph-discharge script in Listing 2.

Figure 3: Google Chart visualizes netbook battery performance with different memory chips.

The results show that the batteries initially discharge at about the same speed with either memory chip. As the batteries approach the half-way point to exhaustion, the larger memory module causes the battery to discharge more quickly, which is not worrying, but it's nice to have the hard facts visualized in an attractive diagram.

Listing 1

data-normalize

001 #!/usr/local/bin/perl -w
002 use strict;
003 use DateTime;
004
005 my @result = ();
006 my $max    = {};
007
008 my $data = {
009   "2gb" => [
010     qw(
011   21:33 100 08:18 83 10:52 80
012   18:40 57  08:36 35 12:21 28
013       )
014   ],
015   "0.5gb" => [
016     qw(
017   14:44 100 16:09 97 18:08 95
018   20:43 88  22:19 86 08:47 73
019   15:19 65  17:52 61 21:19 56
020   23:04 55  07:35 43
021       )
022   ]
023 };
024
025 for my $conf ( keys %$data )
026 {
027
028   my $points =
029     $data->{$conf};
030   my $day_start;
031   my $day_current;
032
033   while (
034     my ( $time, $charge ) =
035     splice( @$points, 0, 2 )
036     )
037   {
038
039     my ( $hour, $minute ) =
040       split /:/, $time;
041
042     if (
043       !defined $day_start )
044     {
045       $day_start =
046         DateTime->today();
047       $day_start->set_hour(
048         $hour);
049       $day_start->set_minute(
050         $minute);
051       $day_current =
052         $day_start->clone();
053     }
054
055     my $time_current =
056       $day_current->clone();
057     $time_current->set_hour(
058       $hour);
059     $time_current
060       ->set_minute($minute);
061
062     if ( $time_current <
063       $day_current )
064     {
065       $time_current->add(
066         days => 1 );
067       $day_current->add(
068         days => 1 );
069     }
070
071     $day_current =
072       $time_current->clone();
073
074     my $x = (
075      $time_current->epoch() -
076      $day_start->epoch()
077     ) / 60;
078
079     push @result,
080       [ $conf, $x, $charge ];
081
082     if ( !exists $max->{x}
083       or $max->{x} < $x )
084     {
085       $max->{x} = $x;
086     }
087     if ( !exists $max->{y}
088       or $max->{y} <
089       $charge )
090     {
091       $max->{y} = $charge;
092     }
093   }
094 }
095
096 my $margin = 2;
097
098 for my $result (@result) {
099   my ( $symbol, $x, $y ) =
100     @$result;
101   print "$symbol ",
102     int( $x *
103       ( 100 - 2 * $margin ) /
104       $max->{x} ) + $margin,
105     " ",
106     int( $y *
107       ( 100 - 2 * $margin ) /
108       $max->{y} ) + $margin,
109     "\n";
110 }

Outsourcing the Chart

The chart wasn't drawn by a program running on my local machine, but by a computer in a cluster, courtesy of Google.

The Perl script simply creates a URL, as per Figure 4, and sends it to the Google Chart service, which returns a PNG-formatted image as a result. Google restricts you to 50,000 access attempts per day, which is fine for this example.

In a previous Perl column [2] I used the service to locate spammers on a map of the world.

Figure 4: When presented with this URL, the Google Chart replies with a PNG image of the chart in

Object Orientation vs. the URL Jungle

To construct the URL in Figure 4, the budding chart builder has to follow the Google Chart Developer's Guide [3] carefully and encode various rules painstakingly in hard-to-read abbreviations. To make this easier, CPAN offers the Google::Chart module, which gives you an object-oriented interface to define the chart. It builds the URL step by step with the use of easily understandable method calls.

But before I start defining the chart, I first need to consolidate the measurements.

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