Greetings!
Here's some code snippets from one of my apps.
You'll need the GD::Graph module from CPAN and the two extension modules, if you want to use the min,max,sum and ceil functions.
### ================================================================== +==== ### use ### ================================================================== +==== # --- Perl Core ------------------- use strict; use warnings; use Data::Dumper; # --- Extension ------------------- use List::Util qw(min max sum); use POSIX qw(ceil); use Win32; # --- GRAPHICS -------------------- use GD; use GD::Graph::hbars; use GD::Graph::lines; use GD::Graph::points; use GD::Graph::linespoints; ### ================================================================== +==== ### BUILD your data struct and plot options, then call plot ### ================================================================== +==== ### --------------------------------------------------------- +- ### build plot @data) @data = ([], []); foreach <your values> { push @{$data[0]}, <your X>; push @{$data[1]}, <your Y>; } # print Dumper(\@data); ### --------------------------------------------------------- +- ### Plot my $s = sum(@{$data[1]}); # Sum all data pts. my $max = max(@{$data[1]}); # Max val. data pt. my ($ymax, $yticks) = scale($max,100); my %opt = ( title => "<your title>", x_label => "<your X-label>", y_label => "<your Y-label>', y_max_value => $ymax, y_tick_number => $yticks, y_number_format => "%3d", show_values => 1, ); $opt{y_max_value} = $max + 100; # Fixed $opt{y_tick_number} = 10; plot(\%opt, \@data, 'hb'); $opt{title} .= '-'; $opt{y_max_value} = $ymax; # Float $opt{y_tick_number} = $yticks; plot(\%opt, \@data, 'hb'); ### ================================================================== +==== ### UTIL: scale ### ================================================================== +==== sub scale { my ($max, $delta) = @_; $delta ||= $max * 0.1; $max = ceil(($max + $delta) / 10) * 10; my $ticks = min (ceil($max+$delta), 10); return ($max, $ticks); } ### ================================================================== +==== ### UTIL: plot (using GD::Graph::$type) ### ================================================================== +==== sub plot { ### Set options for graph, cf params and defaulta my ($rOpt, $rData, $type, $x, $y) = @_; # %Opt, @data, ... $type ||= 'hb'; # Default horiz. bars $x ||= max(scalar(@{$rData->[1]})*20, 150); # Scale to num. data.pt +s $y ||= 400; # Fixed (dyn: max(ceil( +$s/2), 400);) my @opt = %{$rOpt}; # Flatten option hash t +o array my $graph; # (width:y, height:x) if ($type eq 'pt') { $graph = GD::Graph::points->new($y, $x); } if ($type eq 'lp') { $graph = GD::Graph::linespoints->new($y, $x); +} if ($type eq 'hb') { $graph = GD::Graph::hbars->new($y, $x); } $graph->set_x_axis_font(gdTinyFont); # $graph->set(logo => ".\\KMD.gif", # logo_resize => 0.5, # logo_position => "LL", ); $graph->set(@opt) or die $graph->error; ### Plot graph, cf @$rData array # print "Plot: ", Dumper(\$rData); my $gd = $graph->plot($rData) or die $graph->error; my ($cwd, $tgt); $cwd = Win32::GetCwd(); foreach my $dir ("plot", "plot\\$PID") { $tgt = "$cwd\\$dir"; unless (-e $tgt and -d $tgt) { mkdir($tgt) or die "can't mkdir ' +$tgt': $!"; } } open(IMG, ">$tgt\\$rOpt->{title}.png") or die "can't open $tgt\\$t. +png: $!"; binmode IMG; print IMG $gd->png; }

Best regards,
allan dystrup

In reply to Re: how to build a histogram using perl? by ady
in thread how to build a histogram using perl? by sugar

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.