GD::Graph Invalid Data from DBI

A

alexbarham

I am having a problem querying a database and graphing the data using
GD::Graph::lines. Here is the code that I am using:

my $count=0;
my @headers=[1,2,3,4,5];
my @float_values=[32.20,32.25,32.50,32.75,33.00];
my @working_combined_array=();
my @dates=();
my @values=();
my @combined_array=();

push(@working_combined_array,@headers);
push(@working_combined_array,@float_values);

$dbh=DBI->connect("DBI:mysql:host=localhost;database=xxxxx",
'xxxxx','xxxxxx',
{PrintError=>0,RaiseError=>1});

$sth=$dbh->prepare( "SELECT date,close FROM table");
$sth->execute();

while(my @values=$sth->fetchrow_array())
{
push(@dates,$date_values[0]);
push(@values,$push_values[1]);
}

$sth->execute();
$sth->finish();
$dbh->disconnect();

push(@combined_array,@dates);
push(@combined_array,@values);

for ($count=0;$count1 < $date_count;$count++){
print $dates[$count],"\n";
print $values[$count],"\n";
}

my $chart_image=$chart->plot(\@combined_array) or die $chart->error;

The problem is if I use the hard-coded values, GD:Graph works fine.
However, if I use the queried data, I get an invalid data set: 0. The
for loop prints the data correctly as though there are two separate set
of data. I tried using a two dimensional array as such:

$i = 0;
while (@results = $sth->fetchrow_array ())
{
$x = $results[0];
$y = $results[1];
@points = ($x, $y);
$data[$i] = \@points;
$i++;
}
my $image = $plot->plot(\@data);

But this interspersed the dates and values on the x and y axes. If I
print a count of elements for the working_combined_array, I get 2. But
if I print a count of elements for the combined_array from the database
data, I get 10 (there are 5 rows in the table). It seems that my code
is flattening the array but I am using the same syntax of 2
push(@combined_array,@datasets) statements. I am stumped as to why this
is doing this and am wondering if anyone can spot the problem.
Thanks
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g14g2000cwa.googlegroups.com:
I am having a problem querying a database and graphing the data using
GD::Graph::lines. Here is the code that I am using:

<code snipped>

Global symbol "@date_values" requires explicit package name at D:\Home
\ttt.pl line 26.
Global symbol "@push_values" requires explicit package name at D:\Home
\ttt.pl line 27.

Post real code.

Sinan
 
F

Fabian Pilkowski

I am having a problem querying a database and graphing the data using
GD::Graph::lines. Here is the code that I am using:

I'm not familiar with this module, but ...
my $count=0;
my @headers=[1,2,3,4,5];
my @float_values=[32.20,32.25,32.50,32.75,33.00];

.... with brackets "[]" you create a reference to an anonymous array.
Your array @header contains only one element -- that reference. Try:

my @headers = ( 1, 2, 3, 4, 5 );

or just

my @headers = 1 .. 5;

Same for @float_values.
my @working_combined_array=();
my @dates=();
my @values=();
my @combined_array=();

There's no need to assign an empty list to arrays. In Perl, that's the
default for arrays.

my @working_combined_array;
my @dates;
# ...

Or do it all in one:

my( @working_combined_array, @dates );
push(@working_combined_array,@headers);
push(@working_combined_array,@float_values);

$dbh=DBI->connect("DBI:mysql:host=localhost;database=xxxxx",
'xxxxx','xxxxxx',
{PrintError=>0,RaiseError=>1});

$sth=$dbh->prepare( "SELECT date,close FROM table");
$sth->execute();

while(my @values=$sth->fetchrow_array())
{
push(@dates,$date_values[0]);
push(@values,$push_values[1]);

Err, is this really that code you're running? Please use "use strict"
and "use warnings" even in small examples. Do you mean $values[0] and
$values[1] instead?
}

$sth->execute();
$sth->finish();

There's no need to call execute() again, and finish() will be done
automatically. Just drop those two lines.
$dbh->disconnect();

[...]

regards,
fabian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,742
Latest member
AshliMayer

Latest Threads

Top