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
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