*
* script to draw a legend for multiple line plots
*
* (a stupid one - it asks all kinds of questions)
*
function lines(args)
'set vpage off'
while(1)
maxlength = 0

if (args="")
name   = dum.stack
ioflag = 0
else
name   = subwrd(args,1)
ioflag = subwrd(args,2)

* Extract name between quotes
* ---------------------------
       last = substr(name,1,1)
       if( last = '"' )
            nmax = 200
               n = 2
       while ( n<max )
       last = substr(name,n,1)
       if( last = '"' )
           len = n-2
             n = max
       else
             n = n+1
       endif
       endwhile
       name = substr( name,2,len )
       endif

say "name = "name
endif


if (ioflag=0)
say "How many lines are in Plot?"
pull numline
write(name,numline)
else
numline = sublin( read(name),2 )
endif

i = 1
while (i<=numline)
say " "
say "Style Type:"
say "----------"
say "1 - Solid"
say "2 - Long  Dash" 
say "3 - Short Dash" 
say "4 - Long-Short Dash"
say "5 - Dot"
say "6 - Dot-Dash"
say "7 - Dot-Dot-Dash"
say " "
say "Choose Line #"i"  Style(1-6)  <Thickness>  <Color>"

if (ioflag=0)
pull stuff.i
write(name,stuff.i,append)
else
stuff.i = sublin( read(name),2 )
endif

style.i = getstyle(stuff.i)
thick.i = getthick(stuff.i)
color.i = getcolor(stuff.i)
say "Enter Legend"

if (ioflag=0)
pull title.i
write(name,title.i,append)
else
title.i = sublin( read(name),2 )
endif

i = i+1
endwhile

say " "
say "Summary:"
say "-------"
say " "
i = 1
while (i<=numline)
if (style.i=1)
mtype="Solid          "
endif
if (style.i=2)
mtype="Long Dash      "
endif
if (style.i=3)
mtype="Short Dash     "
endif
if (style.i=4)
mtype="Long-Short Dash"
endif
if (style.i=5)
mtype="Dot            "
endif
if (style.i=6)
mtype="Dot-Dash       "
endif
if (style.i=7)
mtype="Dot-Dot-Dash   "
endif
say "Line ("i"): "mtype"  Thickness("thick.i")  Color("color.i")  Legend: "title.i
length = getbits (title.i) 
if( length > maxlength )
maxlength = length
endif
i = i+1
endwhile
*
say " "
if (ioflag=0)
say "Is this okay? (y/n)"
pull doit
endif
if (doit="y" | ioflag=1 | ioflag=2)
 
 
* Determine Position and Draw Legend Box
* --------------------------------------
xlow =0.
ylow =0.
xhigh=0.
yhigh=0.


if (ioflag!=1)
say "Draw Box (lower-left to upper-right) Location for Label"
'getinfo pagex'
pagex = result
'getinfo pagey'
pagey = result
*'set rband 21 box .25 .25 10.75 7.75'
'set rband 21 box 0 0 'pagex' 'pagey
'q pos '
xlow  = subwrd(result,3)
ylow  = subwrd(result,4)
xhigh = subwrd(result,8)
yhigh = subwrd(result,9)
write(name,xlow,append)
write(name,ylow,append)
write(name,xhigh,append)
write(name,yhigh,append)
else
xlow  = sublin (read(name),2)
ylow  = sublin (read(name),2)
xhigh = sublin (read(name),2)
yhigh = sublin (read(name),2)
endif

* White-out Legend Region
* -----------------------
'set line 0'
'draw recf 'xlow' 'ylow' 'xhigh' 'yhigh
'set line 1'
 
x1spot = xlow   + (xhigh-xlow)/10
 x2spot = x1spot + (xhigh-xlow)/4
*x2spot = x1spot + (xhigh-xlow)/2
xtspot = x2spot + (xhigh-xlow)/10
 
'set line 1 1 1'
'draw rec 'xlow' 'ylow' 'xhigh' 'yhigh

* Write in Legend Titles
* ----------------------

vsize = (yhigh-ylow)/(2.5*numline)
hsize = (xhigh-(xhigh-xlow)/10-xtspot)/maxlength
'set strsiz 'hsize' 'vsize

'set string 1 l 1'
'set string 1 l 3'

yspace = (yhigh-ylow)/(numline+1)
i = 1
while (i<=numline)
yspot = yhigh - i*yspace
*'set  line 1 'style.i' 'thick.i
 'set  line 'color.i' 'style.i' 'thick.i
'draw line 'x1spot' 'yspot' 'x2spot' 'yspot
ytspot = yspot-0.00
'set  line 1 'style.i' 'thick.i
'draw string 'xtspot' 'ytspot' 'title.i
i = i+1
endwhile
break
else
say "Try again"
endif
endwhile
function getbits (string) 
tb = ""
i = 1 
while (i<=80)
blank = substr(string,i,1)
if( blank = tb ) 
size = i-1
i = 81   
else 
i = i + 1
endif   
endwhile
return size 

function getstyle (string) 
tb = " "
nl = ""
i = 1 
while (i<=80)
blank = substr(string,i,1)
if( blank = tb | blank = nl ) 
size = i-1
i = 81   
else 
i = i + 1
endif   
endwhile
style = substr(string,1,size)
color = 1
thick = 3
return style

function getthick (string) 
size = getbits(string)
if( size = 1 )
thick = 3
else

tb = " "
nl = ""
i = 1 
while (i<=80)
blank = substr(string,i,1)
if( blank = tb | blank = nl ) 
size2 = i-1
i = 81   
else 
i = i + 1
endif   
endwhile
len = size-size2
pos = size2+1
string2 = substr(string,pos,len)

i = 1 
while (i<=80)
blank = substr(string2,i,1)
if( blank != tb ) 
thk1 = i
i = 81   
else 
i = i + 1
endif   
endwhile
i = thk1
while (i<=80)
blank = substr(string2,i,1)
if( blank = tb | blank = nl ) 
thk2 = i-1
i = 81   
else 
i = i + 1
endif   
endwhile
thick = substr(string2,thk1,thk2-thk1+1)

endif
return thick

function getcolor (string) 
size = getbits(string)
if( size = 1 )
color = 1
else

tb = " "
nl = ""
i = 1 
while (i<=80)
blank = substr(string,i,1)
if( blank = tb | blank = nl ) 
size2 = i-1
i = 81   
else 
i = i + 1
endif   
endwhile
len = size-size2
pos = size2+1
string2 = substr(string,pos,len)

i = 1 
while (i<=80)
blank = substr(string2,i,1)
if( blank != tb ) 
thk1 = i
i = 81   
else 
i = i + 1
endif   
endwhile
i = thk1
while (i<=80)
blank = substr(string2,i,1)
if( blank = tb | blank = nl ) 
thk2 = i-1
i = 81   
else 
i = i + 1
endif   
endwhile
size2 = getbits(string2)
if( size2-thk2 = 0 )
color = 1
else
string3 = substr(string2,thk2+1,size2-thk2)

i = 1 
while (i<=80)
blank = substr(string3,i,1)
if( blank != tb ) 
col1 = i
i = 81   
else 
i = i + 1
endif   
endwhile
i = col1
while (i<=80)
blank = substr(string3,i,1)
if( blank = tb | blank = nl ) 
col2 = i-1
i = 81   
else 
i = i + 1
endif   
endwhile
color = substr(string3,col1,col2-col1+1)
endif

endif
return color
 
