*
* script to draw a label 
*
function label(args)
'set vpage off'
while(1)
maxlength = 0

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

say " "

i = 1
numline = 1
while (i<=numline)
say " "
say "Enter Label"

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

i = i+1
endwhile

i = 1
length = getbits (title.i) 
if( length > maxlength )
maxlength = length
endif
*
 
* Determine Position and Draw Legend Box
* --------------------------------------
xlow =0.
ylow =0.
xhigh=0.
yhigh=0.

if (ioflag!=1)
say "Draw Box Location for Label"
'set rband 21 box .25 .25 10.75 7.75'
'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 1 1'
'draw rec 'xlow' 'ylow' 'xhigh' 'yhigh

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

vsize = (yhigh-ylow)/(2.5*numline)
hsize = (xhigh-(xhigh-xlow)/10)/maxlength
hsize = 0.95*(xhigh-xlow)/maxlength
'set strsiz 'hsize' 'vsize
'set string 1 c 6'

xtspot = (xlow + xhigh)/2
yspace = (yhigh-ylow)/(numline+1)
i = 1
while (i<=numline)
yspot = yhigh - i*yspace
ytspot = yspot-0.00
 'set  line 1 1 3'
*'draw string 'xtspot' 'ytspot' 'title.i
i = i+1
endwhile
break
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 
