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

Greetings

I am having a problem with regard to the GD::Graph module.(I've tried the CPAN documentation and the relevant perldocs, but as far as I can see I've done what I'm meant to.)

I've tried to use the following code to produce a graph within a webpage:

#!/usr/bin/perl -w use strict; use warnings; use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard); use GD::Graph; print header(); print start_html( -title=>'Title' -BGCOLOR=>'#e5e6f4', -style=>{'src'=>'/myicons/styledefs.css','title'=>'rtpstd'}); print start_form, #snip form-related stuff, end_form; #snip database interrogation stuff my $graph=GD::Graph::bars->new(); my $format=$graph->export_format; #seems to default to GIF print header("image/$format"); binmode STDOUT; my @data = ( ["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"], [ 1, 2, 5, 6, 3, 1.5, 1, 3, 4] ); # I originally had @data pointing to some arrays generated from the ab +ove database interrogation, but I changed it to the above to eliminat +e this as a source of error. my $gd=$graph->plot(\@data)->$format() or die $graph->error; print $gd; print end_html;

However, where the graph should be I instead get about 20 lines of meaningless Unicode characters. I've tried manually setting $format to various formats other than the default gif (such as png, gd and jpeg), but this just produces different sets of meaningless Unicode characters. I don't get any error messages when the $gd object is created though, so I think it may be something to do with my browser settings. I've tried poking around in Firefox's and IE's image handling settings, but it doesn't seem to make any difference. I'm running Firefox v. 2.0.0.14, IE v.6 and Perl v.5.8.8

Any ideas?

TIA, campbell

Replies are listed 'Best First'.
Re: Use of GD::Graph
by moritz (Cardinal) on May 06, 2008 at 16:22 UTC
    You can print either HTML or an image. If you mix it, it will fail miserably.

    You say you want to print an image, so do that - but don't print HTML also (you use start_html and end_html)

    You're also printing two headers, remove the first one (only print the image header, not the HTML one).

    If you want to print both an image and HTML, split the script up into two scripts - one that emits the HTML which contains an <img src="http://url.to/other/perl/script.pl"> that points to the second script.

      Oh. I didn't realise that. I'll try splitting the script in two.

      Thanks very much for your quick reply.

      Campbell

        Right. I've tried creating the graph object, and passing it in a URL to a second Perl script like so:

        1st Perl script:

        use strict; use warnings; use CGI qw/:standard *table *Tr *td/; use CGI::Carp qw(fatalsToBrowser); use Date::Manip; use DBI; use GD::Graph; use GD::Graph::bars; use GD::Graph::lines; use GD::Graph::histogram; ... (snip HTML and form generation code plus database interrogation code) ... my $graph=GD::Graph::bars->new(); my $format=$graph->export_format; $graph->set ( x_label=>'Uploaders', y_label=>'Total number of uploads' ); @bar_data=(\@x-axis, \@bars); my $gd=$graph->plot(\@bar_data)->$format() or die $graph->error; print '<img src=http://foobar.com/foobar.pl?image=$gd&format=$format>' +;#sends the parameters to the second script
        And the second Perl script:
        #!/usr/bin/perl -w use strict; use warnings; use CGI qw(:standard); my $image=param('image'); my $format=param('format'); print header("image/$format"); binmode STDOUT; print $image;
        However, I just get a blank where the image should be when I do this. I know the image object is created properly, but passing it as a parameter just doesn't seem to work.

        Any ideas as to what I'm doing wrong?