#!/usr/local/bin/perl
#
# Name: playersgraph.pl
# Based on an example file of Plotter.pm perl module
# by Piotr Klaban <poczta@klaban.torun.pl>
# Date: Mar 15 1999

# Modified by lav@iki.fi 011106

use Graphics::Plotter qx(parampl);

$wipes="199606|199612|199712|199809|200001|200103|200211|200403|200505|200607|200710";

#Data is unordered file with lines of form: label, value
#label of form: yyyymm (eg. 199912), value integer


$outfile = shift(@ARGV) || die "You must supply a gif filename\n";
$| = 1;

# Globals
$XSIZE=600;$YSIZE=280;$FONTSIZE=0.04;

$NORM="black";
$DIM="lightgrey";
$RED="red3";

#Get data (label,value) series
$count=1;
my($miny,$maxy,$minx,$maxx)=(9999,0,9999,0);
while(<>) {
    if (/^\#/) {next;} 
    if(/(\d+)\s*,\s*(\d+)/) {
	($label,$x,$y)=($1,$count++,$2);
	$data{$label}{'x'}=$x;
	$data{$label}{'y'}=$y;
	if(($x<$minx)) {$minx=$x;}
	if(($x>$maxx)) {$maxx=$x;}
	if(($y && $y<$miny)) {$miny=$y;}
	if(($y && $y>$maxy)) {$maxy=$y;}

    }
}
if($miny>25) { $miny=25; }
debug("Max ($maxx,$maxy), Min ($minx,$miny)\n",1);


open(OUTFILE,">$outfile") || "$outfile: $!\n";

# set Plotter parameters
Graphics::Plotter::parampl ("BITMAPSIZE", "${XSIZE}x${YSIZE}");
Graphics::Plotter::parampl ("BG_COLOR", "white"); # background color for window
Graphics::Plotter::parampl ("TRANSPARENT_COLOR", "blue");

# create an Gif Plotter with the specified parameters
$handle = Graphics::Plotter::GIF->new(STDIN, OUTFILE, STDERR);

# open X Plotter, initialize coordinates, pen, and font
if ($handle->openpl() < 0) {
    die "Could not create plotter: $!\n";
}
$handle->fspace (0.0, 0.0, 1.0, 1.0);  # use normalized coordinates
$handle->pencolorname ($NORM);
$handle->ffontsize ($FONTSIZE);
$handle->fontname ("HersheySerif");


$handle->erase ();
$handle->fbox(0,0,1,1);

#Draw Y-legends, step 25
for($y=0;$y<$maxy;$y+=25) {
    next if($y<$miny); next if($y>$maxy);

    $handle->pencolorname ($DIM);
    $handle->fline(0,$y/$maxy,1,$y/$maxy);
    $handle->fmove(0+$FONTSIZE,$y/$maxy);

    $handle->pencolorname ($NORM);
    $handle->alabel('c','b',"$y");
}

foreach $label (sort keys %data) {
    ($x=$data{$label}{'x'}/$maxx) || die "Huh";
    ($y=$data{$label}{'y'}/$maxy) || ($y=$lasty);
    if($label =~ /\d\d(\d\d)\d\d/) {$year=$1;}

    $handle->pencolorname ($NORM);
    #Don't draw the first point
    if(!defined($lasty)) { $handle->fmove($x,$y);}
    else {
	$handle->fline($lastx,$lasty,$x,$y);
	debug("Plotting to ($x, $y) for $entry\n",2);

	#Draw a separator for year, with two digits as text
	if($year!=$lastyear) {
	    $handle->pencolorname ($DIM);
	    $handle->fline($x,$miny/$maxy,$x,0.5);
	    $handle->fmove($x,$miny/$maxy-.05);

	    $handle->pencolorname ($NORM);
	    $handle->alabel('c','b',"$year");
	    debug("Year $year\n",2);
	}
	#Mark 'wipes' with red separator
	if($label =~ /($wipes)/) {
	    $handle->savestate(); {
		my($old)=$handle->pencolorname ($RED);
		$handle->fline($x,$miny/$maxy,$x,1);
	    } $handle->restorestate();
	    debug("Wipe\n",2);
	}
    }

    $lasty=$y;$lastx=$x;$lastyear=$year;
}


#Texts
$handle->fmove(1,0);
$handle->alabel('r','b',"lav\@iki.fi");

my(@text)=("players min=$miny max=$maxy","(red for wipes)","Monthly averages");
my(%pos)=('x'=>0.5, 'y'=>0.0);
my($line)=0;

foreach $text (@text) {
    $handle->fmove($pos{'x'},($line++)*$FONTSIZE);
    $handle->alabel('c','b',"$text");
}


#Finish

$handle->closepl();                    # close Plotter
close(OUTFILE);

exit;


#--- Personal toolkit functions ---

# Print given message if current verbose level allows it
#
# Inputs: Message to be printed, [priority level of the message (default=1)]
# Output: None
# Globals(IN) : $Main::verbose, $Main::OUTPUT
# Globals(OUT): -
sub debug {
    my($data,$level)=@_;
    if(!defined($Main::OUTPUT)) {$Main::OUTPUT=\*STDOUT;}
    if(!defined($level)) {my($level)=1;}
    if(defined($Main::verbose) && ($Main::verbose>=$level)) {
        printf { $Main::OUTPUT } $data;
    }
}

sub info {
    debug(@_,1);
}









