Draw Google diagrams from a Perl script

Painting by Data

Article from Issue 105/2009

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



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.

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Google Chart

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

  • Data Visualization in Python

    Python's powerful Matplotlib, Bokeh, PyQtGraph, and Pandas libraries lend programmers a helping hand when visualizing complex data and their relationships.

  • 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.

  • yEd

    Build complex data structures into simple but illuminating diagrams using the yEd graph editor.

comments powered by Disqus

Direct Download

Read full article as PDF: