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; }

In reply to Re: What do I need to add? by Khen1950fx
in thread What do I need to add? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.