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

Dear Master Monks,

I have not made a lot of graphing scripts, so I humbly ask your advice.

How would I change the following (I cant find the right info in the docs):

  1. Switch the y-axis round, so that the lowest number is at the top
  2. Change the square dot to an X, at the value points
  3. Add the actual time value to the top of the square/X

Code:

#!/usr/bin/perl use strict; use warnings; # running-times 15-05-05 # # Graphing all my running times. # I will just insert the data # directly into this program for now, # but when it becomes too much, Ill # move it into another file/dbm use GD::Graph::linespoints; use Getopt::Std; my %times; # running times my $chart; # the GD::Graph::linespoints object my $plot; # the GD object containing the actual graph my @DAYS = qw(Mon Tue Wed Thu Fri Sat Sun); # process options my %Opt; getopts('ho:', \%Opt); if ($Opt{h} or !$Opt{o}) { die "Usage:\n\t$0 -o outfile.png \n"; } # Running times %times = ( "Mon" => "25.40", "Tue" => "24.20", "Wed" => "22.50", "Thu" => "23.30", "Fri" => "21.10", "Sat" => "24.30", "Sun" => "25.19", ); # build graph $chart = GD::Graph::linespoints->new(480,320); $chart->set(x_label => "Day", y_label => "Time (mins)", title => "Running Times for 5km - W/E May 15th 2005"); $plot = $chart->plot([ [ @DAYS ], [ @times{@DAYS} ], ]); # save it open(F, "> $Opt{o}") or die "Can't open $Opt{o} for writing: $!\n"; print F $plot->png; close F; # Create html page with png in it open H, ">times.html" or die "Can not create webpage: $!\n"; use CGI; my $query = CGI->new( ); print H $query->start_html(-title=>'Gavins Weekly Running Times'); print H $query->p("Here are your times:"); print H "<table><tr><td><img src=$Opt{o}></a></td></tr></table>"; print H $query->end_html( ); close H;

Thanks,
Gavin.

Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a yourname@perl.me.uk? Just ask!!!

Replies are listed 'Best First'.
Re: GD::Graph::linespoints - Various questions
by eibwen (Friar) on May 15, 2005 at 21:50 UTC

      Damn, I am blind. I just read through that POD file and completely missed those.

      Many thanks.

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!
Re: GD::Graph::linespoints - Various questions
by BrowserUk (Patriarch) on May 15, 2005 at 22:13 UTC

    Inverting the y-axis is a bit of a fudge, but this appears to fulfill your reqs:

    #!/usr/bin/perl use strict; use warnings; # running-times 15-05-05 # # Graphing all my running times. # I will just insert the data # directly into this program for now, # but when it becomes too much, Ill # move it into another file/dbm use GD::Graph::linespoints; use Getopt::Std; my %times; # running times my $chart; # the GD::Graph::linespoints object my $plot; # the GD object containing the actual graph my @DAYS = qw(Mon Tue Wed Thu Fri Sat Sun); # process options my %Opt; getopts('ho:', \%Opt); if ($Opt{h} or !$Opt{o}) { die "Usage:\n\t$0 -o outfile.png \n"; } # Running times %times = ( "Mon" => "25.40", "Tue" => "24.20", "Wed" => "22.50", "Thu" => "23.30", "Fri" => "21.10", "Sat" => "24.30", "Sun" => "25.19", ); # build graph $chart = GD::Graph::linespoints->new( 480, 320, 1); $chart->set( x_label => "Day", y_label => "Time (mins)", y_min_value => -26, y_max_value => -20, markers => [ 4 ], show_values => 1, y_number_format => sub{ return abs $_[ 0 ] }, values_format => sub{ return abs $_[ 0 ] }, title => "Running Times for 5km - W/E May 15th 2005" ); $plot = $chart->plot( [ [ @DAYS ], [ map{ -$_ } @times{@DAYS} ], ] ); # save it open(F, "> $Opt{o}") or die "Can't open $Opt{o} for writing: $!\n"; print F $plot->png; close F; # Create html page with png in it open H, ">times.html" or die "Can not create webpage: $!\n"; use CGI; my $query = CGI->new( ); print H $query->start_html(-title=>'Gavins Weekly Running Times'); print H $query->p("Here are your times:"); print H "<table><tr><td><img src=$Opt{o}></a></td></tr></table>"; print H $query->end_html( ); close H;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.