c
c   Copyright (c) 1990 by Dwight Meglan
c
      subroutine DoStripChart( CMND,MACRO,ratio,error )
      implicit none

      include 'tel_cons.inc'
      include 'tel_strc.inc'
      include 'tel_file.inc'
      include 'tel_chrt.inc'
      include 'tel_grph.inc'
      include 'tel_name.inc'

      include 'vogle.fi'

      record /CmndLine/    CMND
      record /MacroStuff/  MACRO
                              
      real*4    ratio
      logical*4 error        
                                                 
      real*4    VpWidth,VpHeight,GrPerRow,VpYMin,VpYMax,VpXMin,VpXMax,
     &          ChartHt,ChartWd,Xmin,Xmax
      integer*4 i,j,NumChart,FileNum,size,key
      character line*80,junk*1
      logical*4 SameXData,Used_ELC_DT,Used_MTN_DT

      save NumChart,FileNum
c                       
c     Check for special case of redisplay of previous Chart
c
      if ( CMND.CmndText(2) .eq. 'PRE' ) then
        if ( GD(1).NumPlts .gt. 0 ) then
          goto 10
        else
          goto 1030 ! cannot redisplay previous graph 
        end if
      end if
c                            
c     Check for options
c                         
      SameXData= .true.                  
      NumChart= 1
      if ( CMND.NumOpt(1) .gt. 0 ) then
        do i= 1, CMND.NumOpt(1)
          if ( (CMND.OptIntVal(i,1) .gt. 1) .and. 
     &         (CMND.OptIntVal(i,1) .le. MAX_CHART) ) then
            NumChart= CMND.OptIntVal(i,1)
            if ( NumChart .gt. MAX_CHART ) goto 1000 ! error, too many charts
          else
            goto 1010 ! error, bad option
          end if
        end do
      end if ! Options present
c                                               
c     Interpret parameters of each graph and set up data structure for each
c                       
      if ( CMND.NumCmnd .lt. 3 ) then ! prompt for first chart
        if ( MACRO.On ) then
          line= MACRO.Text(MACRO.LineNum,MACRO.MacroNum)
          MACRO.LineNum= MACRO.LineNum + 1
        else
          write(6,100) 1
100       format(' chart ',i2,'> ',$)
          read(5,110,end=999) line ! CntrlZ kicks out of subroutine
110       format(a80)
        end if
      else ! remove first entry
        do i= 1, 80
          line(i:i)= ' '
        end do
        line= CMND.Text(1)(CMND.CmndStart(2):80)
      end if         
      call ParseLine( line,CMND )
      i= 1                                  
      error= .false.                                 
      do while (i .le. NumChart )
        if ( error .and. MACRO.On ) goto 1020 ! error
        if ( (i .gt. 1) .or. error ) then
          if ( MACRO.On .and. 
     &        (MACRO.LineNum .le. MACRO.NumLines(MACRO.MacroNum)) ) then
            line= MACRO.Text(MACRO.LineNum,MACRO.MacroNum)
            MACRO.LineNum= MACRO.LineNum + 1
          else
            write(6,100) i
            read(5,110,end=999)  line ! CntrlZ kicks out of subroutine
          end if
          call ParseLine( line,CMND )                                
        end if             
        if ( CMND.NumCmnd .gt. 2 ) then
          call AssignPlotData( CMND,i,SameXData,
     &                         GD(i).NumPlts,GD(i).Min,GD(i).Max,error )
          if ( .not. error ) then
            GD(i).SameFile= .true.
            FileNum= PD(1,i).FileNum(y)
            do j= 2, GD(i).NumPlts
              if ( PD(j,i).FileNum(y) .ne. FileNum ) GD(i).SameFile= .false.
            end do
c            if ( .not. GD(i).SameFile ) goto 1040 ! error
            if ( GROPT(i).GrLabelFlg ) then
              if ( GROPT(i).ManGrLabelFlg ) then
                GD(i).GrLabel= GROPT(i).ManGrLabel
              else
                GD(i).GrLabel= FD(FileNum).GP.Name
              end if
            end if
            i= i + 1
          end if
        else              
          write(6,120)         
120       format(' Error- Not enough parameters.  Try chart again.')
          error= .true.
        end if            
      end do ! while
c
c     Check that all x axis chart data is TIME DATA and that all data in all
c     charts is from the same file
c
      FileNum= PD(1,1).FileNum(x)
      do i= 1, NumChart
c        do j= x, y
c          if ( PD(1,i).FileNum(j) .ne. FileNum ) goto 1040 ! error
c        end do ! j
        do j= 1, GD(i).NumPlts
          if ( PD(j,i).DataType(x) .ne. TIM_DT ) goto 1050 ! error
        end do
      end do ! i
c          
c     Check for special form of schart having the following conditions:
c       - All graphs contain either emg, force or motion data only
c       - Force Alignment must have been carried in ANZ
c       - Data in ANZ file must NOT have been clipped in ANZ
c       - Cycle clipping option must be turned off
c     This combination of conditions allows an schart having all the electrical
c     data (emg and force plate) to be displayed with only a section of the 
c     motion data corresponding to the gait cycle sorted to be displayed also.
c
      if ( FD(FileNum).FF.Aligned .and.
     &     (.not. FD(FileNum).FF.DataClipped ) .and.
     &     (.not. GROPT(1).CycleClip)               ) then
        Used_ELC_DT= .false.
        Used_MTN_DT= .false.
        do i= 1, NumChart
          do j= 1, GD(i).NumPlts
            if ( (PD(j,i).datatype(y) .eq. ELC_DT) .or.
     &           (PD(j,i).datatype(y) .eq. FRC_DT) .or.
     &           (PD(j,i).datatype(y) .eq. EMG_DT)      ) Used_ELC_DT= .true.
            if ( (PD(j,i).datatype(y) .eq. MTN_DT) .or.
     &           (PD(j,i).datatype(y) .eq. MRK_DT) .or.
     &           (PD(j,i).datatype(y) .eq. SEG_DT) .or.
     &           (PD(j,i).datatype(y) .eq. JNT_DT) .or.
     &           (PD(j,i).datatype(y) .eq. REC_DT)      ) Used_MTN_DT= .true.
          end do
        end do
        if ( Used_ELC_DT .and. Used_MTN_DT ) then
          write(6,300)
300       format(//' NOTE- Special SCHART form is being used...'/)
          call ModifySchart(NumChart)
        end if
      end if
c                        
c     All chart data has been successfully assigned to graph structure.
c     Now display it.                       
c                                                   
c       Set up the ViewPort and draw the outlines and gait events
c                                                      

 10   call clear

      if ( CHOPT.DisplayLabel .and. (.not. CHOPT.ManLabel) )
     &  CHOPT.ChartLabel= FD(PD(1,1).FileNum(x)).GP.name
c            
c     Now draw each individual chart
c
      if ( CHOPT.Screen ) then
         call TurnScreenOn(ratio)

         ChartHt= 0.88/float(NumChart)
         ChartWd= 0.85*ratio
         VpXmin= -1.0 + 0.1
         VpXmax= VpXmin + 2.0*ChartHt
         VpYmin= -1.0*ratio + 0.04*ratio
         VpYmax= VpYmin + 2.0*ChartWd

         call DrawChartOutline( NumChart,ratio,Xmin,Xmax )
         do i= 1, NumChart
            call DrawChart( i,VpXmin,VpXmax,VpYmin,VpYmax,ratio,
     &                      Xmin,Xmax,ChartHt,ChartWd,NumChart   )
            call vflush
            VpXmin= VpXmax
            VpXmax= VpXmin + 2.0*ChartHt
         end do
         call vflush
         key= getkey()
      endif

      if ( CHOPT.Print ) then
         call clear()
         write(6,99)
 99      format(' Writing stripchart to file...',$)

         call TurnPrinterOn(ratio)

         ChartHt= 0.88/float(NumChart)
         ChartWd= 0.85*ratio
         VpXmin= -1.0 + 0.1
         VpXmax= VpXmin + 2.0*ChartHt
         VpYmin= -1.0*ratio + 0.04*ratio
         VpYmax= VpYmin + 2.0*ChartWd

         call DrawChartOutline( NumChart,ratio,Xmin,Xmax )
         do i= 1, NumChart
            call DrawChart( i,VpXmin,VpXmax,VpYmin,VpYmax,ratio,
     &                      Xmin,Xmax,ChartHt,ChartWd,NumChart   )
            call vflush
            VpXmin= VpXmax
            VpXmax= VpXmin + 2.0*ChartHt
         end do
         call vflush()
         call clear()
         call TurnScreenOn(ratio)
         write(6,98)
 98      format(' Done.')
      else
         call clear()
      endif



999   return     
 
1000  write(6,1005) MAX_CHART
1005  format(' Error- Too many charts indicated in options'/
     &       '        Max of ',i2,' allowed')
      return

1010  write(6,1015) MAX_CHART
1015  format(' Error- Invalid option for StripCHart command'/
     &       '        Valid options are: 1-',i2,' (# of charts)')
      return

1020  write(6,1025)
1025  format(' Error- Problem in creating chart during macro execution')
      return

1030  write(6,1035)
1035  format(' Error- Cannot redisplay previous chart because no chart'/
     &       '        has been defined yet.')
      return

1040  write(6,1045)
1045  format(' Error- All chart data must come from the same file')
      return

1050  write(6,1055)
1055  format(' Error- All chart x axis data must be the same and be only'/
     &       '        time data : FRAme TIMe PERcent')
      return
      end                                 
c
c   Copyright (c) 1990 by Dwight Meglan
c
      SUBROUTINE ModifySchart(nc)
      implicit none

      include 'tel_cons.inc'
      include 'tel_strc.inc'
      include 'tel_file.inc'
      include 'tel_chrt.inc'
      include 'tel_grph.inc'
      include 'tel_name.inc'

      integer*4 nc

      real*4    tempdata(MAX_PLOT_SIZE),del
      integer*4 fn,i,j,k,kk,frmHS,NumElcFrm,NewNumMtnFrm,NumBeginPadFrm,
     &          NumEndPadFrm,tempevent(MAX_PLOT_SIZE),sch_xd,plt_xd
      logical*4 tempdataok(MAX_PLOT_SIZE)

      fn= PD(1,1).FileNum(x)
c
c     Find the first set of electrical data (EMG or force plate) for use later
c     in reassigning the x data of the plots
c                            
      do i= 1, nc
        do j= 1, GD(i).NumPlts
          if ( (PD(j,i).datatype(y) .eq. ELC_DT) .or.
     &         (PD(j,i).datatype(y) .eq. FRC_DT) .or.
     &         (PD(j,i).datatype(y) .eq. EMG_DT)      ) then
            sch_xd= i
            plt_xd= j
            goto 100
          end if
        end do
      end do
100   do i= 1, nc
c
c       Compute the number of frames that must be padded to the beginning and
c       end of the plot data sets that are of the motion data type.
c
        j= 1                      
        do while ( (PltEvent(j,y,1,i) .eq. 0) .and. (j .lt. PD(1,i).Numpnts) )
          j= j + 1
        end do
        frmHS= j
        if ( frmHS .eq. PD(1,i).Numpnts ) frmHS= 1
        NumElcFrm= FD(fn).FP.num_frm
        NewNumMtnFrm= NumElcFrm/FD(fn).FP.av_frm_ratio
        NumBeginPadFrm= FD(fn).FP.frm_offset/FD(fn).FP.av_frm_ratio - frmHS + 1
        NumEndPadFrm= NewNumMtnFrm - NumBeginPadFrm - PD(1,i).Numpnts
c
c       Pad those plot data sets which are motion data
c
        do j= 1, GD(i).NumPlts
c
c         Pad and move y data
c
          if ( (PD(j,i).datatype(y) .eq. MTN_DT) .or.
     &         (PD(j,i).datatype(y) .eq. MRK_DT) .or.
     &         (PD(j,i).datatype(y) .eq. SEG_DT) .or.
     &         (PD(j,i).datatype(y) .eq. JNT_DT) .or.
     &         (PD(j,i).datatype(y) .eq. REC_DT)      ) then
            do k= 1, PD(j,i).NumPnts
              tempdata(k)=   PltData(k,y,j,i)
              tempdataok(k)= PltDataOK(k,j,i)
              tempevent(k)=  PltEvent(k,y,j,i)
            end do
            do k= 1, NumBeginPadFrm
              PltData(k,y,j,i)=   0.0
              PltDataOK(k,j,i)= .false.
              PltEvent(k,y,j,i)=  0
            end do
            do k= 1, PD(j,i).NumPnts
              kk= NumBeginPadFrm + k
              PltData(kk,y,j,i)=   tempdata(k)
              PltDataOK(kk,j,i)= tempdataok(k)
              PltEvent(kk,y,j,i)=  tempevent(k)
            end do
            do k= 1, NumEndPadFrm
              kk= NumBeginPadFrm + PD(j,i).NumPnts + k
              PltData(kk,y,j,i)=   0.0
              PltDataOK(kk,j,i)= .false.
              PltEvent(kk,y,j,i)=  0
            end do                                   
c
c           Reassign x data also
c
            PD(j,i).NumPnts= NewNumMtnFrm
            del= (PltData(PD(plt_xd,sch_xd).NumPnts,x,plt_xd,sch_xd)-
     &            PltData(1,x,plt_xd,sch_xd))/(NewNumMtnFrm-1)
            do k= 1, NewNumMtnFrm
              PltData(k,x,j,i)=   del*(k-1)
              PltEvent(k,x,j,i)=  PltEvent(k,y,j,i)
            end do
          end if
        end do ! j                                                 
      end do ! i
      return
      end
c                                                    
c   Copyright (c) 1990 by Dwight Meglan
c
      SUBROUTINE DrawChartOutline( NumChart,ratio,DataMin,DataMax )
      implicit none
                                                      
      include 'tel_cons.inc'
      include 'tel_strc.inc'
      include 'tel_chrt.inc'
      include 'tel_grph.inc'

      include 'vogle.fi'

      integer*4 NumChart
      real*4    VpXmin,VpXmax,VpYmin,VpYmax,ratio,DataMin,DataMax

      integer*4 x,y,i,j,numchar,NumEvent
      real*4    inc,chsize(2),length,xinc,xnum,xpos,extra,datadel,numdec,
     &          xp,yp
      logical*4 done
      character EventName(4)*3,AxisNum*7

      data x,y / 1,2 /
      data EventName / 'RHS','LHS','RTO','LTO' /

      call dviewport( -1.0,1.0,-ratio,ratio )      
      call ortho2( 0.0,1.0,0.0,ratio )
      call clipping(.true.)                                              
C
C       DRAW GRAPH BORDER
C                     
        if ( .not. CHOPT.UsePostPrint ) CALL COLOR(6)
        CALL MOVE2(0.05,0.02*ratio)
        CALL RDRAW2(0.88,0.0)
        CALL RDRAW2(0.0,0.85*ratio)
        CALL RDRAW2(-0.88,0.0)
        CALL RDRAW2(0.0,-0.85*ratio)
c
c       Draw the Chart Divisions
c
        inc= 0.88/float(NumChart)
        call move2( 0.05+inc,0.02*ratio )
        do i= 1, NumChart-1
          call rdraw2( 0.0,0.85*ratio )
          call rmove2( inc,-0.85*ratio )
        end do          
C
C       DRAW X AXIS TICK MARKS
C                             
        inc= (0.85*ratio)/float(CHOPT.NumXDiv)
        call move2( 0.93,0.02*ratio+inc)
        do i= 1, CHOPT.NumXDiv-1
          CALL RDRAW2(-0.01,0.0)
          CALL RMOVE2(0.01,inc)
        end do
        call move2( 0.05,0.02*ratio+inc)
        do i= 1, CHOPT.NumXDiv-1
          CALL RDRAW2(0.01,0.0)
          CALL RMOVE2(-0.01,inc)
        end do
C
C       DRAW chart TITLE AND SUBTITLE
C                       
        chsize(x)= 0.01875
        chsize(y)= 0.01875
        CALL DFONT(CHOPT.Font)
        call textsize(chsize(x),chsize(y))
        call textang(90.0)       
        call xcentertext()
        call ycentertext()
        if ( CHOPT.DisplayTitle ) then
          CALL MOVE2(0.01,0.445*ratio)
          CALL DSTRG(CHOPT.Title)
          CALL MOVE2(0.03,0.445*ratio)
          CALL DSTRG(CHOPT.SubTitle)
        end if       
c                                    
c       Draw Chart Name
c
        if ( CHOPT.DisplayLabel ) then
          call textsize(0.6*chsize(x),0.6*chsize(y))
          CALL MOVE2(0.99,0.02*ratio)
          CALL DSTRG(CHOPT.ChartLabel)
          call textsize(chsize(x),chsize(y))
        end if                   
c
c       Check the X axis data and scale it if requested
c
        if ( .not. CHOPT.ManXaxis ) then
          DataMin= GD(1).Min(x)
          DataMax= GD(1).Max(1)
          do i= 2, NumChart
            if ( GD(i).Max(x) .gt. DataMax ) DataMax= GD(i).Max(x)
            if ( GD(i).Min(x) .lt. DataMin ) DataMin= GD(i).Min(x)
          end do
          if ( abs(DataMax-DataMin) .lt. 1.0e-05 ) then
            if ( abs(DataMax) .lt. 1.0e-05 ) then
              DataMax= DataMax + 0.05
              DataMin= DataMin - 0.05
            else
              DataMax= 1.01*DataMax
              DataMin= 0.99*DataMin
            end if
          end if
          if ( CHOPT.SimpleAutoScale ) then
            call SimpleScaleData( DataMin,DataMax,CHOPT.NumXDiv )
          else
            call LooseLabel( DataMin,DataMax,CHOPT.NumXDiv,
     &                       DataDel,NumDec )
c           call ScaleData( DataMin,DataMax,CHOPT.NumXDiv )
          end if
        else
          DataMin= CHOPT.Xmin
          DataMax= CHOPT.Xmax
        end if
C                                       
C       Draw X AXIS SCALE     
C                                    
        xinc= (DataMax-DataMin)/float(CHOPT.NumXDiv)
        xp= 0.98 - 2.0*chsize(x)
        yp= 0.02*ratio
        CALL MOVE2(xp,yp)
        inc= (0.85*ratio)/float(CHOPT.NumXDiv)
        xnum= DataMin      
        call xcentertext()
        call ycentertext()
        do i= 1, CHOPT.NumXDiv+1                              
          call GenNumChar( xnum,axisnum )
          call dstrg(axisnum)
          xnum= xnum + xinc
          yp= yp + inc
          CALL MOVE2(xp,yp)
        end do
c                                    
c       Draw X axis label
c       
        call xcentertext()
        call ycentertext()
        CALL MOVE2(0.98,0.445*ratio)
        if ( .not. CHOPT.ManXaxis ) then
          CALL DSTRG( GD(1).Xtitle )
        else
          CALL DSTRG( CHOPT.XaxisLabel )
        end if                   
c
c       Draw the gait event lines and labels if requested
c       
        if ( CHOPT.EventOn ) then
          if ( .not. CHOPT.UsePostPrint ) call color(1)  
          call textsize( 0.5*chsize(x),0.5*chsize(y) )
          call xcentertext()
          call topjustify()
          CALL TEXTANG(90.0)
          NumEvent= 0
          extra= 0.0
          do i= 1, PD(1,1).NumPnts
            if ( PltEvent(i,y,1,1) .ne. 0 ) then
              NumEvent= NumEvent + 1
              if ( NumEvent .ge. 5 ) extra= -1.5*chsize(y)
              xpos= ratio*(0.02 +  (PltData(i,x,1,1)-DataMin)*
     &                             (0.85/(DataMax-DataMin))    )
              call move2( 0.05,xpos )
              call rdraw2( 0.88,0.0 )         
              call move2(0.07,xpos+0.6*chsize(y)+extra)
              call dstrg( EventName(PltEvent(i,y,1,1)) )
              call move2(0.93-2*chsize(x),xpos+0.6*chsize(y)+extra)
              call dstrg( EventName(PltEvent(i,y,1,1)) )
            end if
          end do ! i                   
        end if ! eventOn
      return
      end
C
C   Copyright (c) 1990 by Dwight Meglan                            
C   
      SUBROUTINE DrawChart( GrNum,VpXmin,VpXmax,VpYmin,VpYmax,ratio,
     &                      Xmin,Xmax,ChartHt,ChartWd,NumChart )
      implicit none    
                                                      
      include 'tel_cons.inc'
      include 'tel_strc.inc'
      include 'tel_grph.inc'
      include 'tel_chrt.inc'

      include 'vogle.fi'

      integer*4 GrNum,NumChart
      real*4    VpXmin,VpXmax,VpYmin,VpYmax,ratio,Xmin,Xmax,ChartHt,ChartWd
      
      integer*4 i,j,k,numchar,x,y
      REAL*4    LENGTH,inc,yinc,ynum,xl,yl,LgndPos(2,9),tempx,TempMax,
     &          DataMax(2),DataMin(2),DataDel(2),chsize(2),yedge,
     &          numdec(2),xp,yp
      CHARACTER AXISNUM*7 
      LOGICAL   DONE,skip

      data x,y / 1,2 /
      data LgndPos / 0.05,0.3, 0.3,0.3, 0.6,0.3,
     &               0.05,0.5, 0.3,0.5, 0.6,0.5,
     &               0.05,0.9, 0.3,0.9, 0.6,0.9 /
                  
c                                                    
c     First check the data that is to be plotted to make sure that it is
c     valid.  Invalid data occurs when all the values are the same in the
c     y direction.  If it is OK then either autoscale the y chart limit
c     or use the manual axis scales in the GROPT structure.
c       
      if ( .not. GROPT(GrNum).ManScale ) then
        if ( abs(GD(GrNum).Max(y)-GD(GrNum).Min(y)) .lt. 1.0e-05 ) then
          if ( abs(GD(GrNum).Max(y)) .lt. 1.0e-05 ) then
            DataMax(y)= GD(GrNum).Max(y) + 0.05         
            DataMin(y)= GD(GrNum).Min(y) - 0.05
          else
            DataMax(y)= 1.01*GD(GrNum).Max(y)
            DataMin(y)= 0.99*GD(GrNum).Min(y)           
          end if
        else                          
          DataMax(y)= GD(GrNum).Max(y)
          DataMin(y)= GD(GrNum).Min(y)
        end if
        if ( GROPT(GrNum).SimpleAutoScale ) then
          call SimpleScaleData( DataMin(y),DataMax(y),GROPT(GrNum).NumDiv(y) )
        else
          call LooseLabel( DataMin(y),DataMax(y),GROPT(GrNum).NumDiv(y),
     &                     DataDel(y),NumDec(y) )
c          call ScaleData( DataMin(y),DataMax(y),GROPT(GrNum).NumDiv(y) )
        end if
      else ! use manual scale limits
        if ( abs(GROPT(GrNum).ManMax(y)-GROPT(GrNum).ManMin(y)) .lt. 1.0e-5 ) then
          if ( abs(GROPT(GrNum).ManMax(y)) .lt. 1.0e-5 ) then
            DataMax(y)= GROPT(GrNum).ManMax(y) + 0.05
            DataMin(y)= GROPT(GrNum).ManMin(y) - 0.05
          else
            DataMax(y)= 1.01*GROPT(GrNum).ManMax(y)
            DataMin(i)= 0.99*GROPT(GrNum).ManMin(y)
          end if         
        else
          DataMax(y)= GROPT(GrNum).ManMax(y)
          DataMin(y)= GROPT(GrNum).ManMin(y)
        end if  
      end if    
      DataMax(x)= Xmax
      DataMin(x)= Xmin
      DataDel(x)= DataMax(x) - DataMin(x)
      DataDel(y)= DataMax(y) - DataMin(y)
      call dviewport( -1.0,1.0,-ratio,ratio )        
      call ortho2(0.0,1.0,0.0,ratio)
      call clipping(.true.)                                              
c                                                  
c       Draw chart label               
c
        if ( NumChart .gt. 5 ) then
          chsize(x)= 0.6*0.01875
          chsize(y)= 0.6*0.01875
        else
          chsize(y)= ChartHt/30.0
          chsize(x)= chsize(y) !/ratio
        end if
        call textsize(chsize(x),chsize(y))
        call ycentertext()
        call leftjustify()
         CALL TEXTANG(90.0)          
        if ( GROPT(GrNum).GrLabelFlg ) then
          if ( NumChart .lt. 5 ) call textsize(0.6*chsize(x),0.6*chsize(y))
          CALL MOVE2(0.05+GrNum*ChartHt-chsize(x),0.03*ratio)
          CALL DSTRG(GD(GrNum).GrLabel)
          if ( NumChart .lt. 5 ) call textsize(chsize(x),chsize(y))
        end if                   
C                                                    
C       Y axis Tick Marks
C
        inc= ChartHt/float(GROPT(GrNum).NumDiv(y))
        if ( .not. CHOPT.UsePostPrint ) CALL COLOR(6)
        CALL MOVE2(0.05+(GrNum-1)*ChartHt+inc,0.02*ratio+ChartWd)
        do i= 1, CHOPT.NumXDiv-1
          call rdraw2(0.0,-0.01)
          call rmove2(inc,0.01)
        end do
C
C       DRAW y axis numerals
C
        chsize(x)= (ratio - 0.02*ratio - ChartWd)/10.0
        chsize(y)= chsize(x)
        call textang(90.0)
        call textsize(chsize(x),chsize(y))
        call leftjustify()
        call ycentertext()
        if ( Abs(DataMin(y)) .gt. DataMax(y) ) then
          call GenNumChar( DataMin(y),axisnum )
        else
          call GenNumChar( DataMax(y),axisnum )
        end if
        yedge= 0.02*ratio + ChartWd + 0.5*chsize(x)
        if ( NumChart .lt. 5 ) then
          YINC= (DataMax(y)-DataMin(y))/float(GROPT(GrNum).NumDiv(y))
          YNUM= DataMax(y)
          xp= 0.05+(GrNum-1)*ChartHt
          yp= yedge
          CALL MOVE2(xp,yp)
          do i= 1, GROPT(GrNum).NumDiv(y)+1
            call GenNumChar( ynum,axisnum )
            if ( i .eq. 1 ) then ! top numeral, so lower it
              call rmove2(chsize(x),0)
              call dstrg(axisnum)
              call rmove2(inc-chsize(x),0)
            else if ( i .eq. GROPT(GrNum).NumDiv(y)+1 ) then ! last numeral, so
              call rmove2(-chsize(x),0)                    ! raise it
              call dstrg(axisnum)
            else              
              CALL DSTRG(AXISNUM)
            end if
            xp= xp + inc
            YNUM= YNUM-YINC
            call move2(xp,yp)
          end do        
        else ! draw only datamin(y) and datamax(y)
          xp= 0.05+(GrNum-0.5)*ChartHt
          yp= yedge
          CALL MOVE2(xp-ChartHt/4.0,yp)
          call GenNumChar( DataMax(y),axisnum )
          call dstrg(axisnum)
          CALL MOVE2(xp+ChartHt/4.0,yp)
          call GenNumChar( DataMin(y),axisnum )
          call dstrg(axisnum)
        end if
C                                         
C       DRAW Y AXIS LaBeL
C            
        call ycentertext()
        if ( NumChart .lt. 5 ) then
          call xcentertext()
          call textang(0.0)
          CALL MOVE2(0.05+(GrNum-0.5)*ChartHt,ratio-0.6*chsize(y))
          CALL DSTRG( GD(GrNum).Ytitle )
        else
          call textang(90.0)
          call rightjustify()
          CALL MOVE2( 0.05+(GrNum-1)*ChartHt+0.6*chsize(x),
     &                0.02*ratio+ChartWd-chsize(y)    )
          CALL DSTRG( GD(GrNum).Ytitle )
        end if
C                                                 
C     DRAW THE LEGEND LBLS       
C                 
        IF ( GROPT(GrNum).LgndFlg ) THEN                
          xl= 0.05+ (GrNum-LgndPos(y,GROPT(GrNum).LgndPosNum))*ChartHt
          yl= 0.02*ratio + LgndPos(x,GROPT(GrNum).LgndPosNum)*ChartWd
          call textang(90.0)
          call leftjustify()
          call ycentertext()
          CALL TEXTSIZE(0.75*chsize(x),0.75*chsize(y))
          do i= 1,GD(GrNum).NumPlts
            if ( .not. CHOPT.UsePostPrint ) CALL COLOR(I)
            call dlstyl(0)
            CALL dmark(xl,yl,chsize(x)/2.0,chsize(y)/2.0,i)           
            CALL dmark(xl,yl+0.0375*ChartWd,chsize(x)/2.0,chsize(y)/2.0,i)
            CALL dmark(xl,yl+0.075*ChartWd,chsize(x)/2.0,chsize(y)/2.0,i)
            CALL DLSTYL(I-1)       
            call move2(xl,yl)
            CALL RDRAW2(0.0,0.075*ChartWd)        
            CALL RMOVE2(0.0,0.025*ChartWd)
            if ( .not. CHOPT.UsePostPrint ) CALL COLOR(6)   
            CALL DSTRG( PD(i,GrNum).Lgnd(y) )
            xl= xl + chsize(x)
          end do
        end if               
C                                    
C     PLOT DATA POINTS
C                                                 
      call dviewport(VpXmin,VpXmax,VpYmin,VpYmax)
      TempMax= abs(DataMin(y)-DataMax(y))
*$ifdef DEBUG
      write(2,885) GrNum,0.0,TempMax,DataMin(x),DataMax(x)
 885  format(' Ortho2 for data in Schart Graph # ',i2/
     &       4(1x,f12.6))
*$endif
      call ortho2( 0.0,TempMax,DataMin(x),DataMax(x) )
      call clipping(.true.)
c
c     if option to draw x and/or y zero lines is set then do it
c             
        if ( GROPT(GrNum).XZeroLine .and.
     &       (DataMin(x) .lt. 0.0) .and. (DataMax(x) .gt. 0.0) ) then
          if ( .not. CHOPT.UsePostPrint ) call color(1)
          call dlstyl(0)
          call move2( 0.0,0.0 )
          call draw2( TempMax,0.0 )
        end if
        if ( GROPT(GrNum).YZeroLine .and.
     &       (DataMin(y) .lt. 0.0) .and. (DataMax(y) .gt. 0.0) ) then
          if ( .not. CHOPT.UsePostPrint ) call color(1)                                       
          call dlstyl(0) 
          tempx= abs(0.0-DataMax(y))
          call move2( tempx,DataMin(x) )
          call draw2( tempx,DataMax(x) )
        end if                 
c                                
c                                                  
        DO I= 1, GD(GrNum).NumPlts
          if ( .not. CHOPT.UsePostPrint ) CALL COLOR(I)
          k= 1
          do while ( (.not. PltDataOK(k,i,GrNum)) .and.
     &               (k .lt. PD(i,GrNum).NumPnts)      )
            k= k + 1
          end do
          tempx= abs(PltData(k,y,i,GrNum)-DataMax(y))
          CALL MOVE2( tempx,PltData(k,x,i,GrNum) )
          CALL DLSTYL(I-1)
          if ( .not. CHOPT.UsePostPrint ) CALL COLOR(I)
          DO j= k, PD(i,GrNum).NumPnts - 1
            tempx= abs(PltData(j,y,i,GrNum)-DataMax(y))
*$ifdef DEBUG
            write(2,886) GrNum,j,i,tempx,PltData(j,x,i,GrNum)
 886        format(1x,i2,1x,i4,1x,i4,1x,f12.6,1x,f21.6)
*$endif
            if ( PltDataOK(j,i,GrNum) ) then
              CALL DRAW2( tempx,PltData(j,x,i,GrNum) )
            else
              CALL MOVE2( tempx,PltData(j+1,x,i,GrNum) )
            end if
          END DO ! k
        END DO ! i
      RETURN   
      END
c
c   Copyright (c) 1990 by Dwight Meglan
c
      subroutine GenNumChar( Number,Text )
      implicit none

      real*4    Number                       
      character Text*7

      integer*4 i,j
      real*4    MAX_AXIS_VALUE,MIN_AXIS_VALUE

      data MAX_AXIS_VALUE,MIN_AXIS_VALUE / 99999.9,-9999.9 /

      if ( Number .gt. MAX_AXIS_VALUE ) then
        write(Text,100) MAX_AXIS_VALUE
      else if ( Number .lt. MIN_AXIS_VALUE ) then
        write(Text,100) MIN_AXIS_VALUE         
      else  
        if ( abs(Number) .lt. 1.0 ) then
          write(Text,101) Number
101       format(f7.2)       
        else
          WRITE(Text,100) Number
100       FORMAT(F7.1)         
        end if
      end if
      i= 1
      do while ( text(i:i) .eq. ' ' .and. i .le. 7 )
         i= i + 1
      end do
      if ( i .ne. 1 ) then
         do j= i , 7
            text(j-i+1:j-i+1)= text(j:j)
            text(j:j)= ' '
         end do
      end if
      return
      end
