Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

GD Graph

by bar10der (Beadle)
on Sep 28, 2005 at 12:11 UTC ( #495713=perlquestion: print w/ replies, xml ) Need Help??
bar10der has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I am trying to create a barchart to display progress of various open projects. Progress data is stored in database.

I am able to extract data from database and plot the graph but if i try to set legends through a variable, my script fails. Here is the code:-

$sql="SELECT pno,pname FROM t1 WHERE status=? AND ctry=?"; my $sth = $dbh->prepare($sql); $sth->execute('O','GB'); while(my $row=$sth->fetchrow_hashref){ push @projects,$row; } ##Now get progress data from table $sql="SELECT NVL(progress,0) FROM chart WHERE pno=? AND chartyear = 20 +05 AND chartmonth in(6,7,8) ORDER BY chartmonth"; foreach (@projects){ $pno= $_->{'PROJECTNUMBER'}; @dat=getMultiRows($sql,1,$dbh,$pno); push(@data,\@dat); $projectname .= "'".$_->{'PROJECTNAME'}."'".","; } chop $projectname; ##Now plot the graph my $mygraph = GD::Graph::bars->new(500, 300); $mygraph->set( x_label => 'Months', y_label => '% Completed', title => 'Project Log progress', bar_width => 2, bar_spacing => 2, long_ticks => 1, show_values => 1, x_label_position => 0.5, t_margin => 10, b_margin => 10, l_margin => 10, r_margin => 10, ) or warn $mygraph->error; $mygraph->set_legend_font(GD::gdMediumBoldFont); $mygraph->set_legend('Dynamic reconfiguration FY06 Initiative','Rmg Es +calation Management Tool'); # Works fine ##Above works but when I try following it fails though value of $proje +ctname is same as above value supplied to set_legend function. #$mygraph->set_legend($projectname); #This fails my $myimage = $mygraph->plot(\@data) or die $mygraph->error; print "Content-type: image/png\n\n"; print $myimage->png;
Can someone help me please?

Thanks for your help

Comment on GD Graph
Download Code
Re: GD Graph
by neilwatson (Curate) on Sep 28, 2005 at 12:39 UTC
    What error messages are created when the script fails?

    Neil Watson
    watson-wilson.ca

Re: GD Graph
by tomhukins (Curate) on Sep 28, 2005 at 12:42 UTC

    You say that your script fails, but not how it fails. Do you see error messages? Does it not do what you want? If so, what does it do and what do you want it to do?

    Essentially, you're tackling two problems: pulling data from a database and building a chart. Try isolating the code to perform each problem separately.

    Firstly, query the database and check you have the data structures you expect in $projectname, @dat and $pno using Data::Dumper and warn in your foreach loop.

    Secondly pass data of the form you expect to retrieve from the database to GD::Graph::bars and see what happens.

      I did run the data extraction code and it returns the expected values from the database. No problem there.

      When I print value of $projectname variable I get following (as expected)-

      'RMG Escalation Management Tool','Dynamic Reconfiguration FY06 Initiative'

      If I put this line -

      $mygraph->set_legend('Dynamic reconfiguration FY06 Initiative','Rmg Escalation Management Tool');

      The graph is generated.

      However if I put this-

      $mygraph->set_legend($projectname);

      I get following err in server log

      Wed Sep 28 13:49:24 2005 error client x.x.x Wed Sep 28 13:49:24 2005 barchart.pl: Illegal division by zero at ....GD/Graph/axestype.pm line 1962., referer: http://index1.html

        $projectname needs to be an ARRAY:

        Methods $graph->set_legend(@legend_keys); Sets the keys for the legend. The elements of @legend_keys corresp +ond to the data sets as provided to plot(). If a key is undef or an empty string, the legend entry will be ski +pped.

        So
        my @projectname = ('Dynamic reconfiguration FY06 Initiative','Rmg Escalation Management Tool');

        and

        $mygraph->set_legend(@projectname);

        HTH.

        Walking the road to enlightenment... I found a penguin and a camel on the way.....
        Fancy a yourname@perl.me.uk? Just ask!!!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://495713]
Approved by lamp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2014-08-20 09:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (110 votes), past polls