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

So, I'm still plugging away to see why my arrays won't graph, and I ran into something that's rather curious to me, and I can't see where the issue lies.

The following code creates a small graph with three lines plotted:

use GD::Graph::lines; $Group = "Test Group"; #@name1 = ; #@name2 = ; #@name3 = ; #@xaxis = ; @data = ( [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [6, 18, 21, 28, 36, 44, 52, 67, 78, 89], [1, 15, 22, 31, 45, 7, 19, 21, 36, 41], [8, 91, 78, 88, 87, 80, 96, 95, 99, 99] ); @userlist2 = ""; $name1 = "John"; $name2 = "Jacob"; $name3 = "Jingleheimer"; $space = " "; push (@userlist2, $name1); push (@userlist2, $name2); push (@userlist2, $name3); push (@userlist2, $space); $graph1 = GD::Graph::lines->new(1024, 768); $graph1->set( x_label => 'Date', y_label => '% Closed', title => "$Group", y_max_value => 100, y_tick_number => 10, x_label_skip => 7 ) or die $graph->error; $graph1->set( dclrs => [ qw(black dblue gold dgreen dred d +purple lorange dpink marine cyan dbrown lgray lblue lyellow dyellow l +green lred lpurple pink ) ] ); $graph1->set( line_types => [1, 3] ); $graph1->set_legend(@userlist2); my $gd = $graph1->plot(\@data) or die $graph1->error; open (IMG, ">./$Group.jpg"); binmode IMG; print IMG $gd->png; close IMG;

It creates the graph just fine, with the small problem that it only displays the first two entries for the legend, and doesn't seem to display the third. I can't really figure that part out. :-/

This is the altered code, which only changes in that instead of hardcoding the array directly, we reference three already-declared arrays to draw, and get no linear output when the graph is created.

use GD::Graph::lines; $Group = "Test Group"; @name1 = [6, 18, 21, 28, 36, 44, 52, 67, 78, 89]; @name2 = [1, 15, 22, 31, 45, 7, 19, 21, 36, 41]; @name3 = [8, 91, 78, 88, 87, 80, 96, 95, 99, 99]; @xaxis = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; @data = ( [@xaxis], [@name1], [@name2], [@name3] ); @userlist2 = ""; $name1 = "John"; $name2 = "Jacob"; $name3 = "Jingleheimer"; $space = " "; push (@userlist2, $name1); push (@userlist2, $name2); push (@userlist2, $name3); push (@userlist2, $space); $graph1 = GD::Graph::lines->new(1024, 768); $graph1->set( x_label => 'Date', y_label => '% Closed', title => "$Group", y_max_value => 100, y_tick_number => 10, x_label_skip => 7 ) or die $graph->error; $graph1->set( dclrs => [ qw(black dblue gold dgreen dred d +purple lorange dpink marine cyan dbrown lgray lblue lyellow dyellow l +green lred lpurple pink ) ] ); $graph1->set( line_types => [1, 3] ); $graph1->set_legend(@userlist2); my $gd = $graph1->plot(\@data) or die $graph1->error; open (IMG, ">./$Group.jpg"); binmode IMG; print IMG $gd->png; close IMG;
This leads me to believe I've been doing something wrong in GD::Graph the entire time I've been working with it (and my boss, who taught me, before me). Can anyone who's even remotely more involved than I am give me an idea of what's going wrong there?

Replies are listed 'Best First'.
Re: Curious issue with Graphing Arrays
by Errto (Vicar) on Nov 13, 2007 at 20:34 UTC
    It looks like this isn't a GD::Graph issue but rather a referencing issue. Specifically, the way you're assigning your arrays is wrong. It should be either:
    @name1 = (6, 18, 21, 28, 36, 44, 52, 67, 78, 89); @name2 = (1, 15, 22, 31, 45, 7, 19, 21, 36, 41); @name3 = (8, 91, 78, 88, 87, 80, 96, 95, 99, 99); @xaxis = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    or else
    $name1 = [6, 18, 21, 28, 36, 44, 52, 67, 78, 89]; $name2 = [1, 15, 22, 31, 45, 7, 19, 21, 36, 41]; $name3 = [8, 91, 78, 88, 87, 80, 96, 95, 99, 99]; $xaxis = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    In the latter case, you would change the @data assignment to
    @data = ( $xaxis, $name1, $name2, $name3 );
    The reason is that with your current code, you're actually creating three one-element arrays, each containing an array reference as its first element. Also, if you do go with the first approach, no need to duplicate the contents of the arrays. Just do
    @data = ( \@xaxis, \@name1, \@name2, \@name3 );
      Thanks for the help, that certainly fixed the issue.

      The next issue that I have is in attempting to name the variable dynamically, using a counter. So, instead of coding @name1, @name2 etc, we'd instead code something like this:

      $x = 1; @name[$x] = (stuff); $x++
      This would loop through several times to assign outputs from dynamic SQL statements to the dynamically named arrays, which are then output to a graph. Any idea on what the syntax would be to name that?