     	SUBROUTINE PIEPLT_PLOT
C
C  PROGRAMMER:  R.S. ZHOU
C		ALFRED UNIVERSITY
C
C	THIS ROUTINE CARRIES OUT THE PLOTTING FOR PIE-CHART PLOT.
C
     	INCLUDE 'PIEPLT.CBS'

     	IF( NPIE.EQ.0.0 ) THEN
     	    IF( IF_DCL_HARDCOPY() ) THEN
     		STOP '%PIEPLT_PLOT: no pie to be plotted'
     	    ELSE
     		CALL IFANSI_VIDEO(70)
     		WRITE(6,1000)
 1000		FORMAT(' oops: no pie to be plotted.')
     		RETURN
     	    ENDIF
     	ENDIF
C
C  INITIALIZE THE CRT...
C
     	CALL IFANSI_VIDEO(47)
     	CALL IFINIT_LINE( 3 )
     	IF( INIT ) THEN
     	    CALL IFINIT_PLOT( XORG(1), XORG(2), DATNAM )
     	ELSE
     	    CALL IFINIT_PLOT( XORG(1), XORG(2), '$OVERLAY' )
     	ENDIF
     	IF( IFCTRL_C() ) GOTO 999
C
C  OPEN A GRAPHICS SEGMENTS TO SAVE THE PLOT...
C
     	IF( .NOT.IFINIT_SAVE() ) RETURN
     	IF( .NOT.IF_DCL_HARDCOPY() ) WRITE(6,1)
  1	FORMAT('$Press CTRL/C to cancel plotting...')
     	CALL IFINIT_DIF(0)
     	CALL IFSCAL( XSCL(1), XSCL(2) )
     	CALL IFINIT_DIF(1)
C
C  DRAW GRAPH TITLE IF ANY...
C
     	IF( TITL.NE.' ' ) THEN
     	    CALL IFSPEN(CTIT)
     	    YPS = -0.5*PIED-0.65
     	    IF( PTIT.NE.1 ) YPS = -YPS
     	    CALL IFTEXT_CENT(-XORG(1),10.0-XORG(1),YPS,YPS,TITL,.12,.12)
     	ENDIF
C
C  DRAW THE PIES...
C
     	IF( IFCTRL_C() ) GOTO 999
    	CALL PIEPLT_DRAW
C
C  CLOSE THE PLOT SESSION...
C
  555	CALL IFSTYL( 1, 1 )			!RESET PLOTTING STYLUS
     	IF( CRT.EQ.2 ) CALL IFSEG_OPEN( 0 ) 	!CLOSE THE SEGMENT
C
C  OVERLAY THE LAYOUT IF LAYOUT FILE EXISTS...
C
     	IF( IFCTRL_C() ) GOTO 999
    	IF(DESNAM.NE.' ') CALL LAYOUT_MAIN(0,XORG(1),XORG(2),DESNAM)
  999	CALL IFINIT_PLOT_END
     	RETURN
    	END

    	SUBROUTINE PIEPLT_DRAW
C
C	THIS ROUTINE DRAWS THE PIES...
C
     	INCLUDE    'PIEPLT.CBS'
    	PARAMETER  D2R= 0.17453292E-1

     	ANG0 = FLOAT(PANG)
     	PRAD = 0.5*PIED
     	DO 1234 I=1, MNP
     	IF( IFCTRL_C() ) RETURN
     	    IF( PERC(I).LT.1.0 ) GOTO 1234
     	    ANGL = 3.6*PERC(I)
     	    CALL IFSPEN( IPEN(I) )
     	    CALL IFSTYL( ILIN(I), 1 )
     	    IF( IPAT(I).NE.0.0 ) CALL IFFILL( IPAT(I) )
     	    CALL IFARCS( XOFF(1,I), XOFF(2,I), PRAD, PRAD, PRAD,
     +                   0.0, ANG0, ANGL, 1.0 )
     	    IF( IPAT(I).NE.0.0 ) CALL IFFILL( 0 )
     	    ANG0 = ANG0 + ANGL
     	    IF( LABL(I).NE.' ' ) THEN
     		XPS = XLPS(1,I)
     		YPS = XLPS(2,I)
     		ANGL= ANG0 - 0.5*ANGL
     		IF( ANGL.LT.0.0 ) ANGL = 360.0+ANGL
     		IF( XPS.EQ.0.0 .AND. YPS.EQ.0.0 ) THEN
     		    XPS = (PRAD+0.25)*COS(D2R*ANGL)
     		    YPS = (PRAD+0.25)*SIN(D2R*ANGL)
     		    IF( ANGL.GT.90.0.AND.ANGL.LT.270.0 ) THEN
     			XPS = XPS-0.1*IFMISC_LENG(LABL(I))
     		    ENDIF
     		    XLPS(1,I) = XPS
     		    XLPS(2,I) = YPS
     		ENDIF
     		XPS = XPS + XOFF(1,I)
     		YPS = YPS + XOFF(2,I)
     		CALL IFTEXT_EXT(XPS,YPS,LABL(I),0.1,0.1,0.0)
     	    ENDIF
1234	CONTINUE
     	RETURN
     	END

