Quick graphs with Perl / GD::Graph 30

Posted by Hisham Wed, 08 Jul 2009 19:05:00 GMT

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

Leave a response

  1. Avatar
    compubomb 12 days later:
    Hi. Do you actually like flexing your perl muscle ?.. Because from what i see here, it doesn't look so pretty.
  2. Avatar
    Thalamus 2 months later:
    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 $!;' :(
  3. Avatar
    hisham 2 months later:
    Thalamus, make sure you run this in a place where you have write permissions in order to create the image.
  4. Avatar
    Beats Headphones about 1 year later:
    Congrats to you guys for making the finest choice in the third party code you ship!
  5. Avatar
    Beats Headphones about 1 year later:
    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
  6. Avatar
    sanning this website then you will get good things. <a Href=" http://www.fivefingers2011.com/mens-fivefingers-classic">Five Fingers Classic Mens</a> about 1 year later:
    sanning this website then you will get good things. Five Fingers Classic Mens
  7. Avatar
    Wealth Trigger Review about 1 year later:
    It is no use doing what you like ; you have got to like what you do .http://www.wealthtriggerreview.org/
  8. Avatar
    tory burch reva over 2 years later:
    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!
  9. Avatar
    burberry outlet over 2 years later:
    I read with great interest.Your article looks nice,thank you for sharing with us!
  10. Avatar
    moncler outlet usa over 2 years later:
    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.
  11. Avatar
    North face discount over 2 years later:
    Thanks for sharing. This website is to I too have to help. Very good.I like the way you write.
  12. Avatar
    Belstaff Leather Jacket over 2 years later:
    It is a harvest journey for me when after have a careful look of your blog.
  13. Avatar
    Hogan Outlet over 2 years later:
    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.
  14. Avatar
    Nike Shoes Australia over 2 years later:
    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.
  15. Avatar
    Nike Argentina over 2 years later:
    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.
  16. Avatar
    Nike Max Air over 2 years later:
    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~
  17. Avatar
    Oakley over 2 years later:
    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.
  18. Avatar
    Oakley Canada over 2 years later:
    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.
  19. Avatar
    Prada Online Outlet over 2 years later:
    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.
  20. Avatar
    Prada Outlet over 2 years later:
    This site has many interesting things, thank you for your efforts. I like your article, look forward to your next article.
  21. Avatar
    Prada Outlet Online over 2 years later:
    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
  22. Avatar
    Prada Outlet Store over 2 years later:
    I like your blog because you write a blog is very interesting, lively, wonderful, support your blog
  23. Avatar
    Polo Outlet over 2 years later:
    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
  24. Good work.I like your awesome post..!
  25. Avatar
    Chanel Outle over 2 years later:
    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.
  26. Avatar
    Chanel Outle over 2 years later:
    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.
  27. Avatar
    brochure design over 2 years later:
    Thank you for the good writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you!
  28. Avatar
    Nike Shoes Australia over 2 years later:
    I like your article, brilliant, lively, attractive. Look forward to your next article.
  29. Avatar
    Ralph Lauren Outlet Online over 2 years later:
    I like your blog, support your particular, the text color of the article. Look forward to your updates faster.
  30. Avatar
    Chanel Outlet Store over 2 years later:
    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
Comments