Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Question about using hash indexes with GD::Graph

by vonman (Acolyte)
on Mar 20, 2001 at 20:09 UTC ( [id://65718]=perlquestion: print w/replies, xml ) Need Help??

vonman has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to use the GD::Graph module to plot a chart that will have militay hour number on the x-axis and a count on the y-axis. I have indexes for my hash table for 'hour' and 'count'. I would like to use the GD::Graph::lines function. Below is the example code that I am trying to plot my data but it gives me errors.
# This is the original data from the perldoc examples # @data=(["1st","2nd","3rd"], # [9,4,7]); # I have a hash table with $data[$index]{'hour'} and $data[$index]{'c +ount'} # $graph= GD::Graph::lines->new(400,300); $graph->set( x_label => 'Hour', y_label => 'Transaction Volume', title => 'Number of transactions per hour', y_max_value => 20, y_label_skip => 1, y_tick_number => 1); $gd = $graph->plot(\@data); open (IMG,'>file.png') or die $!; binmode IMG; print IMG $gd->png; close IMG;
Any ideas? Thanks in advance!!

Replies are listed 'Best First'.
(jeffa) Re: Question about using hash indexes with GD::Graph
by jeffa (Bishop) on Mar 20, 2001 at 20:25 UTC
    UPDATE: see comments

    How about:

    my (@h,@c); push @h, $_->{hour} foreach(@{$data}); # no so great push @c, $_->{count} foreach(@{$data}); # see extremely's comment my $new = [ \@h, \@c ]; $gd = $graph->plot($new); # wrong - see orbital's comme +nt
    I really tried to avoid the temp lists, maybe someone will come along and make this puppy scream with a nasty map block. ;)

    BEFORE: AFTER: $VAR1 = [ $VAR1 = [ { [ 'count' => 5, 1, 'hour' => 1 5, }, 2 { ], 'count' => 2, [ 'hour' => 5 5, }, 2, { 8 'count' => 8, ] 'hour' => 2 ]; } ];
    Data::Dumper - don't code anonymous data structures without it.

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
      merlyn showed ya map but honestly you shouldn't get into the habit of traipsing through a data set multiple times. When ever you see two lines that both end "foreach (@array)" you should be doing:
      foreach (@data) { push @h, $_->{'hour'}; push @c, $_->{'count'); }

      You get in a hurry and do it wrong then wind up going through the same data like 7 or 8 times when there are 100,000 items in the list and you are going start wondering where your afternoon of surfing PM went. =P

      --
      $you = new YOU;
      honk() if $you->love(perl)

      This is perfect except that I had to cange the foreach lines to look like follows.
      my (@h,@c); push @h, $_->{'hour'} foreach(@data); push @c, $_->{'count'} foreach(@data); my $new = [ \@h, \@c];
      Now it is graphing correctly
        Any kind of push @x, .... for @y should immediately invoke that magic three-letter word, "m a p".
        my $new = [ [map $_->{hour}, @data], [map $_->{count}, @data] ];

        -- Randal L. Schwartz, Perl hacker

Re: Question about using hash indexes with GD::Graph
by orbital (Scribe) on Mar 20, 2001 at 20:33 UTC
    I still don't think that will solve the problem of the error. I would agree with jeffa on how to format your data but your 2nd to the last line I think is what is hanging you up. This is my suggestion:

    print IMG $gd->plot(\@data)->png;
      You are correct the error message that I am receiving is
      Can't call method "png" on an undefined value at _graph_test.pl line 9 +1, <STDIN> line 1.
      I am still running into trouble with the data. I have changed the code to read like follows
      my (@h,@c); push @h, $_->{'hour'} foreach(@{$data}); push @c, $_->{'count'} foreach(@{$data}); my $new = [ \@h, \@c]; print (\@new); #@data=(["1st","2nd","3rd"], # [9,4,7]); # I have a hash table with $data[$index]{'hour'} and $data[$index]{'c +ount'} # $graph= GD::Graph::lines->new(400,300); $graph->set( x_label => 'Hour', y_label => 'Transaction Volume', title => 'Number of transactions per hour', y_max_value => 20, y_label_skip => 1, y_tick_number => 1); #$gd = $graph->plot(\@data); open (IMG,'>file.png') or die $!; binmode IMG; print IMG $graph->plot(\@new)->png; close IMG;
      but when I run it I still receive the same error. If I uncomment the @data lines and plot to @data instead of @new it works. Any ideas?
        because $new is an array reference, so all you need to do is fill it in as $graph->plot($new)->png;
        $new is a already a reference to an array consisting of two arrayrefs. There is no such thing (yet) as @new in the code snippet you've posted. Try it with
        print IMG $graph->plot($new)->png;
        (instead of the \@new you have there now) and see what happens.

        use strict should have caught this. You *are* using, strict, aren't you?

        Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://65718]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-13 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found