define FOGR,0  rem  section dealing with graph as a whole
define FSGR,1
define FAGR,2
define TCGR,3
define LCGR,4
define LWGR,5
define PDGR,6
define PSGR,7
define BGGR,8
define PWGR,9
define PHGR,10
define BWGR,11
define DCOLGR,12
define GRGR,17
define ROWS,18
define COLUMNS,19
define TDGR,20
define GRAPHTYPE,21
define XLABEL,22
define YLABELS,23
define THICKNESSGR,29
define FRGR,30
define OLGR,31
define OTGR,32
define ORGR,33
define OBGR,34
define ILGR,35
define ITGR,36
define IRGR,37
define IBGR,38
define MTGR,39
define MSGR,40
define PIGR,41
define MAGR,42
define PSIZE,43
define FOA,0   rem  Section dealing with axis
define FSA,1
define FAA,2
define TCA,3
define LCA,4
define LWA,5
define STA,6
define LGA,7
define INA,8
define UPA,9
define LOA,10
define NSA,11
define AA,12
define BA,13
define WIDTHA,14
define HEIGHTA,15
define HOTXA,16
define HOTYA,17
define FORMATA,18
define LENGTHA,19
define PAXIS,20
define FOD,0    rem Section dealing with data set
define FSD,1
define FAD,2
define TCD,3
define LWD,4
define LCD,5
define PDD,6
define PSD,7
define FCD,8
define MTD,9
define MSD,10
define MCD,11
define FRD,12
define TDD,13
define PDATA, 14
define FOM,0    rem Section dealing with marker keys - text properties
define FSM,1
define FAM,2
define TCM,3
define LWM,4    rem line properties
define LCM,5
define PDM,6
define PSM,7
define MTM,8    rem marker properties- repeated 5 times
define MSM,9
define MCM,10
define XSM,28
define YSM,29
define XEM,30
define YEM,31
define NSM,32
define MDATA,33
define FOC,0    rem Section dealing with colour keys - text properties
define FSC,1
define FAC,2
define TCC,3
define LWC,4    rem line properties
define LCC,5
define PDC,6
define PSC,7
define FCC,8    rem colour (repeated 16 times)
define XSC,20
define YSC,21
define XEC,22
define YEC,23
define CDATA,24
define LINES_ONLY,0   rem types of 2D graph
define POINTS_ONLY,1
define LINES_AND_POINTS,2
define VERTICAL_HISTOGRAM,3
define HORIZONTAL_HISTOGRAM,4
define STACKED_VERTICAL_HISTOGRAM,5
define STACKED_HORIZONTAL_HISTOGRAM,6
define PICTOGRAM,7
define PIE_CHART,8
define TWO_LINES,9
define BOX_AND_TAILS,10
define DISTIBUTION,11
define GENERAL,12
define LINE_HISTO,13
define DRAG_ALLOWED, 0x1
define SELECT_ALLOWED, 0x2
define TEXT_EFFECTS, 0x4
define FILL_EFFECTS, 0x8
define LINE_EFFECTS, 0x10
define GRAPH_COLOUR_ALLOWED, 0x20
define THREE_DEE_ALLOWED, 0x40
define GRID_ALLOWED, 0x80
define EXPLODE_ALLOWED, 0x100
define COLOURS_ALLOWED, 0x200
define BAR_WIDTH_ALLOWED, 0x400
define POINT_DETAILS_ALLOWED, 0x800
define TICKS_ALLOWED, 0x1000
define DIMENSIONS_ALLOWED, 0x2000
define TOP_ALLOWED, 0x4000
define DELETE_ALLOWED, 0x8000
define ALL_SELECTED, 0x10000
define NONE_SELECTED, 0x20000
define REDRAW_NEEDED, 0x40000



rem : *****************************************************************
rem : General Commentary on Axes
rem : 
rem : In general, an axis contains the following items:
rem : 
rem : 1)   An axis line which represents the range of the variable plotted
rem : 
rem : 2)   Two or more 'major ticks' which represent specific values on
rem : that line.  Each tick is labelled with a number.
rem : 
rem : 3)   Possibly some 'minor ticks' between pairs of major ticks. These
rem : represent specific but unlabelled values.  On a linear axis both major
rem : and minor ticks are regularly spaced.  On a log axis the major ticks
rem : are evenly spaced one decade apart and the minor ticks are 
rem : logarithmically spaced in between.
rem : 
rem : 4)  Possibly an axis label. We follow certain conventions:
rem : 
rem :     The label is always in the same typeface and colour as the numbers
rem : used to label the axis, but magnified 1.5 times.
rem : 
rem : Labels on horizontal axes are centred under the axis
rem : 
rem : Labels on vertical axes are placed at the top.
rem : 
rem : On a normal vertical axis, the numbers are to the left and ticks to the
rem : right. If an axis is inverted the numbers come to the right and ticks 
rem : are one the left.   Inverted vertical axes can be placed at the 
rem : left of a graph.
rem : 
rem : Horizontal axes do not need to be inverted.
rem : 
rem : An axis has several 'public' features:
rem : 
rem : 1)    Its dimensions : breadth and height.
rem : 
rem : 2).   The position of the 'hot spot' relative to the bottom corner
rem : of the axis. The hot spot is the 'low' end of the axis line.
rem : 
rem : 3).   Two constants, a and b, are used to convert the value of a 
rem : variable x into a graph position along the axis. The formulas used 
rem : are
rem :            x' = ax+b           for a linear axis, and
rem :            x" = a log(x) +b    for a logarithmic axis
rem : 
rem : Each axis type has two separate calls: One to determine its 
rem : dimensions and the relative position of the hot spot, and a 
rem : second to actually display it.
rem : 
rem : Addition (930307) :  This addition is needed when axes are being drawn
rem : for 3-D graphs. The grid-lines stemming from an axis are imagined
rem : as being drawn on the inside of a shallow open box which 'contains'
rem : the graph. Each axis function terefore needs an extra parameter which 
rem : represents the 'thickness' of the graph.  We regard this as a general
rem : graph property and put in to the GR parameter block.

rem : This function is entered with the extreme values of an axis.
rem : It returns the greater number of places needed by either
rem : of the two values, plus 1
rem : If the NSA marker is set, the axis is not drawn and only
rem : a null object is planted

macro CRM_decimal_places (x,y)
local j,k 
      j = len(format(x,"%0.5g"))
      k = len(format(y,"%0.5g"))
      if ( k > j) then = k+1
      =j+1
endmacro

rem : *****************************************************************
rem : This function plans a horizontal axis to appear in the given window.
rem : The function is passed a complete GR parameter list in p,and
rem : a partially complete axis list in xp. In particular, xp(WIDTHA)
rem : is set to the required width of the axis. It may be a dummy!
rem : The function computes the height and the position of the hot-spot.
rem : and the height of the axis
rem : if xp(LGA) is set the axis is taken as logarithmic

macro CRM_plan_horizontal_axis(w,xp(),p())


     local res, firstlength,lastlength,z,l,h,a,scalelength
     on_error_exit
                                   rem : set required font  
     res = font(w,xp(FOA),xp(FSA),xp(FSA)*xp(FAA)/100)

                  rem : set true axis length, starting in middle of first
                  rem : value and ending in middle of last
     
     firstlength = textwidth(w,format(xp(LOA),"%0.5g"))
     lastlength = textwidth(w,format(xp(UPA),"%0.5g"))
     scalelength = xp(WIDTHA) - (firstlength+lastlength)/2-0.33*p(THICKNESSGR)
     if xp(LGA) then
          z = CRM_log_scale(xp(LOA),xp(UPA),scalelength)
     else
          z = CRM_linear_scale(xp(LOA),xp(UPA),scalelength)
     endif
     if z = "0:0" then = "Bad axis"
     xp(AA) = CRM_extract(z,"a=")
     xp(BA) = CRM_extract(z,"b=")
     l=CRM_extract(z,"l=")
     h = CRM_extract(z,"h")
     a = CRM_extract(z,"a")
     firstlength = textwidth(w,format(l,"%0.5g"))
     xp(HOTXA) = firstlength/2
     if len(p(XLABEL)) > 0 then
          xp(HEIGHTA) = 8*xp(FSA)    : rem was 7
     else
          xp(HEIGHTA) = 5*xp(FSA)    : rem was 4
     endif
     xp(HOTYA) = xp(HEIGHTA)
     xp(LENGTHA) = scalelength
     =0
endmacro



rem : *********************************************************************
rem : This function plans a vertical axis to appear in the given window.
rem : The function is passed a complete GR parameter list in p,and
rem : a partially complete axis list in yp. In particular, xp(HEIGHTA)
rem : is set to the required height of the axis. It may be a dummy or 
rem : an estimate!
rem : The function computes the height and the position of the hot-spot.
rem : and the width of the axis
macro CRM_plan_vertical_axis(w,yp(),p())

   local z,a,b,l,h,i,res,y,qq
   local charheight, scalelength,labelwidth,q,tf
   on_error_exit

                                              rem : set required font  
   res = font(w,yp(FOA),yp(FSA),yp(FSA)*yp(FAA)/100)
                         rem : set true axis length, starting in middle of 
                         rem : first value and ending in middle of last
   charheight = textheight(w,"1")
   tf = p(TDGR)*0.55 * p(THICKNESSGR)
   scalelength = yp(HEIGHTA)-charheight-tf-(len(p(YLABELS)) > 0) * yp(FSA)*3.0
                                              rem : calculate scale parameters
   z = CRM_linear_scale(yp(LOA),yp(UPA),scalelength)
   if z = "0:0" then = "Bad vertical axis"
   a = CRM_extract(z,"a=")
   b = CRM_extract(z,"b=")
   yp(AA) = a
   yp(BA) = b
   l=CRM_extract(z,"l=")
   h=CRM_extract(z,"h=")
   i=CRM_extract(z,"i=")
   if i = 0 then = "Bad vertical axis"
   if a=0 then ="A=0"
   while i*a < 1.2* charheight
      i = i * 2
   endwhile
   q = (h-l)/i
   i = (h-l)/floor(q)
               rem : Now generate all label values and find maximum width
   labelwidth = 0
   y=l
   while (y <= h + i/2)
      q = textwidth(w,format(y,"%0.5g"))
      if q > labelwidth then labelwidth = q
      if (i > 0.000001) then
          y=round(y+i,6)
      else
          y=y+i
      endif
   endwhile
   yp(WIDTHA) = labelwidth + charheight
   yp(HOTXA) =  yp(WIDTHA)
   yp(HOTYA) = 0
   yp(LENGTHA) = scalelength
   =0  
endmacro
        


rem : **********************************************************************
rem : This function actually draws the axis specified in p and xp, with the
rem : hot-spot located at xs,ys. It is assumed that the axis has already been
rem : planned with plan_horizontal_axis
rem : tl is set to the length of the grid-lines needed, and 
rem : tag is the drawing tag
rem : cp is the position where the axis is crossed by the y-axis

macro CRM_draw_horizontal_axis(w,xs,ys,dpp,xp(),p(),tl,tag,cp)

     local firstlength, lastlength,scaleoffset,numberlength,pos
     local linelevel,numberlevel,tick,dump,res,l,h,i,q,x,string,z,a,b
     local active_length,dp,ii
                                             rem : Set attributes as needed

     res = lineattributes(w,1,p(LWGR),xp(LCA),0)
     res = font(w,xp(FOA),xp(FSA),xp(FSA)*xp(FAA)/100)

               rem : set true axis length, stating in middle of first value
     firstlength = textwidth(w,format(xp(LOA),"%0.5g"))
     lastlength = textwidth(w,format(xp(UPA),"%0.5g"))
     dp = CRM_decimal_places(xp(LOA),xp(UPA))
rem  scalelength = xp(WIDTHA) - (firstlength+lastlength)/2-0.33*p(THICKNESSGR)
 
                                                     rem : determine scale

     z = CRM_linear_scale(xp(LOA),xp(UPA),xp(LENGTHA))
     if z = "0:0" then = "Bad horizontal axis"
     xp(AA) = CRM_extract(z,"a=")
     xp(BA) = CRM_extract(z,"b=") + xs
     l=CRM_extract(z,"l=")
     h=CRM_extract(z,"h=")
     i=CRM_extract(z,"i=")
     a = xp(AA)
     b = xp(BA)
     while i*a < 0.6*(firstlength+lastlength)
        i = i * 2
     endwhile
     linelevel = ys+dpp
     numberlevel = linelevel - 2.25 * textheight(w,"1")
     tick = xp(FSA)/2
     q = (h-l)/i
     i = (h-l)/floor(q)
             
                                           rem : Set up initial conditions
     res = settag(tag,TICKS_ALLOWED+SELECT_ALLOWED+TEXT_EFFECTS+DELETE_ALLOWED+REDRAW_NEEDED)
     res = group(w)
                               rem : don't output a font inside a group!!!
     res = line(w,xs,linelevel,xp(LENGTHA),0)
     if xp(NSA) = 0 then
    
                                        rem : Draw scale values and major ticks
         x=l  
         while(x < h+i/2)
            if (x<>0) then
               if abs(l/x) > 50 then x=0
            endif
            string = format(x,"%0.5g")
            numberlength = textwidth(w,string)
            pos = x*a+b-numberlength/2
            if p(GRGR) = 0 then res=line(w,x*a+b,linelevel,0,2*tick)
            if round(x,dp) <>0 or dpp = 0 then
                res=string(w,pos,numberlevel,string,xp(TCA),p(BGGR))
            endif
            x=x+i
         endwhile
    
                                      rem : Draw grid lines across the picture
    
                                                        rem : Draw minor ticks
        if (xp(STA) > 1) then
           ii=i/xp(STA)
           x=l
           while (x < h)
              if (x>l) then res = LINE (w,x*a+b,linelevel,0,tick)
              x = x + ii
           endwhile
        endif
        if p(GRGR) > 0 then
           res = lineattributes(w,1,p(LWGR),0xAAAAAA00,0)
           linelevel = ys
           x=l+i
           while ( x <= h+i/2)
              if (p(THICKNESSGR) = 0 ) then
                  res = line(w,x*a+b,linelevel,0,tl)
              else
                  res = line(w,x*a+b,linelevel,0.33*p(THICKNESSGR),0.55*p(THICKNESSGR))
                  res = line(w,x*a+b+0.33*p(THICKNESSGR),linelevel+0.55*p(THICKNESSGR),0,tl)
              endif        
              x=x+i
           endwhile
           res = lineattributes(w,1,p(LWGR),xp(LCA),0)
        endif
                                     rem outputlabel
        if len(p(XLABEL)) > 0 then
          dump = xp(FSA)
          xp(FSA) = 1.5 * xp(FSA)
          h = CRM_extract(CRM_finddims(w,xp,p(XLABEL)),"x=")
          if cp <= 2 then res =CRM_showlabel(w,xp,p(XLABEL),xs+(xp(WIDTHA)-h)/2, ys-4.7*xp(FSA))
          if cp > h then res = CRM_showlabel(w,xp,p(XLABEL),xs,ys-3.7*xp(FSA))
          if cp > 2 and cp <=h then res = CRM_showlabel(w,xp,p(XLABEL),xp(WIDTHA)-h,ys-4.7*xp(FSA))
          xp(FSA) = dump
       endif
    endif
    res = endgroup(w)
    =0
endmacro


rem : **********************************************************************

macro CRM_draw_horlog_axis(w,xs,ys,dpp,xp(),p(),tl,tag)

rem : This function draws a logarithmic horizontal axis as specified in
rem :  p and xp, with the hot-spot located as xs,ys.
rem : tl is set to the length of the grid-lines needed and tag is
rem : the drawing tag
   local firstlength, lastlength,scaleoffset,numberlength,pos
   local linelevel, numberlevel, tick, dump,res,l,h,i,q,x,string,z,a,b
   local alog,d,y
   on_error_exit
   rem : Set attributes as needed
   res = lineattributes(w,1,p(LWGR),xp(LCA),0)
   res = font(w,xp(FOA),xp(FSA),xp(FSA)*xp(FAA)/100)
             rem : set true axis length, stating in middle of first value
   firstlength = textwidth(w,format(xp(LOA),"%0.5g"))
   lastlength = textwidth(w,format(xp(UPA),"%0.5g"))
rem   scalelength = xp(WIDTHA) - (firstlength+lastlength)/2-0.55*p(THICKNESSGR)
   rem : scaleoffset = xs+firstlength/2
   scaleoffset = xs
   if xp(LENGTHA) < 200  then = "X"
   
   rem : determine scale
   z = CRM_log_scale(xp(LOA),xp(UPA),xp(LENGTHA))
   a=CRM_extract(z,"a=")
   b=CRM_extract(z,"b=") + scaleoffset
   xp(AA) = a
   xp(BA) = b
   l=log(CRM_extract(z,"l="))
   h=log(CRM_extract(z,"h="))
   i=CRM_extract(z,"i=")
   rem : Set up initial conditions 
   res = settag(tag,TICKS_ALLOWED+SELECT_ALLOWED+TEXT_EFFECTS+DELETE_ALLOWED+REDRAW_NEEDED)
   linelevel = ys+dpp
   res = group(w)
          rem don't output a font inside a group!!!
   res = line(w,scaleoffset,ys,xp(LENGTHA),0)
   if xp(NSA) = 0 then
       
                     rem : Draw scale values and major ticks
          x=l
          while(x <= h+0.5)
              alog = CRM_antilog(x)
              numberlength = textwidth(w,format(alog,"%0.5g"))
              pos = x*a+b-numberlength/2
              res=line(w,x*a+b,linelevel,0,xp(FSA))
              if x = l then pos = pos + numberlength
              res=string(w,pos,ys-2*xp(FSA),format(alog,"%0.5g"),xp(TCA),p(BGGR))
                x=x+1
          endwhile
       
          rem : Draw grid lines across the picture
          if p(GRGR) > 0 then
             res = lineattributes(w,1,p(LWGR),0xAAAAAA00,0)
             x=l+1   
             linelevel = ys
             while ( x <= h+0.5)
               d=x*a+b
                if (p(THICKNESSGR) = 0) then
                    res = line(w,d,linelevel,0,tl)
                 else
                    res = line(w,d,linelevel,0.33*p(THICKNESSGR),0.55*p(THICKNESSGR))
                    res = line(w,d+0.33*p(THICKNESSGR),linelevel+0.55*p(THICKNESSGR),0,tl)
              endif        
                  x=x+1
             endwhile
          res = lineattributes(w,1,p(LWGR),xp(LCA),0)
          endif
       
          rem : Draw minor ticks
          if (xp(STA) > 0) then
             x = l
             while (x < h)
                for q = 2 to 9
                   y = log(q)+x
                   res = LINE (w,y*a+b,ys,0,xp(FSA))
                next q
                x = x + 1
             endwhile
          endif
          res = endgroup(w)
    
          if len(p(XLABEL)) > 0 then
            dump = xp(FSA)
            xp(FSA) = 1.5 * xp(FSA)
            res=CRM_extract(CRM_finddims(w,xp,p(XLABEL)),"x=")
            res=CRM_showlabel(w,xp,p(XLABEL),xs+(xp(WIDTHA)-res)/2, ys-3.7*xp(FSA))
            xp(FSA) = dump
          endif
      endif
      res = endgroup(w)
      =0
endmacro



rem : ********************************************************************
rem : This function draws a linear vertical axis as specified in
rem : p and xp, with the hot-spot located as xs,ys.
rem : tl is set to the length of the grid-lines needed and tag is
rem : the drawing tag

macro CRM_draw_vertical_axis(w,xs,dpp,ys,yp(),p(),tl,tag,has_top_label)
   local z,a,b,l,h,i,res,y,tick,dump,d
   local charheight, lineoffset,textoffset,q,il,ir
                                          rem : set drawing parameters
   res = lineattributes(w,1,p(LWGR),yp(LCA),0)
   res = font(w,yp(FOA),yp(FSA),yp(FSA)*yp(FAA)/100)
   charheight = textheight(w,"1")
   rem scalelength = yp(HEIGHTA)-charheight - has_top_label * yp(FSA)*3.0
                           rem : calculates scale parameters
   z = CRM_linear_scale(yp(LOA),yp(UPA),yp(LENGTHA))
   if z = "0:0" then = "Bad vertical axis"
   a=CRM_extract(z,"a=")
   b = CRM_extract(z,"b=")+ys
   yp(AA) = a
   yp(BA) = b
   l=CRM_extract(z,"l=")
   h=CRM_extract(z,"h=")
   i=CRM_extract(z,"i=")
   while i*a < 1.2* charheight
      i = i * 2
   endwhile
   q = (h-l)/i
   i = (h-l)/floor(q)
   res = settag(tag,TICKS_ALLOWED+SELECT_ALLOWED+TEXT_EFFECTS+DELETE_ALLOWED+REDRAW_NEEDED)
   res = group(w)
                       rem don't output a font inside a group!!!
   if p(GRGR) > 0 then
       res = lineattributes(w,1,p(LWGR),0xAAAAAA00,0)
       y=l+i
       lineoffset = xs
       while ( y <= h+i/2)
          if round(y,6) <> 0 or p(THICKNESSGR) <> 0 then
              d = y*a+b
                if p(THICKNESSGR) then
                    res = line(w,lineoffset,d, 0.33*p(THICKNESSGR),0.55*p(THICKNESSGR))
                    res = line(w,lineoffset+0.33*p(THICKNESSGR),d+0.55*p(THICKNESSGR),tl,0)
                 else   
                    res = line(w,lineoffset,y*a+b,tl,0)
                 endif
          endif
          y=y+i
        endwhile
   endif
   res = lineattributes(w,1,p(LWGR),yp(LCA),0)
   if yp(INA) = 0 then
       textoffset = xs-yp(HOTXA)+dpp
       lineoffset = xs+dpp
       tick = yp(FSA)/2
   endif
   if yp(INA) <> 0 then
       textoffset = xs+yp(FSA)+dpp
       lineoffset = xs +dpp
       tick = -yp(FSA)/2
   endif
   res = line(w,lineoffset,l*a+b,0,yp(LENGTHA))

   if yp(NSA) = 0 then
       y=l
       lineoffset = xs+dpp
       while(y <= h+i/2)
           if p(GRGR) = 0 then res=line(w,lineoffset,y*a+b,2*tick,0)
            d = y*a+b-charheight/2
            if round(y,6) <>0 or dpp = 0 then
                if y<>0 then
                    if abs(i/y) > 10 then y = 0
                endif
              res = string(w,textoffset,d,format(y,"%0.5g"),yp(TCA),p(BGGR))
           endif
            y=y+i
     endwhile

   
       if (yp(STA) > 1) then
          i=i/yp(STA)
          y=l
          while (y < h)
             if y <> l then  res = LINE (w,lineoffset,y*a+b,tick,0)
             y = y + i
          endwhile
       endif
       if has_top_label then
            il = p(OLGR)+p(IBGR)
            ir = p(PWGR)-p(ORGR)-p(IRGR)
            dump = yp(FSA)
           yp(FSA) = 1.5 * yp(FSA)
           i=CRM_extract(CRM_finddims(w,yp,p(YLABELS)),"x=")
           if lineoffset - i/2 < il then
               res=CRM_showlabel(w,yp,p(YLABELS),il,ys+yp(LENGTHA)+yp(FSA)+0.55*p(THICKNESSGR))
               else
                  if lineoffset +i/2 > ir then
                    res=CRM_showlabel(w,yp,p(YLABELS),ir-i,ys+yp(LENGTHA)+yp(FSA)+0.55*p(THICKNESSGR))
                  else
                    res=CRM_showlabel(w,yp,p(YLABELS),lineoffset-i/2,ys+yp(LENGTHA)+yp(FSA)+0.55*p(THICKNESSGR))
                  endif
           endif
           yp(FSA) = dump
       endif
   endif
   res = endgroup(w)
   =1
endmacro


rem : **********************************************************************
rem : This function draws a logarithmic vertical axis as specified in
rem : p and yp, with the hot-spot located as xs,ys.
rem : tl is set to the length of the grid-lines needed and tag is
rem : the drawing tag
 
macro CRM_draw_verlog_axis(w,xs,dpp,ys,yp(),p(),tl,tag,has_top_label)

    local z,a,b,l,h,i,res,y,alog,q,d,tick,dump
    local charheight, lineoffset,textoffset
    res = lineattributes(w,1,p(LWGR),yp(LCA),0)
    res = font(w,yp(FOA),yp(FSA),yp(FSA)*yp(FAA)/100)
    charheight = textheight(w,"1")
    rem scalelength = yp(HEIGHTA)-charheight - has_top_label*2.5*yp(FSA)
    
    z = CRM_log_scale(yp(LOA),yp(UPA),yp(LENGTHA))
    a=CRM_extract(z,"a=")
    rem b=CRM_extract(z,"b=")+ys+ charheight/2
    b = CRM_extract(z,"b=")+ys
    yp(AA) = a
    yp(BA) = b
    l=log(CRM_extract(z,"l="))
    h=log(CRM_extract(z,"h="))
    i=CRM_extract(z,"i=")
    res = settag(tag,TICKS_ALLOWED+SELECT_ALLOWED+TEXT_EFFECTS+DELETE_ALLOWED+REDRAW_NEEDED)
    res = group(w)
    rem don't output a font inside a group!!!
    
    lineoffset = xs+dpp
    if yp(INA) = 0 then
        textoffset = xs- yp(WIDTHA)
        tick = yp(FSA)/2
    endif
    if yp(INA) <> 0 then
        textoffset = xs + yp(FSA)
        tick = -yp(FSA)/2
    endif
    res = line(w,lineoffset,ys,0,yp(LENGTHA))
if yp(NSA) = 0 then
        y=l
        while(y <= h)
            alog = CRM_antilog(y)
            d = a*y+b
            res=string(w,textoffset,d-charheight/2,format(alog,"%0.5g"),yp(TCA),p(BGGR))   
            res=line(w,lineoffset,d,tick*2,0)
            y=y+1
        endwhile
        if p(GRGR) > 0 then
           res = lineattributes(w,1,p(LWGR),0xAAAAAA00,0)
           y=l+1
           lineoffset = xs
           while ( y <= h+0.5)
                d = a*y+b
                if p(THICKNESSGR) then
                  res = line(w,lineoffset,d, 0.33*p(THICKNESSGR),0.55*p(THICKNESSGR))
                  res = line(w,lineoffset+0.33*p(THICKNESSGR),d+0.55*p(THICKNESSGR),tl,0)
                else   
                    if y > l then res = line(w,lineoffset,d,tl,0)
                endif    
                y=y+1
           endwhile
        res = lineattributes(w,1,p(LWGR),yp(LCA),0)
        endif
        y=l
        if (yp(STA) > 0) then
        while (y < h)
           for q = 2 to 10
             d = log(q)+y
             res = LINE (w,lineoffset,a*d+b,tick,0)
           next q
           y = y + 1
        endwhile
        endif
        if has_top_label then
           dump = yp(FSA)
           yp(FSA) = 1.5 * yp(FSA)
           res =CRM_showlabel(w,yp,p(YLABELS),p(OLGR)+p(ILGR), ys+yp(LENGTHA)+yp(FSA))
           yp(FSA) = dump
        endif
    endif
    res = endgroup(w)
    =1
endmacro

rem : ****************************************************************
rem : plan_horizontal_histo_axis:  This function planss the horizontal
rem : axis for a vertical histogram.  The parameters are: 
rem :       w     : window
rem :       xaxis : The xaxis descriptor
rem :       p     : The graph descriptor
rem :       data  : The data block . Labels are in data(0,0) to data(p(ROWS)-1,0)
rem : The function is entered with the a provisional width of the axis in xaxis(WIDTHA).
rem : It establishes the size of the axis and the relative position of the hotspot.

macro CRM_plan_horizontal_histo_axis(w,xaxis(),p(),data())
   local n,res,in,aa,b,x,xd, maxheight, hh,zz,format
   n = p(ROWS)
   res = font(w,xaxis(FOA),xaxis(FSA),xaxis(FSA)*xaxis(FAA)/100)
           rem : Next survey labels and find the maximum height
   maxheight = 0
   x=0
   xaxis(FORMATA)  = 0
   while x < n
      if textwidth(w,data(x,0)) > xaxis(WIDTHA)/(p(ROWS)) then 
         xaxis(FORMATA)=1
      endif

      hh = textheight(w,data(x,0)) 
      if hh > maxheight then maxheight = hh
      x=x+1
   endwhile
   maxheight = maxheight
   if xaxis(FORMATA) = 1 then maxheight = maxheight * 2.5
   if len(p(XLABEL)) > 0 then maxheight = maxheight + 3*xaxis(FSA)
   xaxis(HEIGHTA) = maxheight+2*xaxis(FSA)
   xaxis(HOTXA) = 0
   xaxis(HOTYA) = xaxis(HEIGHTA)
   xaxis(LENGTHA) = xaxis(WIDTHA) - 0.33*p(TDGR)*p(THICKNESSGR)
   =0
endmacro


rem : *********************************************************
rem : Function draw_horizontal_histo_axis generates a labelled axis suitable
rem : for the display of a DISCRETE variable, such as a histogram. The parameters are:
rem :    w :   a window
rem :    xs,ys : position of the hotspot
rem :    axis  : A set of axis parameters
rem :    p     : A grraph parameter block
rem :    data  : The data block
rem :    tag   : The tage used for labelling the compound object
rem :    tl    : The length of grid-lines
macro CRM_draw_horizontal_histo_axis(w,xs,ys,xaxis(),p(),data(),tag,tl)
   local fs,tc,lc,fo,res,in,aa,b,x,xd, maxheight, hh,zz,format,dump
   local textheight
                                               rem : Set up initial conditions
   res = settag(tag,SELECT_ALLOWED+TEXT_EFFECTS+DELETE_ALLOWED+REDRAW_NEEDED)
   res = group(w)
   aa = xaxis(WIDTHA) / p(ROWS)
   ys=ys-xaxis(HOTYA)
   xs=xs-xaxis(HOTXA)
   b= xs
             rem : Next survey labels and find the maximum height
   maxheight =  xaxis(HOTYA)
   res = lineattributes(w,1,p(LWGR),0,0)
   res = line(w,xs,ys+maxheight,xaxis(WIDTHA),0)
   if xaxis(NSA) = 0 then
       textheight = ys + (len(p(XLABEL))>0)*3*xaxis(FSA)+1.5*xaxis(FSA)
        if p(GRGR) > 0 then
           res = lineattributes(w,1,p(LWGR),0xAAAAAA00,0)
           x=0
           while ( x <= p(ROWS))
              if p(THICKNESSGR) = 0 and x<>0 then
                  res = line(w,x*aa+b,ys+maxheight,0,tl)
              else
                  res = line(w,x*aa+b,ys+maxheight,0.33*p(THICKNESSGR),0.55*p(THICKNESSGR))
                  res = line(w,x*aa+b+0.33*p(THICKNESSGR),ys+maxheight+0.55*p(THICKNESSGR),0,tl)
              endif        
              x=x+1
           endwhile
           res = lineattributes(w,1,p(LWGR),xaxis(LCA),0)
        endif
       x=0
       while(x < p(ROWS))
             xd = aa*x+b
             hh =  how_many_lines(w,data(x,0),xaxis,aa)
            res  = displaytext(w,data(x,0),xaxis,xd,textheight,aa,hh,xaxis(FORMATA),p(BGGR))
             res = line( w,xd,ys+maxheight,0,xaxis(FSA))
             x=x+1
       endwhile
       res = line(w,aa*p(ROWS)+b,ys+maxheight,0,xaxis(FSA))
    
    
                                      rem output centred label
       if len(p(XLABEL)) > 0 then
          dump = xaxis(FSA)
          xaxis(FSA) = 1.5 * xaxis(FSA)
          res = CRM_extract(CRM_finddims(w,xaxis,p(XLABEL)),"x=")
          res =CRM_showlabel(w,xaxis,p(XLABEL),xs+(xaxis(WIDTHA)-res)/2, ys+xaxis(FSA))
          xaxis(FSA) = dump
       endif
   endif 
   res = endgroup(w)
   =0
endmacro

rem : ****************************************************************
rem : plan_vertical_histo_axis:  This function plans the vertical
rem : axis for a horizontal histogram.  The parameters are: 
rem :       w     : window
rem :       yaxis : The yaxis descriptor
rem :       p     : The graph descriptor
rem :       data  : The data block . Labels are in data(0,0) to data(p(ROWS)-1,0)
rem : The function is entered with the a provisional height of the axis in yaxis(HEIGHTA).
rem : It establishes the size of the axis and the relative position of the hotspot.

macro CRM_plan_vertical_histo_axis(w,yaxis(),p(),data())
   local n,res,in,aa,b,x,xd, maxlength, hh,zz,format
   n = p(ROWS)
   res = font(w,yaxis(FOA),yaxis(FSA),yaxis(FSA)*yaxis(FAA)/100)
           rem : Next survey labels and find the maximum length
   maxlength = 0
   x=0
   yaxis(FORMATA)  = 0
   while x < n
      zz = textwidth(w,data(x,0))
        if(zz > maxlength) then maxlength = zz 
      x=x+1
   endwhile
   yaxis(WIDTHA) = maxlength + 2*yaxis(FSA)
  
   yaxis(HOTXA) = yaxis(WIDTHA)
   yaxis(HOTYA) = 0
   yaxis(LENGTHA) = yaxis(HEIGHTA) - 0.33*p(TDGR)*p(THICKNESSGR)
   if len(p(XLABEL)) > 0 then yaxis(LENGTHA) = yaxis(LENGTHA)-7*yaxis(FSA) 
   =0
endmacro


rem : *********************************************************
rem : Function draw_vertical_histo_axis generates a vertical labelled axis
rem : suitable for the display of a DISCRETE variable, such as a histogram.
rem : The parameters are:
rem :    w :   a window
rem :    xs,ys : position of the hotspot
rem :    axis  : A set of axis parameters
rem :    p     : A graph parameter block
rem :    data  : The data block
rem :    tag   : The tage used for labelling the compound object
rem :    tl    : The length of grid-lines
macro CRM_draw_vertical_histo_axis(w,xs,ys,yaxis(),p(),data(),tag,tl)
   local fs,tc,lc,fo,res,in,aa,b,x,xd, maxheight, hh,zz,format,dump
   local textplace
                                               rem : Set up initial conditions
   res = settag(tag,SELECT_ALLOWED+TEXT_EFFECTS+DELETE_ALLOWED+REDRAW_NEEDED)
   res = group(w)
   aa = yaxis(LENGTHA) / p(ROWS)
   ys=ys-yaxis(HOTYA)
   xs=xs-yaxis(HOTXA)
   b= ys
   res = lineattributes(w,1,p(LWGR),0,0)            
   res = line(w,xs+yaxis(HOTXA),ys,0,yaxis(LENGTHA))
   if yaxis(NSA) = 0 then
       textplace = xs + yaxis(FSA)
        if p(GRGR) > 0 then
           res = lineattributes(w,1,p(LWGR),0xAAAAAA00,0)
           x=0
           while ( x <= p(ROWS))
              if (p(THICKNESSGR) = 0)  then
                  res = line(w,xs+yaxis(HOTXA),b+aa*x,tl,0)
              else
                  res = line(w,xs+yaxis(HOTXA),b+aa*x,0.33*p(THICKNESSGR),0.55*p(THICKNESSGR))
                  res = line(w,xs+yaxis(HOTXA)+0.33*p(THICKNESSGR),b+aa*x+0.55*p(THICKNESSGR),tl,0)
              endif        
              x=x+1
           endwhile
           res = lineattributes(w,1,p(LWGR),yaxis(LCA),0)
        endif
       x=0
       res = font(w,yaxis(FOGR),yaxis(FSGR),yaxis(FSGR)*yaxis(FAGR)/100)
       while(x < p(ROWS))
             xd = aa*x+b
             res = string( w,textplace,b+aa*(x+0.5)-0.5*yaxis(FSA),data(p(ROWS)-1-x,0),yaxis(TCGR),p(BGGR))
             res = line( w,xs+yaxis(HOTXA),xd,yaxis(FSA),0)
             x=x+1
       endwhile
       res = line(w,xs+yaxis(HOTXA),aa*p(ROWS)+b,yaxis(FSA),0)
    
    
                                      rem output centred label
       if len(p(XLABEL)) > 0 then
          dump = yaxis(FSA)
          yaxis(FSA) = 1.5 * yaxis(FSA)
          res = CRM_extract(CRM_finddims(w,yaxis,p(XLABEL)),"y=")
          res =CRM_showlabel(w,yaxis,p(XLABEL),xs,aa*p(ROWS)+b+yaxis(FSA)+p(THICKNESSGR)*0.55)
          yaxis(FSA) = dump
       endif
   endif
   res = endgroup(w)
   =0
endmacro



