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

Hey everyone, Just trying to figure out GD graph - I would like to dynamically assign data in order to create a dynamic graph. Here's the code I have so far, but it fails, it seems that I am not assigning the data correctly. Also, is there a way to seperate labels and data into different arrays like MS::Graph??
#!/usr/bin/perl use MSGraph qw/gen_graph/; use GD; use DBI; $sql = "select day, count(day) as day_total from tec_event_data where +month='Feb' group by day order by day asc"; $db = DBI->connect('dbi:Oracle:tec.world','tec','tectec') or die "Unab +le to connect: $DBI::errstr"; $sth = $db->prepare( $sql ); $sth->execute; my $graph = new GD::Graph::lines3d( 600, 400 ); $graph->set( x_label => 'Months', y_label => 'Number of Events', title => 'Monthly Events by Day' ); while (@row = $sth->fetchrow_array) { my $gd = $graph->plot( \@row); } my $format = $graph->export_format; open (IMG, ">/usr/tivoli/TSE/httpd/docs/images/test.$format") or die $ +!; binmode IMG; print IMG $gd->$format(); close IMG;
TIA,

Replies are listed 'Best First'.
Re: GD Graph Question
by {NULE} (Hermit) on Feb 20, 2002 at 00:13 UTC
    Hi liquidc00l,

    I'm going to make a few suggestions to you. You are not constructing your array in the way GD::Graph expects, but there are some other things that I hope you'll listen to.

    The first is to make sure you use strict; That will tell you if this is failing for some silly reason like having mis-typed "data". It's going to force you to go back and make sure all of your variables are initialized with my but that's a good thing.

    OK, now with that out of the way, let's see what is wrong with your @data array. If you check the documentation for GD::Graph you'll see that the plot method requires that your data be sent as a list of anonymous arrays. Doing a my @data = (@a, @b); has the effect of concatinating the arrays together. What you actually want to do is construct a list of array references like this: my @data = (\@a, \@b);

    To see why this does what you want and why your print test should have clued you into this see my sample code at the end.

    One more nit to pick first though! You are mixing functional calls and methods in your code. Don't do that! It's confusing and my not even work with all modules (like the crappy ones I write! :) ). If you like methods be consistent and say my $graph = GD::Graph::lines3d->new(600,400); or whatever the format is. It may not be a big deal in the big scheme of things, but consistency always pays off for a programmer.

    Good luck and check out my demo code at the end for clarification,
    {NULE}
    --
    http://www.nule.org

Re: GD Graph Question
by liquidc00l (Novice) on Feb 19, 2002 at 16:05 UTC
    OK, I have made some changes to my orginal code and here's what I have now. the for loop just verfies that the data is actually there, which is working. When I actually try to execute the script here's the error I get.
    Can't call method "png" on an undefined value at ./graph.pl line 35.
    I am assuming that this is due to the fact the @data has no data?? Here's my new code
    #!/usr/bin/perl -w use GD::Graph::lines3d; use DBI; $sql = "select day, count(day) as day_total from tec_event_data where +month='Feb' group by day order by day asc"; $db = DBI->connect('dbi:Oracle:tec.world','tec','tectec') or die "Unab +le to connect: $DBI::errstr"; $sth = $db->prepare( $sql ); $sth->execute; while (($day,$events) = $sth->fetchrow_array) { push (@days, $day); push (@events, $events); } $sth->finish; my @data = (@days,@events); #for ($x=0;$x<=$#data;$x++) { # print "$data[$x]\n"; #} $graph = new GD::Graph::lines3d( 600, 400 ); $graph->set( x_label => 'Months', y_label => 'Number of Events', title => 'Monthly Events by Day' ); $gd = $graph->plot( \@data ); $format = $graph->export_format; open (IMG, ">/usr/tivoli/TSE/httpd/docs/images/test.$format") or die $ +!; binmode IMG; print IMG $gd->$format(); close IMG; $db->disconnect;
    Any suggestions would be greatly appreciated. TIA, liquid
      Found what the problem was...since I am just learning this, I was using an example to get started, when I was pushing my information into the @data array, the format needed to be as follows,
      my @data = ([@days],[@events]);
      Worked fine after that. Liquid