Timeline Diagrams with Perl

Diagram of the "begat" sequence from Genesis 5

Two weeks ago, I introduced you to my new module SVG::TrafficLight and hinted that there were more SVG-based modules to follow. Today, I’d like to talk about the next one – SVG::Timeline.

It all started over a year ago when I was looking through some of the more ridiculous religious questions on Quora when I came across one asking why Adam wasn’t mentioned in the Bible after the first couple of books in Genesis. As part of my answer I wanted to illustrate just how long a time Genesis 5 covers.

I knew that SVG would be the best approach and it only took half an hour or so to whip up the image you can see in my answer (and there’s a newer version, generated with the current version of the code, at the top of this page). It’s important to note that  I didn’t hand-craft SVG that drew the diagram – I wrote code that generated the diagram from an input file.

I then realised that this could be a more generally useful tool, so I set about making the code more generic. It languished on Github for a year or so before I decided it was useful enough to clean it up and release it to CPAN. Let’s take a quick look at how it works.

As you can see from the example above, a timeline is made up of a number of events. An event has a start date, an end date and some text. So you can start a timeline diagram with code like this:

And once you have added all of your events, you can produce the timeline using:

That code writes the SVG document is written to STDOUT, so you’ll probably want to redirect that to a file.

That will draw a timeline of your events using all of the default settings (which, in most cases produce a useful diagram). There are plenty of options that you can pass to the object constructor to tweak things. The most useful are probably the aspect ratio (if your diagram is going to be particularly long or thing – the default is 16/9) and the number of years between gridlines in  the output (the default is ten and you might want to change that if your timeline covers a particularly large or small number of years – like the Genesis example above).

The default behaviour is to colour all of the events the same colour (which can be changed from the default in the constructor for the SVG::Timeline object). But you can also change it for each individual event by adding an optional “colour” parameter to the add_event() call.

But that’s all a lot of work for the simple case. So the distribution also includes a command line program called timeline which does all of that for you. It reads a datafile and produces a timeline diagram based on  the contents.

Each record in the input file has three or fields separated by tabs. The fields are the parameters for the add_event() call in the order: text, start, end and (optionally) colour.

There are example data files in the distribution for producing some of the timelines I’ve talked about in this article – along with shell scripts showing how to produce timeline diagrams using the command line program.

There are a few things I’d like to add. Support for events with unknown dates (perhaps fading the colour towards the unknown end). Diagrams that go vertically instead of horizontally and support for events that begin and end in the same year (currently, they are zero size and just vanish – I discovered that when I added Paul McGann and Christopher Eccleston to the Doctor Who example).

I find the program… well, if not exactly useful, it’s still fun to play with.

Please let me know if you produce any interesting timelines with it.

Drawing Traffic Lights With Perl

Traffic Lights

For a thing (that you may hear more about at some point in the future) I needed diagrams of traffic lights. But Google Image Search didn’t really have what I was looking for. Everything was either too realistic or not CC-licensed so I could use the images how I wanted.

So I decided to do it myself. But I’m not exactly artistic. I far prefer it when I can get computers to draw images for me. I’ve dabbled with SVG before and it seemed like the perfect tool for the job. And there’s a module from CPAN that makes it simple to create SVG images from Perl.

It only took an hour or so before I was drawing images like the one above – which was exactly what I was looking for.

Initially, I shared my code as a Gist, but since then I’ve extracted the useful bits into a module which I’ve uploaded to CPAN as SVG::TrafficLight. I’ve tried to make it as configurable as possible, so you should be able to use it for all your traffic light drawing needs as well.

Starting to use it is pretty simple.

The default sequence of lights shows the UK’s standard traffic light sequence (green,  amber, red, red and amber, green) but it’s simple enough to produce a different sequence (even one that you would never see on the roads).

If you read the documentation, you’ll see how you can customise pretty much anything in the diagram – the size of the lights, the padding between them, even the colours used.

Let me know if you find it all at useful. SVG is fun. I’ll think I’ll investigate it some more.

 

Several Small Bits of News

A few little bits and pieces, none of which justify a blog post to themselves.

blogs.perl.org

Some of you will have seen that Evozon’s grant to replace blogs.perl.org was cancelled a couple of months ago. This made me sad as I (along with the rest of the blogs.perl.org team) really want to see the current, fragile, set-up replaced as soon as possible.

I’m happy to see that a new grant proposal has been received from a team at Booking.com. They want to take Evozon’s work, along with some other improvements that they’ve made in house, and complete the project.

I’d really like to see this grant approved and the project completed. Please feel free to add your comment to the proposal.

Perl News

Who remembers use.perl.org? For many years it was the best place to go for both Perl news and Perl blogs. The idea behind blogs.perl.org was to replace the blogging part of that site and a few years ago, Leo Lapworth and I built perlnews.org to replace the other part of the equation.

Unfortunately, neither of us really had the time to invest in the site and it never really took off. These days there are plenty of other places to get your Perl news, so we’ve taken the decision to close the site down. The existing stories will remain online and I might replace the current WordPress installation with a static site at some point in the future.

The Perl Conference in Amsterdam

A couple of my recent blog posts have been about deciding what training course to run alongside The Perl Conference (The Conference Formerly Known As YAPC Europe) in Amsterdam.

Unfortunately, my plans had a big collision with Real Life and I’ve realised that it’s just unrealistic for me to have enough time to prepare for the conference. So, sadly, I’ve made the decision that I won’t be in Amsterdam this August.

I’m sure it’ll be a great conference though and I wish the organisers the best of luck with it.

Web Application Development in Perl 6

Gabor asked me to give him a quotation explaining why I had backed his Indiegogo campaign to write a book on web development with Perl 6. This is what I sent him:

I’ve been largely ignoring Perl 6 development since the project started in 2000. I figured that I would have plenty of chance to catch up with it before clients started expecting me to know it. The official release of Perl 6 eighteen months ago means that the time is now right for me to start taking an interest. A lot of the code I write drives web sites, so I want to get up to speed with web development in Perl 6 quickly. That’s why I supported this crowdfunding campaign – I want to read this book and I think that Gabor is the right person to write it.

I think this will be a very useful book. You might consider backing it too.

CPAN Badges

I’m a big fan of the badges from shields.io. I use their CPAN badge on my dashboard. Unfortunately, this badge has stopped working – it just says “cpan | invalid”.

I did some investigation and discovered this was because they use the MetaCPAN v0 API – which has now been switched off. It was simple enough to patch the code to use the v1 API. I’ve sent them a pull request, but it hasn’t been accepted yet.

I Wrote Some Perl

Ok, yes, I’ve been writing Perl for over twenty years. But Perl 5.26 was released this week and for the first time, my name is mentioned in the release notes. Because I have not one, but two fixes in this release of Perl.

The first is this commit which fixes a piece of documentation to make it clear that grep() returns a list, not an array.

The second is this commit which fixes some sample code so that it runs without warnings under use strict.

It’s a small start, I admit, but I have a taste for it now. In a years time, I hope to report that I have more than two commits in Perl 5.28.

And you can help too. Instructions on how to contribute are in the perlhack manual page. There is more information in perlhacktips and perlhacktut.

The people working on Perl all do a great job. But it’s a hard job and it might just get a little easier if more of us helped out.

Amsterdam Training Questionnaire

Update: I’ve closed this survey earlier than I expected to. For various personal reasons, I’ve decided that I just don’t have the time to attend the conference in Amsterdam.


It was back in the middle of March that I first raised the question of running some training in conjunction with the Perl Conference in Amsterdam this August. I didn’t mean to leave it so long before following-up, but I’ve a lot of real life to deal with over the last couple of months and I’m afraid a lot of my digital life got shoved to one side.

But I’m back now and we should really get something organised for Amsterdam.

So here’s a Google Form for you to fill out, to tell me what training course you’d like to see me run in Amsterdam. I’ll leave it running for a couple of weeks before making a decision.