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 DISTRIBUTION,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 : This macro takes the maxumum count in any row or
rem : column and returns a scaling factor. For the interval
rem : 1-10 it is 1.
rem : for 10-20 it is 2. 21 to 40, 4; and 41 to 100, 10.
rem : for higher powers of 10 it is pro rata.

macro CRM_vertical_scale (x)
   local p,q
   if (x <=10) then =1
   p = 1
   while (x > 100)
      x = x/10
      p = 10*p
   endwhile
   if x <= 20 then =2*p
   if x <= 40 then = 5*p
   = 10*p
endmacro


 
macro CRM_plot_vertical_picto (w,data(),p(),xaxis(),yaxis(),di(),tag,xm,ym,base,vs)
   local res,n,j,color,xa,xb,ya,yb,h_interval,col_width,start,m,q,g
   local grlist(30)
   n = p(ROWS)
   g = fetchgraph(p(PIGR))
   if iserror(g) then ="Error in fetchgraph"
   for j = 0 to n-1
     grlist(j) = fetchgraph(data(j,0))
     if iserror(grlist(j))  then  grlist(j) = g
    next j
   h_interval = xaxis(WIDTHA) / n
   for j = 0 to n-1
      start = xm + (j+0.2)*h_interval
      m = 1
      while m <= data(j,1)/vs +0.5     
         yb = yaxis(BA) + yaxis(AA) * (m-0.8)*vs
         res = scaledpicture(w,grlist(j),start,yb,h_interval*0.7,yaxis(AA)*0.8*vs)
         m=m+1
      endwhile
   next j   
  =0
endmacro
  



rem : *******************************************************************
rem : this macro is called when the data for the vertical pictogram 
rem : has been arranged in a regular way in array data. Only the first
rem : two columns are used

macro CRM_picto_plot(data(),s,p(),v)

   local xaxis(PAXIS),yaxis(PAXIS),di(PDATA),kk(MDATA)
   local miny,maxy,flag
   local x_axis_height,y_axis_width
   local x_tag,y_tag,gr_tag,le_tag,base
   local j,k,res,w,hor_label_dims,ver_label_dims,q
   local data_up,data_right,data_height,data_width
   local dsc,dsp,vs
   local has_legend
   local has_top_label
   local xsm_dump,ysm_dump
               rem find max and min values in both axes
  
   has_top_label = len(p(YLABELS)) > 0
   gr_tag = psexsub(v)
   has_legend = 0
                  rem open window
   res = settag(gr_tag, LINE_EFFECTS+TEXT_EFFECTS+COLOURS_ALLOWED+GRAPH_COLOUR_ALLOWED+DIMENSIONS_ALLOWED)
   w = gstart(p(PWGR),p(PHGR))
                  rem find data minima and maxima
      miny = data(0,1)
      maxy = data(0,1)
      for j = 0 to p(ROWS)-1
            if data(j,1) > maxy then maxy = data(j,1)
            if data(j,1) < miny then miny = data(j,1)
      next j
      maxy = 1.1*maxy
      vs = CRM_vertical_scale(maxy)
      if (vs > 1) then
         if has_top_label then
            p(YLABELS) = p(YLABELS) cat "   One symbol stands for "cat vs cat " units."
         else
            has_top_label = 1
            p(YLABELS) = "  One symbol stands for " cat vs cat " units."
         endif 
      endif

      if miny = maxy then ="Bad vertical axis"
      if maxy > 0 and miny > 0 then 
          miny = 0
          base = 0
      endif
                             rem   Now extract x-axis data
   res = psstep(v)
   if iserror(res) then = "psstep failed"
   if res<> CRM_code("B") then = "Missing B() Parameter"
   x_tag = psexsub(v)
   res = CRM_get_ax_params(p,xaxis,v)
                rem   Now extract y-axis data
   res = psstep(v)
   if iserror(res) then = "psstep failed"
   if res<> CRM_code("C") then = "Missing C() Parameter"
   y_tag = psexsub(v)
   res = CRM_get_ax_params(p,yaxis,v)
                                                     rem set limits
   if yaxis(LOA) = (-1) or yaxis(LOA) > miny then yaxis(LOA) = miny
   if yaxis(UPA) = (-1) or yaxis(UPA) < maxy then yaxis(UPA) = maxy

       kk(XEM) = 0
       kk(YEM) = 0

                rem now plan layout using dummy width for x-axis and
                rem dummy height for y-axis

   xaxis(WIDTHA) = p(PWGR)-p(OLGR)-p(ILGR)-p(IRGR)-p(ORGR)-200 - kk(XEM)
   yaxis(HEIGHTA) = p(PHGR)-p(OTGR)-p(ITGR)-p(IBGR)-p(OBGR)-200
      res = CRM_plan_horizontal_histo_axis(w,xaxis,p,data)
   if type(res)=3 then = "Plan horizontal axis failed"
   res = CRM_plan_vertical_axis(w,yaxis,p) 
   if type(res) = 3 then ="Plan vertical axis failed"
                           rem now compute sheet geography correctly
                           rem get true values for width and height
   xaxis(WIDTHA) = p(PWGR)-p(OLGR)-p(ILGR)-p(IRGR)-p(ORGR)-yaxis(WIDTHA)-kk(XEM)
   yaxis(HEIGHTA) = p(PHGR)-p(OTGR)-p(ITGR)-p(IBGR)-p(OBGR)-xaxis(HEIGHTA)
   data_right = yaxis(WIDTHA)+p(OLGR)+p(ILGR)+xaxis(HOTXA)
   data_up = xaxis(HEIGHTA)+p(OBGR)+p(IBGR)+yaxis(HOTYA)
   data_width = p(PWGR) -p(OLGR)-p(ILGR)-p(IRGR)-p(ORGR)-data_right-0.33*p(THICKNESSGR)
   if kk(XEM) > 0 then data_width = data_width -20 -kk(XEM)
   data_height = p(PHGR) -p(OTGR)-p(ITGR)-p(IBGR)-p(OBGR)- data_up
                            rem fill sheet with white
   yaxis(HEIGHTA) = p(PHGR)-p(OTGR)-p(ITGR)-p(IBGR)-p(OBGR) - xaxis(HEIGHTA)
   xaxis(WIDTHA) = data_width
   res = CRM_plan_vertical_axis(w,yaxis,p) 
   if type(res) = 3 then =res
   res = box(w,0,0,p(PWGR),p(PHGR),p(MAGR))
                            rem draw background if not white
   if p(BGGR) <> p(MAGR) then
       res = box(w,p(OLGR),p(OBGR),p(PWGR)-p(OLGR)-p(ORGR),p(PHGR)-p(OBGR)-p(OTGR),p(BGGR))
   endif
 
   p(THICKNESSGR) = 0
   res = CRM_plan_horizontal_histo_axis(w,xaxis,p,data)
   if type(res) = 3 then =res
   res = CRM_draw_horizontal_histo_axis(w, data_right,data_up,xaxis,p,data,x_tag,yaxis(LENGTHA))
   if type(res) = 3 then =res
   res = CRM_draw_vertical_axis(w,data_right,0,data_up,yaxis,p,xaxis(WIDTHA),y_tag,has_top_label)
   if type(res) = 3 then =res
   base = yaxis(BA)-data_up        
   res = CRM_get_di_default_params(p,di,dsc)
   res = CRM_plot_vertical_picto(w,data,p,xaxis,yaxis,di,res,data_right,data_up,base,vs)
   res = CRM_close_string(v)
   res = CRM_tail_end(w,s,p)
   if res then =res
   =w                 
endmacro


rem : *********************************************************
rem : This function plots a vertical pictogram
macro pictogram (a(),s)
graphmacro "Pictogram",0x1AF5," "
   local v,q,n,res,h,w
   local p(PSIZE)

   v = psregister(s)
   q = psstep(v)
   if iserror(q) then = "Error in pstep"
   if q<> CRM_code("A") then = "Missing parameter substring"

   res = CRM_get_gr_params(p,v)
   h = first(a)
   w = second(a)
   p(GRAPHTYPE)=PICTOGRAM
   n = CRM_datasets(v,w,h)
   if pscontains(v,"u") then
       res = CRM_point_plot_row_data(a,s,v,p,w,n,1)
   else
      res = CRM_point_plot_col_data(a,s,v,p,h,n,1)
   endif
   
   =res
endmacro
