Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Adding text to a GD::Graph object...

by Clownburner (Monk)
on Jun 22, 2002 at 01:20 UTC ( [id://176429]=perlquestion: print w/replies, xml ) Need Help??

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

I've been fiddling with GD::Graph and GD::Text all day, but this is eluding me.

GD::Graph is easy enough to use, and I have it producing the graphs I want. But now I would like to add a small table of text at the bottom of the returned PNG graphic, and I can't quite figure that out.

I've tried using the instantiated object as the target of a GD::Text::Wrap operation, but it returns the error "Not a GD::Image object". Puzzling, because the docs clearly say:
$graph->plot(\@data) Plot the chart, and return the GD::Image object.
So I did this (most of this code is samples from the man pages, I will be using this with real data once I get the output done properly):
#!/usr/bin/perl use GD::Graph::bars3d; use GD::Text::Wrap; my @data = ( [qw (Sun Mon Tue Wed Thu Fri Sat) ], [ 1100, 1500, 1800, 1950, 2324, 2162, 140], [ 103, 2000, 1173, 908, 232, 1200, 100], ); my $graph = new GD::Graph::bars3d( 495, 211 ); $graph->set( x_label => 'Day of the week', dclrs => [ qw(red dgreen) ], bar_spacing => 10, cumulate => 1, ); my ($gd) = $graph->plot( \@data )->png; my $text = <<EOSTR; Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. EOSTR my $wrapbox = GD::Text::Wrap->new( $gd, line_space => 4, color => 'black', text => $text, ); binmode STDOUT; print $gd;
..Which returns the error:
Not a GD::Image object at ./graphtest.pl line 26

Any clues? I think I just don't know GD well enough to know the best way to do this. All help appreciated.

"Non sequitur. Your facts are un-coordinated." - Nomad

Replies are listed 'Best First'.
Re: Adding text to a GD::Graph object...
by dree (Monsignor) on Jun 22, 2002 at 09:56 UTC
    GD::Text::Wrap require a GD object.
    So you have to put the 3d graph in an image and then make a GD object with it to pass to GD::Text::Wrap.

    This works for me:
    use GD; use GD::Graph::bars3d; use GD::Text::Wrap; use strict; my @data = ( ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], [ 1203, 3500, 3973, 2859, 3012, 3423, 1230] ); my $graph = GD::Graph::bars3d->new( 400, 300 ); $graph->set( x_label => 'Day of the week', y_label => 'Number of hits', title => 'Daily Summary of Web Site', ); my $gd = $graph->plot( \@data ); my $img_tmp="img_tmp.png"; open FILE, ">$img_tmp"; binmode FILE; print FILE $gd->png; close FILE; my $gd_image = GD::Image->newFromPng($img_tmp) ; my $text = <<EOSTR; Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. EOSTR my $wrapbox = GD::Text::Wrap->new( $gd_image, line_space => 4, color => 'black', text => $text, ); $wrapbox->set(align => 'left', width => 120); $wrapbox->draw(110,140); open FILE, ">img.png"; binmode FILE; print FILE $gd_image->png; close FILE;

    In my Win32 box, it works properly with GD 1.27.2.
    If you have a more recent version of GD, instead making a temporary image, you could use:
    my $gd_image = GD::Image->newFromPngData($gd->png);
      Thanks!

      THere's just one problem: It doesn't work for me. :-(

      Any suggestions? Maybe I've got a bad library somewhere?

      > ./graphtest.pl Not a GD::Image object at ./graphtest.pl line 26 > perl -mGD -e 'print "$GD::VERSION\n"' 1.33 > perl -v This is perl, version 5.005_03 built for i386-linux

      "Non sequitur. Your facts are un-coordinated." - Nomad
Re: Adding text to a GD::Graph object...
by Clownburner (Monk) on Jun 22, 2002 at 01:22 UTC
    Ooh, minor bug: If this wasn't returning an error, I'd probably want to be printing $wrapbox instead of $gd. So sue me. ;-)
    "Non sequitur. Your facts are un-coordinated." - Nomad

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-24 01:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found