Quick graphs with Perl / GD::Graph 30
So I had to quickly whip up some graphs today at work based on values coming in from one of our database tables. Nothing better then Perl and GD (GD::Graph) for a quick and effective solution.
use strict;
use DBI;
use GD::Graph::bars;
use GD::Graph::Data;
my $db_host = 'XXXXXX';
my $db_name = 'XXXXXX';
my $db_user = 'XXXXXX';
my $db_pass = 'XXXXXX';
my $query = "select month, undelivered from XXXXX where XXXXX";
# create labels and values for x-axis
my ($labels, $values) = get_data_from_sql($db_host, $db_name, $db_user, $db_pass, $query);
# graph and save the output
graph($labels, $values, "Month", "Undelivered", "Undelivered Messages by Month", "undelivered.png");
sub get_data_from_sql($$$$$)
{
my ($db_host, $db_name, $db_user, $db_pass, $query) = @_;
my $dbh = DBI->connect("dbi:mysql:$db_name:$db_host", $db_user, $db_pass)
or die "Couldn't connect to database: " . DBI->errstr;
my $sth = $dbh->prepare($query)
or die "Couldn't prepare statement: " . $dbh->errstr;
$sth->execute();
my @row= undef;
my @labels = ();
my @values = ();
while (@row = $sth->fetchrow_array())
{
push @labels, $row[0];
push @values, $row[1];
}
$dbh->disconnect;
return (\@labels, \@values);
}
sub graph($$$$$$)
{
my ($labels, $values, $x_label, $y_label, $title, $out_file) = @_;
my $data = GD::Graph::Data->new([$labels, $values,])
or die GD::Graph::Data->error;
my $my_graph = GD::Graph::bars->new();
$my_graph->set(
x_label => $x_label,
y_label => $y_label,
title => $title,
bar_spacing => 8,
shadow_depth => 4,
shadowclr => 'dred',
transparent => 0,
)
or warn $my_graph->error;
$my_graph->plot($data) or die $my_graph->error;
my $gd = $my_graph->plot($data) or die $my_graph->error;
open(IMG, ">$out_file") or die $!;
binmode IMG;
print IMG $gd->png;
}
If you do not want your data coming from an SQL statement simply fill in $labels and $values with anything you want, like:
$labels = ['Monday', 'Tuesday', 'Wednesday'];
$data = [45, 66, 89];
Pretty straight forward and handy. the graph looks like this.
Trackbacks
Use the following link to trackback from your own site:
http://hisham.cc/trackbacks?article_id=quick-graphs-with-perl-gd-graph&day=08&month=07&year=2009
Comments
-
Hi. Do you actually like flexing your perl muscle ?.. Because from what i see here, it doesn't look so pretty.
-
Whats wrong with the code ? It looks nice and clean 2 me. Only problem I've got is that it doesn't work :( - permission denied ' open(IMG, ">$out_file") or die $!;' :(
-
Thalamus, make sure you run this in a place where you have write permissions in order to create the image.
-
Congrats to you guys for making the finest choice in the third party code you ship!
-
bravo à vous deux !!! pas mal votre idéé de faire sponsorisé votre mariage !!jje vous souhaite tout le bonheur du monde !!! vous etiez magnifique !!! que de beaux souvenirs pour vous !!! soyez heureux bisous odile de marseille
-
sanning this website then you will get good things. Five Fingers Classic Mens
-
It is no use doing what you like ; you have got to like what you do .http://www.wealthtriggerreview.org/
-
This article is very good, can express the author's point of view, this is the best article I've ever seen.Thanks for sharing the information!
-
I read with great interest.Your article looks nice,thank you for sharing with us!
-
here,make your life full of color ,it's a perfect website.Very informative blog. Thanks for taking the time to share your view with us.
-
Thanks for sharing. This website is to I too have to help. Very good.I like the way you write.
-
It is a harvest journey for me when after have a careful look of your blog.
-
I got what you think, thanks for swing up. Woh I am pleased to comprehend this the main ideas of the text. Also let I can understand it.
-
Although now recreation and exchange place and way more and more, such as a micro-blog, write a signature and so on, but the forums and blogs have been thinking that my love, is also a lot of fresh matter source.
-
Thanks for all your efforts that you have put in this. very interesting information. i like with express my support of your ideas in your article, and looking forward to your next article.
-
Recommend with this articles!unknowingly came here to see your article, I feel very good, I will share with the people around one, Hope see your new article in next time~
-
Good good! Have read your article I broaden the horizons, so I learned a lot, produced a lot of inspiration to write another article. I also wrote many articles, I like it very much! I really like your job. Thank you.
-
First came to this site, I never use this site, I really do not know how to comment. So this comment is just a test, I like this forum, it always has some interesting comments, I hope this is allowed. Thank you.
-
First came to this site, rarely use the site, do not know how to write a comment. So this comment is just a small suggestion, I like this forum, it is always something new.
-
This site has many interesting things, thank you for your efforts. I like your article, look forward to your next article.
-
Well, I reap a lot, your article is very special, I will always focus on your article, sharing is a blessing, I am waiting for your next good article
-
I like your blog because you write a blog is very interesting, lively, wonderful, support your blog
-
I agree with you, as you said, many of my friends should have such thoughts, tell you their thoughts and feelings, so that everyone gathered here, looking forward to a new article
-
Good work.I like your awesome post..!
-
Yeah~,I come back and nice to see your new article,It's great! Thanks for all your efforts that you have put in this. i like with express my support of your ideas in your article, and looking forward to your next article.
-
oh no ,just~,I come back and nice to see your new article,It's great! Thanks for all your efforts that you have put in this. i like with express my support of your ideas in your article, and looking forward to your next article.
-
Thank you for the good writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you!
-
I like your article, brilliant, lively, attractive. Look forward to your next article.
-
I like your blog, support your particular, the text color of the article. Look forward to your updates faster.
-
This is the perfect blog for anyone who wants to know about this topic. You know so much its almost hard to argue with you (not that I really would want...HaHa). You definitely put a new spin on a subject thats been written about for years. Great stuff, just great!27/12