head     1.3;
branch   ;
access   ;
symbols  ;
locks    ; strict;
comment  @# @;


1.3
date     92.04.21.12.00.38;  author romig;  state Exp;
branches ;
next     1.2;

1.2
date     92.04.20.16.38.22;  author romig;  state Exp;
branches ;
next     1.1;

1.1
date     92.04.17.11.26.28;  author romig;  state Exp;
branches ;
next     ;


desc
@Generic graph maker.
@


1.3
log
@Fixed so it makes a range of dates even if there's a single data point.
@
text
@#! /usr/local/bin/perl

#
# Copyright (c) 1992 The Ohio State University.
# All rights reserved.
#
# Redistribution and use in source and binary forms are permitted
# provided that: (1) source distributions retain this entire copyright
# notice and comment, and (2) distributions including binaries display
# the following acknowledgement:  ``This product includes software
# developed by The Ohio State University and its contributors''
# in the documentation or other materials provided with the distribution
# and in all advertising materials mentioning features or use of this
# software. Neither the name of the University nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#

#
# Use gnuplot to make a graph.  command line options and args control 
# appearance and format and junk.
#

$MAX_XTICS = 12;
$MINUTES = 60;
$HOURS = 60 * $MINUTES;
$DAYS = 24 * $HOURS;

$options = "o:d:Dzt:p:y:P";

$usage = "usage: mkgr [-o outputfile] [-d debuglevel] [-D] [-z] 
  [-p postscript] [-y maxy] [-P] title xlabel ylabel 
  (datafile name type style)+

    -o outputfile	write the gnuplot commands to named file
			rather than stdout.
    -d debuglevel	turn on debugging
    -D			x-axis is Unix timestamps, convert to 
			dates.
    -z			automatically scale max y axis, but 
			start at 0.
    -p postscript	make postscript output.
    -P			pause after plot.
    title		title for the graph.
    xlabel		label for the x-axis.
    ylabel		label for the y-axis.
    data name style	one or more datafile,name,style pairs.\n";

require 'yagrip.pl';

$minx = 1e+10;	$miny = 1e+10;
$maxx = -1e+10;	$maxy = -1e+10;
$num_data_points = 0;

die $usage if !&getopt($options);
die "wrong number of args\n" if $#ARGV < 4;

die "can't use both -y and -z\n" 
  if defined($opt_y) && defined($opt_z);

$title = shift(@@ARGV);
$xlabel = shift(@@ARGV);
$ylabel = shift(@@ARGV);

while ($#ARGV >= 3) {
    push(@@files, shift(@@ARGV));
    push(@@names, shift(@@ARGV));
    push(@@types, shift(@@ARGV));
    push(@@styles, shift(@@ARGV));
}

die "too many args\n" if $#ARGV != -1;
 
if (defined($opt_o)) {
    close(STDOUT);
    die "can't open $opt_o for writing ($!)\n"
      if ! open(STDOUT, ">$opt_o");
}

#
# Scan the data files and find the min and max x and y coords.
#
foreach $file (@@files) {
    die "can't open $file ($!)\n" if ! open(FILE, $file);

    while (<FILE>) {
	$num_data_points++;
	chop;
	if (/[0-9\.]+\s+[0-9\.]+/) {
	    ($x, $y) = split;

	    $x += 0; $y += 0;
	
	    $minx = $x if $x < $minx;
	    $maxx = $x if $x > $maxx;
	} else {
	    $y = $_;
	    $y += 0;
	}

	$miny = $y if $y < $miny;
	$maxy = $y if $y > $maxy;
    }

    close(FILE);
}

if ($num_data_points == 0) {
    exit;
}

#
# Write out the pieces parts of the graph commands.
#
print "set title '$title'\n";
print "set xlabel '$xlabel'\n";
print "set ylabel '$ylabel'\n";
print "set tics out\n";

if (defined($opt_y)) {
    printf "set yrange [0:%g]\n", $opt_y;
} elsif (defined($opt_z)) {
    print "set yrange [0:$maxy]\n";
} else {
    print "set autoscale y\n";
}

print "set noborder\n";
print "set nozeroaxis\n";

if ($#files == 0) {
    print "set nokey\n";
}

if (defined($opt_p)) {
    print "set terminal postscript\n";
    print "set output '$opt_p'\n";
}

if (defined($opt_D)) {
    &dateline();
} else {
    print "set autoscale x\n";
    print "set xtics\n";
}

print "plot ";

if ($#files > 0) {
    $sep = ", ";
} else {
    $sep = "";
}

while ($#files >= 0) {
    print $sep if $donesome;
    $donesome = 1;
    printf "'%s' title '%s' with %s %s",
	   pop(@@files),
           pop(@@names),
	   pop(@@types),
    	   pop(@@styles);
}

print "\n";

if (defined($opt_P)) {
    print "pause -1 'type RETURN to continue'\n";
}


sub dateline {
    local(@@local_time, $mindate, $incr, $date, 
	  $span_secs, $span_days, @@tics);

    #
    # No dates given.
    #
    if ($maxx == -1) {
        printf "set noxtics\n";
	return;
    }

    #
    # One date given.
    #
    if ($minx == $maxx) {
	$maxx = $minx + $DAYS;
	# fall through, so we get a range of 2 dates on the x axis...
    }

    #
    # 2 or more dates given.
    #

    # Find start of that day
    $mindate = $minx;
    @@local_time = localtime($mindate);
    $mindate -= $local_time[2] * $HOURS;
    $mindate -= $local_time[1] * $MINUTES;
    $mindate -= $local_time[0];

    # How many days does the range cover?
    $span_secs = $maxx - $minx;
    $span_days = $span_secs / $DAYS;

    if ($span_days < $MAX_XTICS) {
	$incr = $DAYS;
    } else {
	$incr = int($span_secs / $MAX_XTICS) + (($span_secs % $MAX_XTICS) ? $DAYS : 0);
    }

    for ($date = $mindate; $date <= $maxx; $date += $incr) {
	@@local_time = localtime($date);
	push(@@tics, sprintf("\"%d/%d\" %d", 
			    $local_time[4] + 1,
			    $local_time[3],
			    $date));
    }

    printf "set xtics (%s)\nset xrange [$mindate:$maxx]\n",
		   join(',', @@tics);
}
@


1.2
log
@Don't create a graph if there's no data.
@
text
@d191 2
a192 6
	@@local_time = localtime($minx);
	printf "set xtics (\"%d/%d\" %d)\n",
		       $local_time[4] + 1,
		       $local_time[3],
		       $minx;
	return;
@


1.1
log
@Initial revision
@
text
@d54 3
a56 2
$minx = 9999999999;	$miny = time;
$maxx = -1;		$maxy = -1;
d90 1
d109 4
@
