#!/usr/bin/perl -w

# draw html bars using simple data
#
# 2003-04-20 Dobrica Pavlinusic <dpavlin@rot13.org>
# for usage with PLies
#
# usage:
#
# use draw_bar.pm
# draw_bar("
# title which will be centered
# name of column[tab]2.44
# name 2	[tab]3.11
# " [ , # "#ff0000", "#00ff00", ... ] );
#
# if you don't enter colors, it will use default ones
#
# design based on status page from BlueEyedOS available at
# http://www.blueeyedos.com/project/status.html
#

use strict;

sub draw_bar2 {

my $data = shift @_;
my @colors = ( '#FF99CC','#FFCC99','#FFFF99','#CCFFCC','#99CCFF','#CC99FF' );

@colors = @_ if (@_);

print '
<table width="70%" border="0" cellspacing="0" cellpading="0" bgcolor="#ffffff">
';

my @names;
my @vals;
my $min_val;
my $max_val;

foreach (split(/\n+/, $data)) {
	chomp;
	next if (/^$/);
	my ($name,$value) = split(/\t+/,$_);
	push @names,$name;
	push @vals,$value;

	next if (! $value);

	# set initial value
	$min_val = $value if (! $min_val);
	$max_val = $value if (! $max_val);

	# update it to latest
	$min_val = $value if ($value < $min_val);
	$max_val = $value if ($value > $max_val);
}

my $nr = 0;
foreach my $name (@names) {
	my $val = shift @vals;
	# calculate percent from absolute value
	my $p;
	if ($val) {
		$p = int( ($val - $min_val) * 90 / ($max_val - $min_val) );
		print '
        <tr>
         <td width="10% nowrap">'.$name.'</td>
         <td>
          <table width="100%" border="1" cellspacing="0" cellpadding="0" bgcolor="#d8d8d8" bordercolordark="#efefef" bordercolorlight="#b8b8b8">

           <tr>
            <td>
             <table width="'.($p+10).'%" border="1" cellspacing="0" cellpadding="0" bgcolor="#889cc8" bordercolordark="#5874a0" bordercolorlight="#a0b4d8">
              <tr>
               <td>&nbsp;</td>
              </tr>
             </table>
            </td>
           </tr>

          </table>
         </td>
         <td align="right">'.$val.'</td>
        </tr>
		';
		$nr++;
		$nr = 0 if ($nr > $#colors);
	} else {
		# move name to middle of bar
		print '
			<tr>
			<td align="center" width="100%" colspan="2"><b>'.$name.'</b></td>
			</tr>
		';
		$nr = 0;	# reset color
	}
}

print '
</table>
';

} # sub

1;
