
# SUMMARY NOTES
# -------------
# => 


use strict;
use vars qw/$HISTOGRAM_WIDTH $HISTOGRAM_HEIGHT $HISTOGRAM_CHAR/;

sub histogram {
   my @numbers = @_;
   my $count = @numbers;

   my $max = 0;
   for (@numbers) {
      $max = $_ if $_ > $max;
   }

   my ($w, $h, $ch) = ($HISTOGRAM_WIDTH, $HISTOGRAM_HEIGHT, $HISTOGRAM_CHAR);
   die "Invalid height and/or width paramaters\n" if $w =~ /\D/ or $h =~/\D/;
   $h  = 20     unless $h;
   $w  = $count unless $w;
   $ch = '*'    unless $ch =~ /^\S$/;
   if ($w < $count) {
      print "WARNING: With the 'WIDTH' parameter that you have supplied,\n";
      print "your chart will be truncated!!\n";
      $count = $w;
   }

   my @fudged_numbers;
   push @fudged_numbers, int $_ * $h / $max + 0.5 for @numbers;

   my @bars;
   push @bars, $ch x $_ . ' ' x ($h - $_) for @fudged_numbers;

   my @chart;
   my $w2 = $w + 1;
   my $spacing = $w2 >= ($count * 2) ? ' ': '';
   my $bar_width = int $w2 / $count - 1 || 1;
   for my $i (0..($h-1)) {
      my $temp = '';
      for my $j (0..($count-1)) {
         $temp .= (substr $bars[$j], $i, 1) x $bar_width;
         $temp .= $spacing unless $j == ($count - 1);
      }
      $temp .= "\n";
      unshift @chart, $temp;
   }

   return @chart;
}

1;

