GD::Graph Invalid Data from DBI

Discussion in 'Perl Misc' started by alexbarham@yahoo.ca, Jul 10, 2005.

  1. Guest

    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
     
    , Jul 10, 2005
    #1
    1. Advertising

  2. wrote in news:1120980526.043734.266850
    @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

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jul 10, 2005
    #2
    1. Advertising

  3. * schrieb:

    > 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
     
    Fabian Pilkowski, Jul 10, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Sakkis
    Replies:
    1
    Views:
    466
    Szabolcs Nagy
    Jan 29, 2007
  2. Dr Ann Huxtable

    Missing Graph.h and (Graph.lib) woes - any help

    Dr Ann Huxtable, Dec 21, 2004, in forum: C Programming
    Replies:
    6
    Views:
    661
    Dr Ann Huxtable
    Dec 21, 2004
  3. Jef Driesen
    Replies:
    3
    Views:
    2,570
    mlimber
    Jan 24, 2006
  4. Almoni
    Replies:
    0
    Views:
    3,120
    Almoni
    Jan 17, 2010
  5. Emilio Mayorga
    Replies:
    6
    Views:
    355
    Martien Verbruggen
    Oct 8, 2003
Loading...

Share This Page