SoaponaRope has asked for the wisdom of the Perl Monks concerning the following question:
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Curious issue with Graphing Arrays
by Errto (Vicar) on Nov 13, 2007 at 20:34 UTC | |
by SoaponaRope (Novice) on Nov 13, 2007 at 20:49 UTC |