rakheek has asked for the wisdom of the Perl Monks concerning the following question:

Hi Friends, I have a code where I am doing DBI query and plotting data using perl GD module. My code does not give any error but also does not plot the data. I have tried sample data and it seems to work. Any clues? The code is as below:
use CGI qw(:standard); use DBI; use GD::Graph::bars; use GD::Graph::Data; $cgi= new CGI; #print $cgi->header( -type => 'image/png'); print $cgi->header ; #print $cgi->start_html(-title=>'Basic CGI'); #print $cgi->table({border=>1}); #$project_name = $input('Project'); if ($input{'Project'} eq "DEIMOS") { #my $run_id_number="499"; my $dbh = DBI->connect('dbi:mysql:bugz:bugzilla.telegent.com:3306', 's +wqa', 'sImANten') or die "Connection Error: $DBI::errstr\n"; my $sql = "Select log_date, open_cnt from swqa.Poseidon where product +like '%DEIMOS%' and rec_type='1'" and log_date >= '2010-01-01'; my $sth = $dbh->prepare($sql); $sth->execute or die "SQL Error: $DBI::errstr\n"; # HTML TABLE #print '<title> QA Metric charts for DEIMOS</title>'; #print '<tr><td><b>Open P1-P2 Bugs</b></td></tr>'; # my @log_date = (); my @deimos_open_bugs = (); while (my @row = $sth->fetchrow_array) { # print "<tr><td>$row[0]</tr></td>\n"; push @log_date, $row[0]; push @deimos_open_bugs, $row[1]; } $dbh->disconnect; my $mygraph = GD::Graph::bars->new(); $mygraph->set( x_label => 'Log_Date', y_label => 'Open P1-P2 Bugs', title => 'DEIMOS Data', ); #my @data = (['Fall 01', 'Spr 01', 'Fall 02', 'Spr 02' ], # [80, 90, 85, 75], # [76, 55, 75, 95], # [66, 58, 92, 83]); #print $cgi->end_table; #print 'Content-Type: image/png\n\n'; my $data = GD::Graph::Data->new([$log_date], [$deimos_open_bugs]) or d +ie GD::Graph::Data->error; my $myimage = $mygraph->plot(\@data) or die $mygraph->error; binmode STDOUT; print $myimage->png;

Replies are listed 'Best First'.
Re: Having problem graphing with Perl GD module
by BrowserUk (Patriarch) on Mar 12, 2010 at 23:25 UTC

    If you added use strict & use warnings to your code, you'd find about a dozen reasons why what you've posted doesn't work.

    These include

    1. An unclosed if statement.
    2. Assigning to $data and passing \@data.
    3. Where does GD::Graph::Data come from? I can't find it on CPAN (and it surely isn't needed for what you are doing).

    If you cleaned up the obvious things in your code, you'd get a lot closer to being able to solve your own problem.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thank you BrowserUk. Actually, #2 was the problem. I got around with following code. Also, I got rid of GD::Graph::Data. My if is closed. BTW, I am new to perlmonks and am very happy to see quick response on the site.
      my @data = (\@log_date, \@deimos_open_bugs); my $myimage = $mygraph->plot(\@data) or die $mygraph->error;

        Oh dear. New maintainer, new fluff. At least it's optional.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.