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

Hello monks!
I came across this script to create a plot with perl.
The thing is that I am completely new to plot creation with perl and I don't know what I must add to finally save the image to a file, say 'test.png'.
The code is:
#!/usr/local/bin/perl -w use GD::Graph::bars; use GD::Graph::hbars; use GD::Graph::Data; # Both the arrays should same number of entries. my @data = (["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], [23, 5, 2, 20, 11, 33, 7, 31, 77, 18, 65, 52]); my $mygraph = GD::Graph::bars->new(500, 300); $mygraph->set( x_label => 'Month', y_label => 'Number of Hits', title => 'Number of Hits in Each Month in 2002', ) or warn $mygraph->error; my $myimage = $mygraph->plot(\@data) or die $mygraph->error;

Replies are listed 'Best First'.
Re: What do I need to add?
by chromatic (Archbishop) on Dec 22, 2010 at 18:53 UTC

    The POD (perldoc GD::Graph) suggests something like:

    use autodie; ... open my $fh, '>', 'test.png'; binmode $fh; print {$fh} $gd->png;

    Update: Added the all-important binmode; thanks to BrowserUk for the reminder.

Re: What do I need to add?
by Khen1950fx (Canon) on Dec 23, 2010 at 08:30 UTC
    I think that this is what you were looking for. You were missing the save_chart sub, and the script saves the chart as a gif (by default):
    #!/usr/bin/perl use strict; use warnings; use GD::Graph::bars; use GD::Graph::hbars; use GD::Graph::Data; my $data = GD::Graph::Data->new([ ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], [23, 5, 2, 20, 11, 33, 7, 31, 77, 18, 65, 52]]); my $mygraph = GD::Graph::bars->new(500, 300); $mygraph->set( x_label => 'Month', y_label => 'Number of Hits', title => 'Number of Hits in Each Month in 2002', ) or warn $mygraph->error; $mygraph->plot($data) or die $mygraph->error; save_chart($mygraph, 'test'); sub save_chart { my $chart = shift or die "Need a chart!"; my $name = shift or die "Need a name!"; local(*OUT); my $ext = $chart->export_format; open(OUT, ">$name.$ext") or die "Cannot open $name.$ext for write: + $!"; binmode OUT; print OUT $chart->gd->$ext(); close OUT; }
    Note that my @data = ([ should be
    my $data = GD::Graph::Data->new([
    Updated: Noticed something missing? Here's the corrected chart:
    #!/usr/bin/perl use strict; use warnings; use GD::Graph::bars; use GD::Graph::hbars; use GD::Graph::Data; my $data = GD::Graph::Data->new([ ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], [23, 5, 2, 20, 11, 33, 7, 31, 77, 18, 65, 52]]) or warn GD::Graph::Data->error; for my $mygraph (GD::Graph::bars->new(500, 300), GD::Graph::hbars->new(500, 300) { my $name = 'test'; print STDERR "Processing $name\n"; $mygraph->set( x_label => 'Month', y_label => 'Number of Hits', title => 'Number of Hits in Each Month in 2002', bar_spacing => 8, shadow_depth => 4, shadowclr => 'dred', transparent => 0, ) or warn $mygraph->error; $mygraph->plot($data) or die $mygraph->error; save_chart($mygraph, $name); } sub save_chart { my $chart = shift or die "Need a chart!"; my $name = shift or die "Need a name!"; local(*OUT); my $ext = $chart->export_format; open(OUT, ">$name.$ext") or die "Cannot open $name.$ext for write: + $!"; binmode OUT; print OUT $chart->gd->$ext(); close OUT; }