Draw Google diagrams from a Perl script
Painting by Data
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.
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.
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.
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.
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.
Our Services
Direct Download
Tag Cloud
News
-
Google and NASA Partner in Quantum Computing Project
Vendor D-Wave scores big with a sale to NASA's Quantum Intelligence Lab.
-
Mageia Project Announces Mageia 3 Linux
Many package updates and Steam integration highlight the latest from the Mandriva-based community Linux.
-
FSF Outs the World Wide Web Consortium over DRM Proposal
Richard Stallman calls for the W3C to remain independent of vendor interests.
-
Debian 7.0 Debuts
The new release supports nine architectures, 73 human languages, and zero non-Free components.
-
Alpha Version of Fedora 19 Released
Fedora developers release the first alpha version of Fedora 19, known as Schrödinger’s Cat, for general testing. The final release is expected in July 2013.
-
ack 2.0 Released
ack is a grep-like, command-line tool that has been optimized for programmers to search large trees of source code.
-
SUSE Studio 1.3 Released
New features in SUSE Studio 1.3 include enhanced cloud integration, VM platform support, and lifecycle management.
-
Xen To Become Linux Foundation Collaborative Project
The Linux Foundation recently announced that the Xen Project is becoming a Linux Foundation Collaborative Project.
-
RunRev Releases Open Source Version of LiveCode
Open source version of LiveCode is now available for developing apps, games, and utilities for all major platforms.
-
OpenDaylight Project Formed
OpenDaylight is an open source software-defined networking project committed to furthering adoption of SDN and accelerating innovation in a vendor-neutral and open environment.
