Home > Archive > PERL Beginners > June 2005 > AoA for GD::Graph
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| ahuber@sdf.lonestar.org 2005-06-06, 3:56 am |
| I am trying to build an array that can be used with GD::Graph
to show the graphical representation of a MySQL query. This module
only takes in an AoA like this:
@data = ([1,2,3,4],[10,20,30,40]); # @arr[$x vals], [$y vals]
It then takes that array and parses it into a graph like this:
my $image = $plot->plot(\@data);
My SELECT statement correctly returns two columns of data but I'm having
trouble reading them into an array shaped like the above using the following:
while (@results = $sth->fetchrow_array ())
{
$x = $results[0];
$y = $results[1];
push (@data,[$x],[$y]);
}
When I use the above code I get an array that looks like this
@data = [1,10][2,20][3,30][4,40].
How can I get my array to come out correctly?
Thanks, Aaron.
P.S. Sorry for posting this again, but I left out some important
information in my first post and I was mis-understood.
| |
| Wiggins d'Anconia 2005-06-06, 3:56 am |
| ahuber@sdf.lonestar.org wrote:
> I am trying to build an array that can be used with GD::Graph
> to show the graphical representation of a MySQL query. This module
> only takes in an AoA like this:
>
> @data = ([1,2,3,4],[10,20,30,40]); # @arr[$x vals], [$y vals]
>
> It then takes that array and parses it into a graph like this:
>
> my $image = $plot->plot(\@data);
>
> My SELECT statement correctly returns two columns of data but I'm having
> trouble reading them into an array shaped like the above using the following:
>
> while (@results = $sth->fetchrow_array ())
> {
> $x = $results[0];
> $y = $results[1];
> push (@data,[$x],[$y]);
> }
>
> When I use the above code I get an array that looks like this
>
> @data = [1,10][2,20][3,30][4,40].
>
You need to understand the base concept of what you are building, the
perlreftut docs I mentioned before should be helpful. You should also
read through,
perldoc perldsc
perldoc perllol
They will help you understand.
> How can I get my array to come out correctly?
>
You are really selecting a set of x,y but you need separate lists of all
x's and all y's. So knowing that,
my (@x, @y);
while (my ($x, $y) = $sth->fetchrow_array) {
push @x, $x;
push @y, $y;
}
my $image = $plot->plot( [ \@x, \@y ] );
Or to condense further,
my $data;
while (my ($x, $y) = $sth->fetchrow_array) {
push @{$data->[0]}, $x;
push @{$data->[1]}, $y;
}
my $image = $plot->plot( $data );
> Thanks, Aaron.
HTH,
http://danconia.org
|
|
|
|
|