Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-20 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found