Mike, First of all many thanks for the detailed reply. I thought I
understood what you were saying but, having changed my code
accordingly, I'm not so sure.

If I understand you right, to embed a graph on my HTML page I need to
create it outside the HTML with Content-type: image/png\n\n. This is
achieved, at the point in my HTML where the graph is to be displayed,
by the inclusion of  an IMG tag.
(E.G. <img src='SBFPAchart.cgi?start-date=01/01/2008&end-
date=09/01/2008&1=on&2=on'/>)

Presumably what is happening here is that the HTML with the IMG tag is
generated on the server and sent to the browser on the client. The
browser see the embedded IMG tag and requests the server to run the
script specified on it. This the server does, the graph image is
created on the server and sent to the browser which now adds it to the
location on the page indicated by the HTML.

On this premise I:

a) removed the lines relating to the graph generation from my script
responsible for generating the HTML and replaced them with the IMG tag
above.
b) wrote script SBFPAchart.cgi to process the parameters passed back
from the browser, call my database manager to generate the data
arrays, and use GD::Graph to create the image using this data. This
image is then output, preceded with the Content-type: image/png\n\n
statement.

The net result is that SBFPAchart.cgi script runs (the trace file
proves this) but no image is displayed (just the red cross in a box
placeholder). (If I uncomment the line that pushes the image onto the
trace array, I see what I assume to be the image in the trace file but
it's not in binary so I can't cut and paste it to a file to view.)

I tried to understand what you meant by-
>
> You can either do this by commenting out the content-type head and
> redirecting the output.
> ./test.cgi >test.png
>
but couldn't figure out how to code this in the context of my code. I
tried coding in my HTML
              <img src='SBFPAchart.cgi?start-date=19/01/2009&end-
date=23/01/2009&58=on&242=on>test.png'/> and
             <img src='SBFPAchart.cgi>test.png?start-
date=19/01/2009&end-date=23/01/2009&58=on&242=on'/>
but couldn't find file test.png anywhere.

Thanks to your help I feel I'm almost there. If you could give me a
few pointers on what to try next, I'd really appreciate it.

Cheers,

Bryan

P.S. The SBFPAchart.cgi script follows;

#!d:/perl/bin/perl.exe -w

use warnings;
use strict;

use feature qw(switch);

use CGI::Carp qw(fatalsToBrowser);                         # only for
debugging
use Date::Calc qw/Delta_Days/;
use GD::Graph::lines;

use MyDatabaseManager;

use constant CHARTWIDTH  => 1238;
use constant CHARTHEIGHT => 690;

# Declare local variables

my(@chartColours) = [qw(blue lred gold dpink dgreen)];

my($formInput) = "";
my(@parms, @pairs, $pair, $name, $value, $worst, $best, $startDate,
$endDate, @selectedFundRefs, @chartHash);

my(@trace) = ();                                           # *****
DEBUG  *****

push(@trace, "SBFPAchart.cgi entered\n");                  # *****
DEBUG  *****

push(@trace, "@ARGV\n");                                   # *****
DEBUG  *****

if($ENV{'REQUEST_METHOD'})
  {
  $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;                   # Translate to 
uppercase
  push(@trace, "  REQUEST_METHOD\n");                      # *****
DEBUG  *****
  if ($ENV{'REQUEST_METHOD'} eq "POST")
    {
    read(STDIN, $formInput, $ENV{'CONTENT_LENGTH'});
    push(@trace, "    POST\n");                            # *****
DEBUG  *****
    }
  else                                                     # assume
'GET' method
    {
    $formInput = $ENV{'QUERY_STRING'};
    push(@trace, "    GET\n");                             # *****
DEBUG  *****
    }

  if($formInput)
    {
    push(@trace, "      Some input\n");                    # *****
DEBUG  *****
    # Split information into name/value pairs
    @pairs = split(/&/, $formInput);                       # Parse
input on '&' separator into array entries
    foreach $pair (@pairs)
      {
      ($name, $value) = split(/=/, $pair);                 # Parse
each array entry on '='
      push(@parms, "name: " . $name . ", value: " . $value);
      $value =~ tr/+/ /;
      $value =~ s/%(..)/pack("C", hex($1))/eg;
      given($name)
        {
        when (/\d+/)
          {
          push(@selectedFundRefs, $name);                  # Add fund
reference to array
          push(@trace, "        Fund ref: $name\n");       # *****
DEBUG  *****
          }
        when ("start-date")
          {
          $startDate = $value;
          push(@trace, "        Start date : $startDate\n"); # *****
DEBUG  *****
          }
        when ("end-date")
          {
          $endDate = $value;
          push(@trace, "        End date : $endDate\n");   # *****
DEBUG  *****
          }
        }
      }

    my($startDD, $startMM, $startYYYY) = ($startDate =~ m^(\d\d)/(\d
\d)/(\d\d\d\d)^);
    my($startYYYYMMDD) = $startYYYY . "-" . $startMM . "-" . $startDD;
    my($endDD, $endMM, $endYYYY) = ($endDate =~ m^(\d\d)/(\d\d)/(\d\d\d
\d)^);
    my($endYYYYMMDD) = $endYYYY . "-" . $endMM . "-" . $endDD;

    my($databaseManager) = MyDatabaseManager->new();
    ($worst, $best) = $databaseManager-
>getDailyPerformanceStatisticsForSelectedFunds($startYYYYMMDD,
$endYYYYMMDD, \...@selectedfundrefs, \...@charthash);

    push(@trace, "  Worst : $worst, Best: $best\n");       # *****
DEBUG  *****

    my(@xAxis) = '';
    my($deltaDays) = Delta_Days(($startYYYY, $startMM, $startDD),
($endYYYY, $endMM, $endDD)) + 1;

    push(@trace, "  Delta days : $deltaDays\n");           # *****
DEBUG  *****

    for(my($i) = 0; $i < $deltaDays; $i++)
      {
      push(@xAxis, "-");                                   # Dummy x-
axis label until I figure out the
rules
      }

    my(@data);
    push(@data, \...@xaxis);
    push(@data, \...@charthash);

    $|++;
    binmode STDOUT;

    my $myChart = GD::Graph::lines->new(CHARTWIDTH, CHARTHEIGHT, 1);
    $myChart->set
      (
      x_label     => 'Date',
      y_label     => 'Performance',
      title       => 'Sterling Bond Funds Performance',
      transparent => 0,
      zero_axis   => 1,
      y_min_value => $worst,
      y_max_value => $best,
      dclrs       => @chartColours,
      ) or warn $myChart->error;

    my($myImage) = $myChart->plot(\...@data) or die $myChart->error;

    push(@trace, "  Image created\n");                     # *****
DEBUG  *****

    print "Content-type: image/png\n\n";
    print $myImage->png;
    #push(@trace, $myImage->png);                      # *****  DEBUG
*****

    push(@trace, "  Image output\n");                      # *****
DEBUG  *****

    }
  }

push(@trace, "SBFPAchart.cgi left\n");                     # *****
DEBUG  *****

open(TRACE, ">ChartTestTrace.txt") or die "Cannot open $name for
write: $!"; # *****  DEBUG  *****
print TRACE @trace;                                        # *****
DEBUG  *****
close TRACE;                                               # *****
DEBUG  *****

exit 0;






-- 
To unsubscribe, e-mail: beginners-cgi-unsubscr...@perl.org
For additional commands, e-mail: beginners-cgi-h...@perl.org
http://learn.perl.org/


Reply via email to