* PLOT.CMD X,Y plots on screen and printer
* Uses PLOT0,PLOT1,PLOT2
*
CLEAR
SET DEFA TO A
SET TALK OFF
SET BELL OFF
*
STORE 0 TO LEVEL
DO WHILE LEVEL=0
*
ERASE
? '                    PLOT.CMD'
?
? '     --- X vs Y Plots on Screen and Printer ---'
?
? '    (Assumes data (DBF) file is on the B: drive.)'
?
?
?
STORE '0' TO FILNM
DO WHILE FILNM='0'
  ACCEPT 'Enter data file name (? for list),(no B:)->' TO FILNM
  IF FILNM=' '
    RELEASE ALL
    RETURN
  ENDIF
  IF FILNM='?'
    DISP FILES ON B
    STORE '0' TO FILNM
  ENDIF
ENDDO
STORE 'B:'+FILNM TO FILNM
USE &FILNM
*
STORE 1 TO LEVEL
DO WHILE LEVEL=1
*
STORE ' ' TO XVAR
STORE ' ' TO YVAR
STORE '.' TO TYPL
*
ERASE
STORE '_' TO TYPL
@ 5,15 SAY 'Plot Types Are:'
@ 6,10 SAY 'P(oints - Scatter Diagram'
@ 7,10 SAY 'B(ar    - Bar Chart'
@ 8,10 SAY '<RET>   - Return to DBASE II'
DO WHILE TYPL<>'B' .AND. TYPL<>'P' .AND. TYPL<>' '
  @ 10,10 SAY 'Enter plot type P,B,<RET> ->' GET TYPL PICTURE 'X'
  READ
ENDDO
ERASE
IF TYPL=' '
  RELEASE ALL
  RETURN
ENDIF
*
STORE 2 TO LEVEL
DO WHILE LEVEL = 2
*
IF TYPL='B'
  ? 'Note:  The B(ar chart option requires that the X axis field data be in'
  ? '       assending order with no duplications.  The record number can be'
  ? '       used as the X axis by typing # for the X variable name.'
  ?
ENDIF
STORE 'X' TO LOOKFOR
DO WHILE LOOKFOR='X'
  ACCEPT 'Enter X variable name (? for list)->' TO XVAR
    IF XVAR=' '
      RELEASE ALL
      RETURN
    ENDIF
    IF XVAR='?'
      DISP STRU
      LOOP
    ENDIF
    IF TYPE(&XVAR)<>'N'
      DISP OFF 'Non numeric variable, or no match in file.  Try again.'
      LOOP
    ENDIF
  STORE 'Y' TO LOOKFOR
ENDDO
*
DO WHILE LOOKFOR='Y'
  ACCEPT 'Enter Y variable name (? for list)->' TO YVAR
    IF YVAR=' '
      RELEASE ALL
      RETURN
    ENDIF
    IF YVAR='?'
      DISP STRU
      LOOP
    ENDIF
    IF TYPE(&YVAR)<>'N'
      DISP OFF 'Non numeric variable, or no match in file.  Try again.'
      LOOP
    ENDIF
  STORE 'DONE' TO LOOKFOR
ENDDO
*
* Find min and max of fields
GO TOP
DO WHILE *
  SKIP
ENDDO
STORE &XVAR TO MINX
STORE &XVAR TO MAXX
STORE &YVAR TO MINY
STORE &YVAR TO MAXY
*
DO WHILE .NOT. EOF
  SKIP
  IF .NOT. *
    IF &XVAR<MINX
      STORE &XVAR TO MINX
    ENDIF
    IF &XVAR>MAXX
      STORE &XVAR TO MAXX
    ENDIF
    IF &YVAR<MINY
      STORE &YVAR TO MINY
    ENDIF
    IF &YVAR>MAXY
      STORE &YVAR TO MAXY
    ENDIF
  ENDIF
ENDDO
*
STORE 3 TO LEVEL
DO WHILE LEVEL=3
  ERASE
  STORE 0 TO XMARKS
  STORE 0 TO YMARKS
  @ 2,12 SAY '-- Specify Plot Bounds ---'
  @ 3,12 SAY '--------------------------'
  @ 5,14 SAY '(Default Values Shown)'
  @ 9,9 SAY XVAR USING 'XXXXXXXXXX'
  @ 10,1 SAY 'Min X     ' GET MINX PICT '99999999.999999'
  @ 11,1 SAY 'Max X     ' GET MAXX PICT '99999999.999999'
  @ 12,1 SAY 'Divisions ' GET XMARKS PICT '99'
  @ 14,9 SAY YVAR USING 'XXXXXXXXXX'
  @ 15,1 SAY 'Min Y     ' GET MINY PICT '99999999.999999'
  @ 16,1 SAY 'Max Y     ' GET MAXY PICT '99999999.999999'
  @ 17,1 SAY 'Divisions ' GET YMARKS PICT '9'
  @ 11,30 SAY 'Max Y  |'
  @ 12,30 SAY '       |'
  @ 13,30 SAY '       |'
  @ 14,30 SAY 'Min Y  |'
  @ 15,30 SAY '       -------------------'
  @ 16,30 SAY '       Min X         Max X'
  READ
*
* Plot border and points
STORE 6 TO PLOT:LF
STORE 75 TO PLOT:RT
STORE 21 TO PLOT:BT
STORE YVAR TO SIDTI
DO PLOT0
STORE (PLOT:RT+PLOT:LF)/2 - LEN(TRIM(XVAR))/2 TO XPOS
@ PLOT:BT+1,XPOS SAY XVAR
DO CASE
  CASE TYPL='B'
    DO PLOT2
  OTHERWISE
    DO PLOT1
ENDCASE
*
STORE N TO HARDCOPY
STORE PLOT:BT+2 TO YPOS
@ YPOS,1 SAY 'Points Out of Bounds: '
@ YPOS,23 SAY OUTBND USING '9999'
@ YPOS,32 SAY 'Hard Copy (Y/N)?' GET HARDCOPY PICTURE 'X'
READ
*
DO WHILE HARDCOPY
  ERASE
  @ 1,17 SAY 'Enter plot tiles:'
  STORE '                        ' TO TOPTI
  @ 5,1 SAY 'Top: ' GET TOPTI PICTURE 'XXXXXXXXXXXXXXXXXXXXXXXXX'
  STORE '                        ' TO SIDTI
  @ 6,1 SAY 'Side Title: ' GET SIDTI PICTURE 'XXXXXXXXXXXXXXXX'
  STORE ;
   '                                                                     ' ;
      TO BOT1
  @ 7,1 SAY 'Bot1: ' ;
    GET BOT1 PICTURE ;
   'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
  STORE '                                                 ' TO BOT2
  @ 8,1 SAY 'Bot2: ' ;
    GET BOT2 PICTURE 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
  STORE '                                                 ' TO BOT3
  @ 9,1 SAY 'Bot3: ' ;
    GET BOT3 PICTURE 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
  STORE T TO PTRRDY
  @ 12,1 SAY 'Ready printer, then hit return.' GET PTRRDY PICTURE 'X'
  READ
  IF .NOT.(PTRRDY)
    STORE F TO HARDCOPY
    LOOP
  ENDIF
*
  ERASE
  STORE 30 TO PLOT:BT
  STORE 72 TO PLOT:RT
  STORE 6 TO PLOT:LF
  DO PLOT0
  DO CASE
    CASE TYPL='P'
      DO PLOT1
    CASE TYPL='B'
      DO PLOT2
  ENDCASE
*
  SET FORMAT TO PRINT
  @ 0,0 SAY CHR(27)+'E'
  @ 0,1 SAY CHR(14)
  @ 0,24-LEN(TRIM(TOPTI))/2 SAY TOPTI
  ?
  ?
  STORE 1 TO YPOS
  STORE 0 TO XPOS
  DO WHILE YPOS<PLOT:BT+3
    STORE ' ' TO LINPRN
    DO WHILE XPOS<=PLOT:RT
      STORE PEEK(61440+XPOS+128*(YPOS-1)) TO SCRC
      DO CASE
        CASE SCRC>31
          STORE LINPRN+CHR(SCRC) TO LINPRN
        CASE SCRC=11
          STORE LINPRN+CHR(202) TO LINPRN
        CASE SCRC=24
          STORE LINPRN+CHR(172) TO LINPRN
        CASE SCRC=26
          STORE LINPRN+CHR(174) TO LINPRN
        CASE SCRC=3
          STORE LINPRN+CHR(174) TO LINPRN
        CASE SCRC=12
          STORE LINPRN+CHR(206) TO LINPRN
        CASE SCRC=18
          STORE LINPRN+CHR(161) TO LINPRN
        CASE SCRC=20
          STORE LINPRN+CHR(162) TO LINPRN
        CASE SCRC=6
          STORE LINPRN+CHR(176) TO LINPRN
        CASE SCRC=7
          STORE LINPRN+CHR(192) TO LINPRN
        CASE SCRC=1
          STORE LINPRN+CHR(181) TO LINPRN
        CASE SCRC=4
          STORE LINPRN+CHR(202) TO LINPRN
        CASE SCRC=5
          STORE LINPRN+CHR(203) TO LINPRN
        CASE SCRC=17
          STORE LINPRN+CHR(183) TO LINPRN
        CASE SCRC=23
          STORE LINPRN+CHR(163) TO LINPRN
        OTHERWISE
          STORE LINPRN+CHR(32) TO LINPRN
     ENDCASE
     STORE XPOS+1 TO XPOS
   ENDDO
   @ YPOS,0 SAY LINPRN
   STORE YPOS+1 TO YPOS
   STORE 0 TO XPOS
  ENDDO
*
  @ PLOT:BT+3,(40-LEN(TRIM(BOT1))/2) SAY BOT1
  @ PLOT:BT+4,(40-LEN(TRIM(BOT2))/2) SAY BOT2
  @ PLOT:BT+5,(40-LEN(TRIM(BOT3))/2) SAY BOT3
  EJECT
  SET FORMAT TO SCREEN
  ERASE
  STORE N TO HARDCOPY
ENDDO
*
ERASE
@ 2,20 SAY '--Options--'
@ 5,10 SAY 'Q  - Quit to DBASE II'
@ 6,10 SAY 'F  - Change data file'
@ 7,10 SAY 'B  - Change plot bounds'
@ 8,10 SAY 'V  - Change variables (fields)'
@ 9,10 SAY 'T  - Change plot type (Bar, Points)'
STORE ' ' TO OPT
@ 13,15 SAY 'Option ->' GET OPT PICTURE 'X'
READ
*
DO CASE
  CASE !(OPT)='B'
    STORE 3 TO LEVEL
  CASE !(OPT)='V' 
    STORE 2 TO LEVEL
  CASE !(OPT)='T'
    STORE 1 TO LEVEL
  CASE !(OPT)='F'
    STORE 0 TO LEVEL
  OTHERWISE
    SET DEFA TO B
    RELEASE ALL
    RETURN
  ENDCASE
  ERASE
ENDDO
ENDDO
ENDDO
ENDDO