Thursday, March 26, 2009

URI::GoogleChart - Generate Google Chart URIs




Google Charts. Everything goes just fine until you want to do something slightly complicated, like this (left) which involves a complex URL like this:
http://chart.apis.google.com/chart?cht=lxy&chs=200x125&chco=3072F3,FF0000&chd=t:5.3,16.0,37.2,79.8,90.4,95.7,100%7C16.0,26.6,37.2,47.9,58.5,69.1,79.8%7C-1%7C0,21.3,42.6,63.8,85.1

I don't know about you, but that looks like a real pain to remember and get correct all the time.

However, that chart was generated with the spectacular new URI::GoogleChart from the intrepid Norwegian Perl hacker Gisle Aas.

The only code necessary to generate that chart was this:

URI::GoogleChart->new("lxy", 200, 125,
data => [
[10,20,40,80,90,95,99],
[20,30,40,50,60,70,80],
[undef],
[5,25,45,65,85],
],
color => [qw(3072F3 red)],
);
Now that looks a bit more readable, doesn't it?

As you can see in the examples below, URI::GoogleChart makes it easy to generate many kinds of charts with the Google Charts API.


URI::GoogleChart Examples

URI::GoogleChart->new("pie-3d", 250, 100,
data => [60, 40],
chl => "Hello|World",
);


URI::GoogleChart->new("lines", 200, 125,
data => [40,60,60,45,47,75,70,72,],
min => 0,
max => 100,
);


URI::GoogleChart->new("sparklines", 200, 125,
data => [27,25,60,31,25,39,25,31,26,28,80,28,27,31,27,29,26,35,70,25],
min => 0,
max => 100,
);


URI::GoogleChart->new("lxy", 200, 125,
data => [
[10,20,40,80,90,95,99],
[20,30,40,50,60,70,80],
[undef],
[5,25,45,65,85],
],
color => [qw(3072F3 red)],
);


URI::GoogleChart->new("horizontal-stacked-bars", 200, 150,
data => [
[10,50,60,80,40],
[50,60,100,40,20],
],
min => 0,
max => 200,
color => [qw(3072F3 f00)],
);



URI::GoogleChart->new("vertical-grouped-bars", 300, 125,
data => [
[10,50,60,80,40],
[50,60,100,40,20],
],
min => 0,
max => 100,
chco => "3072F3,ff0000",
);

http://chart.apis.google.com/chart?cht=bvg&chs=300x125&chco=3072F3,ff0000&chd=t:10,50,60,80,40%7C50,60,100,40,20


URI::GoogleChart->new("gom", 125, 80, data => 80, chl => 80, title => "Awesomeness");


URI::GoogleChart->new("usa", 200, 100);


0 comments: