/****************************************************************
Author:
Cameron Rookley      rookley@aruba.ccit.arizona.edu
University of Arizona
Doctoral Candidate
Dept. of Economics/Dept. of Finance
Date: 24Nov96
Provided without guarantees for public, non-commercial use.
****************************************************************/
/* NICEBOX.SRC
             Interactive procedure to nicely generate box-plot graphs.

   Written by Cameron Rookley, University of Arizona, Fall 1996.

   Format:   nfmt=nicebox(data,ofmt,nfmt)

             Where:

        data is a TxK matrix.

        ofmt is a string used to load old graph format information
             which has been saved in a numeric matrix and a character
             string. ^ofmt.fmt for num. formats, ^ofmt.fst for char.
             information.  If set to 0, then user is prompted for all
             information.

        nfmt is a string used to save final graph information into a
             numerical matrix ^nfmt.fmt and ^nfmt.fst.  If set to 0
             nothing will be saved.  Choose a string name if you wish
             to save format information which can be loaded again by
             setting ofmt=nfmt with subsequent calls.


   Note: Be sure to issue the statement library pgraph; before calling this
         routine. Other than that, simply follow the prompts to "nicely"
         generate and modify a graph which is presentable, without having
         to remember the 8 million global variables contained in pgraph.

         Also, try to avoid calling nicebox and nicexy from same program.
*/
proc(1)=nicebox(rawdat,ofmt,nfmt);
local tit,xax,yax,resp2,resp3,grp,i,xal,minyval,maxyval,incszy,sdivy,resp4;
local resp6,resp7,ofile,nmess,resp9,mess,mfs,mxc,myc,mestxt,charinf,numinf;
local r1,r7,brkind,nextc,cont,resp10,mchoice,resp4b,respchg,charout,numout;
local resp1b;
cls;
graphset;
if ofmt $==0;
print "Note: To superscript text use [text], to subscript text use ]text[";
print;
print "Enter title of this graph.";
tit=cons; print;
print "Enter title of x-axis";
xax=cons; print;
print "Enter title of y-axis";
yax=cons; print;
xlabel(xax);
ylabel(yax);
title(tit);
print "Print time and date in top-right corner? y/n";
resp2=cons; print;
if strsect(resp2,1,1) $=="y" or strsect(resp2,1,1,)$=="Y";
   _pdate="\201GAUSS   ";
else;
   _pdate=0;
endif;
cls;
print "How would you like to label each group?";
print;
print "1  Numerically from 1-K";
print "2  Numerically with values supplied by you";
print "3  Using text supplied by you";
print "4  No labels";

resp3=con(1,1);
grp=0; xal=0;
if resp3==1;
   grp=0;
elseif resp3==2;
   i=1; grp=zeros(cols(rawdat),1);
   do while i le cols(rawdat);
      print "Enter group number for series:" i;
      grp[i]=con(1,1);
      print;
      i=i+1;
   endo;
elseif resp3==3;
   i=1; grp=0;
   do while i le cols(rawdat);
      print "Enter text (8 bytes max) for series:" i;
      if i==1;
         xal=cons;
      else;
         xal=xal|cons;
      endif;
      print;
      i=i+1;
   endo;
   asclabel(xal,0);
elseif resp3==4;
   xal="";
   asclabel(xal,0);
endif;
cls;

minyval=0; maxyval=0; incszy=0; sdivy=0;
print "Would you like direct control over y axis?";
print "Type y for direct control, any other key for auto-scaling";
resp4=cons; print;
if strsect(resp4,1,1) $=="y" or strsect(resp4,1,1,)$=="Y";
   print "Current ranges of all series" minc(rawdat)~maxc(rawdat);
   print;print "Enter minimum y value"; minyval=con(1,1);
   print;print "Enter maximum y value"; maxyval=con(1,1);
   print;print "Max number of zeros following decimal:"; _pypmax=con(1,1);
   print;print "Enter increment between major y ticks"; incszy=con(1,1);
   print;print "Enter number of minor y sub-divisions"; sdivy=con(1,1);
   ytics(minyval,maxyval,incszy,sdivy);
endif;
cls;
print;
print "Create an tkf output file?";
resp7=cons; print;
if resp7$=="y" or resp7$=="Y";
   print "Enter name of graph file WITH A .tkf extension";
   ofile=cons;
   _ptek=ofile;
endif;
cls;
nmess=0;
print "Would you like to add any extra messages or notes to your graph? y/n";
resp9=cons; print;
if resp9$=="y" or resp9$=="Y";
   print "How many messages would you like to add to the graph?";
   nmess=con(1,1);
   mess=1;
   do while mess le nmess;
    cls; print "ENTER INFORMATION FOR MESSAGE #:" mess; print;
    print "Enter font size IN INCHES (typical value=0.125) ";
    mfs=con(1,1); print;
    print "Enter x coordinate for lower left corner of message (in inches 0-9)";
    print "This number must be reasonable enough for message to fit on page";
    mxc=con(1,1); print;
    print "Enter y coordinate for lower left corner of message (in inches 0-6.85)
           ";
    myc=con(1,1); print;
    if mess==1;
      _pmsgctl=mxc~myc~mfs~0~2~10~0;
    else;
      _pmsgctl=_pmsgctl|(mxc~myc~mfs~0~2~10~0);
    endif;
    print "Enter message text.";
    mestxt=cons; print;
    if mess==1;
      _pmsgstr=mestxt;
    else;
      _pmsgstr=_pmsgstr $+ "\000" $+ mestxt;
    endif;
    mess=mess+1;
   endo;
endif;
cls;
elseif ofmt $/= 0;
 loads charinf=^ofmt;
 loadm numinf=^ofmt;
 minyval=numinf[1];
 maxyval=numinf[2];
 incszy=numinf[3];
 sdivy=numinf[4];
 if (minyval+maxyval+incszy+sdivy) ne 0;
    ytics(minyval,maxyval,incszy,sdivy);
 endif;
 nmess=numinf[5];
 r1=6; r7=12;
 if nmess>0;
    i=1;
    do while i le nmess;
      if i==1;
       _pmsgctl=numinf[r1:r7]';
       r1=r1+7; r7=r7+7;
      else;
       _pmsgctl=_pmsgctl|numinf[r1:r7]';
      endif;
      i=i+1;
    endo;
 endif;
 _pysci=numinf[r1];
 grp=numinf[r1+1:rows(numinf)];
 /* scan through charinf and extract necessary information, which has
    been separated with a "><" combination */
 brkind=strindx(charinf,"><",1);
 tit=strsect(charinf,1,brkind-1);
 charinf=strsect(charinf,brkind+2,strlen(charinf)-(brkind+1));
 brkind=strindx(charinf,"><",1);
 xax=strsect(charinf,1,brkind-1);
 charinf=strsect(charinf,brkind+2,strlen(charinf)-(brkind+1));
 brkind=strindx(charinf,"><",1);
 yax=strsect(charinf,1,brkind-1);
 charinf=strsect(charinf,brkind+2,strlen(charinf)-(brkind+1));
 brkind=strindx(charinf,"><",1);
 _pdate=strsect(charinf,1,brkind-1);
 charinf=strsect(charinf,brkind+2,strlen(charinf)-(brkind+1));
 brkind=strindx(charinf,"><",1);
 _ptek=strsect(charinf,1,brkind-1);
 charinf=strsect(charinf,brkind+2,strlen(charinf)-(brkind+1));
 if nmess>0;
   brkind=strindx(charinf,"><",1);
   _pmsgstr=strsect(charinf,1,brkind-1);
   charinf=strsect(charinf,brkind+2,strlen(charinf)-(brkind+1));
 endif;
 brkind=strindx(charinf,"><",1);
 nextc=strsect(charinf,1,brkind-1);
 charinf=strsect(charinf,brkind+2,strlen(charinf)-(brkind+1));
 xal=0;
 if strlen(nextc)>2;
    xal=nextc;
 endif;
 cont=1;
 do while cont==1;
    brkind=strindx(charinf,"><",1);
    if brkind>0;
      nextc=strsect(charinf,1,brkind-1);
      charinf=strsect(charinf,brkind+2,strlen(charinf)-(brkind+1));
      xal=xal|nextc;
    else;
      cont=0;
    endif;
 endo;
 title(tit);
 xlabel(xax);
 ylabel(yax);
 if xal $/= 0;
   asclabel(xal,0);
 endif;

 print "Change name of tkf file? (y/n) Current name:" _ptek;
 respchg=cons;
 if respchg$=="y" or respchg$=="Y";
    print "Enter new file name, being sure to use tkf extension:"; _ptek=cons;
    print;
 endif;
endif;
box(grp,rawdat);
cls;
print "Would you like to make any modifications to this graph? (y/n)";
resp10=cons;
if resp10$=="y" or resp10$=="Y";
 cont=1;
 do while cont==1;
 cls;
 print "ENTER THE NUMBER FOR ANY ITEMS YOU WOULD LIKE TO MODIFY.";
 print "AFTER EACH MODIFICATION IS MADE, THE GRAPH WILL BE REPLOTTED.";
 print;
 print " 0: Done modifying, happy with current graph.";
 print " 1: Modify main and/or axis titles and/or gauss time stamp.";
 print " 2: Modify y tick mark scheme.";
 print " 3: Modify location and/or size of extra messages. See 99.";
 print " 4: Modify contents of extra messages.";
 print " 5: Control use of scientific notation.";
 print " 6: Modify Group labels on x-axis";
 print "99: Observe a temporary coordinate grid with current graph";

 print; mchoice=con(1,1);
 if mchoice==1;
    cont=1;
    do while cont==1;
     cls;
     print "What would you like to change?";
     print;
     print "0: Nothing. Happy with current titles";
     print "1: Main Title:" tit;
     print "2: X-axis title:" xax;
     print "3: Y-axis title:" yax;
     print "4: Gauss time stamp:" _pdate;
     resp1b=con(1,1);
     if resp1b==1;
        print "Enter title of this graph.";
        tit=cons; print;
     elseif resp1b==2;
        print "Enter title of x-axis";
        xax=cons; print;
     elseif resp1b==3;
        print "Enter title of y-axis";
        yax=cons; print;
     elseif resp1b==4;
        print "Print time and date in top-left corner with Gauss stamp? y/n";
        resp2=cons; print;
        if strsect(resp2,1,1) $=="y" or strsect(resp2,1,1,)$=="Y";
          _pdate="\201GAUSS   ";
        else;
          _pdate=0;
        endif;
     elseif resp1b==0;
        cont=0;
     else;
        print "Invalid choice, pausing for 2 seconds" pause(2);
     endif;
    endo;
    cont=1;
    xlabel(xax);
    ylabel(yax);
    title(tit);
    cls;
 elseif mchoice==2;
    cls;
    print "Would you like to modify y axis tick marks? (y/n)";
    resp4b=cons; print;
    if strsect(resp4b,1,1) $=="y" or strsect(resp4b,1,1,)$=="Y";
       print "Current ranges of all series" minc(rawdat)~maxc(rawdat);
       print;print "Enter minimum y value"; minyval=con(1,1);
       print;print "Max number of zeros following decimal:"; _pypmax=con(1,1);
       print;print "Enter maximum y value"; maxyval=con(1,1);
       print;print "Enter increment between major y ticks"; incszy=con(1,1);
       print;print "Enter number of minor y sub-divisions"; sdivy=con(1,1);
       ytics(minyval,maxyval,incszy,sdivy);
    endif;
    cls;
 elseif mchoice==3;
   print "Modify location of which message?";
   print "Current message string (each message separated by null)";
   print _pmsgstr;
   print;
   mess=con(1,1);
   mfs=_pmsgctl[mess,3]; mxc=_pmsgctl[mess,1]; myc=_pmsgctl[mess,2];
   print "Enter font size for message IN INCHES. Current value:" mfs;
   mfs=con(1,1); print;
   print "Enter x coordinate for lower left corner of message (in inches 0-9)";
   print "Current value is:" mxc;
   mxc=con(1,1); print;
 print "Enter y coordinate for lower left corner of message (in inches 0-6.85)";
   print "Current value is:" myc;
   myc=con(1,1); print;
   _pmsgctl[mess,3]=mfs; _pmsgctl[mess,1]=mxc; _pmsgctl[mess,2]=myc;
 elseif mchoice==4;
    mess=1;
    do while mess le nmess;
    print "Enter text for message" mess;
    mestxt=cons; print;
    if mess==1;
      _pmsgstr=mestxt;
    else;
      _pmsgstr=_pmsgstr $+ "\000" $+ mestxt;
    endif;
    mess=mess+1;
   endo;
 elseif mchoice==5;
  print "Threshold at which to use scientific notation on y-axis?";
  print "Current value is:" _pysci;
  print; _pysci=con(1,1);
 elseif mchoice==6;
  cls;
  print "How would you like to label each group?";
  print;
  print "1  Numerically from 1-K";
  print "2  Numerically with values supplied by you";
  print "3  Using text supplied by you";
  print "4  No labels";
  resp3=con(1,1);
  grp=0; xal=0;
  if resp3==1;
    grp=0;
  elseif resp3==2;
    i=1; grp=zeros(cols(rawdat),1);
    do while i le cols(rawdat);
       print "Enter group number for series:" i;
       grp[i]=con(1,1);
       print;
       i=i+1;
    endo;
  elseif resp3==3;
    i=1; grp=0;
    do while i le cols(rawdat);
       print "Enter text (8 bytes max) for series:" i;
       if i==1;
          xal=cons;
       else;
          xal=xal|cons;
       endif;
       print;
       i=i+1;
     endo;
     asclabel(xal,0);
   elseif resp3==4;
     xal="";
     asclabel(xal,0);
   endif;
   cls;
 elseif mchoice==99;
    call gridco(grp,rawdat);
 elseif mchoice==0;
    cont=0;
 else;
    print "INVALID CHOICE!!! pausing for 2 seconds."; pause(2);
 endif;
 if mchoice ne 99;
    box(grp,rawdat);
 endif;
endo;
endif;

charout=tit $+ "><" $+ xax $+ "><" $+ yax $+ "><" $+ _pdate;
charout=charout $+ "><" $+ _ptek $+ "><" $+ _pmsgstr;
charout=charout $+ "><";
if xal ne 0;
   i=1;
   do while i le rows(xal);
      charout=charout $+ xal[i] $+ "><";
      i=i+1;
   endo;
endif;
numout=minyval|maxyval;
numout=numout|incszy|sdivy;
numout=numout|nmess;
/* write each row of _pmsgctl 1 by 1 */
if nmess>0;
i=1;
do while i le nmess;
   numout=numout|(_pmsgctl[i,.]');
   i=i+1;
endo;
endif;
numout=numout|_pysci|grp;
if nfmt $/= 0;
   save charout, ^nfmt=charout;
   save numout, ^nfmt=numout;
endif;
retp(nfmt);
endp;

proc gridco(grpval,dat);
  local gridmat,r;
  cls;
  print "Your current graph with a set of coorinate points will now";
  print "be drawn.  Please make a note of these coordinates so that";
  print "you can efficiently add whatever is necessary to your graph.";
  print "Note the 1st line on the far left is inch 1 in x.";
  print "The 1st line from the bottom is inch 1 in y.";
  print "Hit return to continue"; cons;
  gridmat=zeros(14,9);
  gridmat[.,1]=2.*ones(14,1); /* line */
  gridmat[.,2]=ones(14,1);    /* solid */
  /* horizontal lines   */
  gridmat[9:14,3]=zeros(6,1);/* x strt*/
  gridmat[9:14,4]=seqa(1,1,6);/* y strt*/
  gridmat[9:14,5]=9.*ones(6,1);/* x end */
  gridmat[9:14,6]=seqa(1,1,6);/* y end */
  /* vertical lines    */
  gridmat[1:8,3]=seqa(1,1,8);    /* x strt*/
  gridmat[1:8,5]=seqa(1,1,8);    /* x end */
  r=1;
  do while r le 7;
   gridmat[r,4]=0;  gridmat[r,6]=6.85;    /* y strt*/
   gridmat[r+1,4]=6.85; gridmat[r+1,6]=0; /* y end */
   r=r+2;
  endo;
  gridmat[.,7]=ones(14,1);
  gridmat[.,8]=4.*ones(14,1);
  gridmat[.,9]=1.*ones(14,1);
  _pline=gridmat;
  box(grpval,dat);
  _pline=0;
  retp(0);
endp;

