#! /usr/NeWS/bin/psh
%
% This file is a product of Sun Microsystems, Inc. and is provided for
% unrestricted use provided that this legend is included on all tape
% media and as a part of the software program in whole or part.  Users
% may copy or modify this file without charge, but are not authorized to
% license or distribute it to anyone else except as part of a product
% or program developed by the user.
% 
% THIS FILE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
% WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
% PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
% 
% This file is provided with no support and without any obligation on the
% part of Sun Microsystems, Inc. to assist in its use, correction,
% modification or enhancement.
% 
% SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
% INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS FILE
% OR ANY PART THEREOF.
% 
% In no event will Sun Microsystems, Inc. be liable for any lost revenue
% or profits or other special, indirect and consequential damages, even
% if Sun has been advised of the possibility of such damages.
% 
% Sun Microsystems, Inc.
% 2550 Garcia Avenue
% Mountain View, California  94043
%
%
% "@(#)piechart 9.2 88/01/18
%
% Copyright (c) 1985 by Sun Microsystems, Inc.
%



    /DrawSlice { /grayshade exch def
		 /endangle exch def
		 /startangle exch def
		 /thelabel exch def
		 newpath 0 0 moveto
		 0 0 radius startangle endangle arc
		 closepath
		 gsave
		 grayshade monochromecanvas
		 { setgray } { .3 1 sethsbcolor } ifelse fill
		 grestore
		 stroke
		 gsave
		 startangle endangle add 2 div rotate
		 radius 0 translate
		 newpath
		 0 0 moveto labelps .8 mul 0 lineto stroke
		 labelps 0 translate
		 0 0 transform
		 grestore
		 itransform
		 /y exch def /x exch def
		 x y moveto
		 x 0 lt { thelabel stringwidth pop neg 0 rmoveto } if
		 y 0 lt { 0 labelps neg rmoveto } if
		 thelabel show
	       } def
    /findgray  { /i exch def /n exch def
		 i 2 mod 0 eq
		 { i 2 div n 2 div round add n div } 
		 { i 1 add 2 div n div } 
		 ifelse
    } def
/DrawPieChart {
		gsave
		xcenter ycenter translate
		/Helvetica findfont titleps scalefont setfont
		title stringwidth pop 2 div neg radius neg
		titleps 2.5 mul sub moveto
		title show
		/Helvetica findfont labelps scalefont setfont
		/numslices PieArray length def
		/slicecnt 0 def
		/curangle 0 def
		PieArray
		 { /slicearray exch def
		   slicearray aload pop
		   /percent exch def
		   /label exch def
		   /perangle percent 360 mul def
		   /slicecnt slicecnt 1 add def
		   label curangle curangle perangle add
		   numslices slicecnt findgray DrawSlice
		   /curangle curangle perangle add def
		 } forall
		grestore
	      } def
/DrawCPie {
		clippath pathbbox pop pop translate
		clippath pathbbox 4 2 roll pop pop	% T L A w h
		2 div exch 2 div exch			% T L A w/2 h/2
		dup 2 index exch 6 index 1.25 mul sub
		5 index 2 mul sub min
		exch 5 index add exch
		DrawPieChart
	  } def
/testpie {	/title (January Pie Sales) def
    		/titleps 24 def
		/labelps 12 def
		/PieArray [
		   [(Blueberry) .12]
		   [(Cherry) .30]
		   [(Apple) .26]
		   [(Boston Cream) .16]
		   [(Other) .04]
		   [(Vanilla Cream) .12]
		] def
		clippath pathbbox		% Calculate center & radius
		2 div /ymid exch def		% to just fit the window.
		2 div /xcenter exch def pop pop
		/ycenter ymid titleps 1.25 mul add def
		/radius xcenter ymid min titleps 1.25 mul sub labelps 2 mul sub def
		radius 10 lt { 				% Too small!
		    /ycenter ymid def
		    /radius xcenter ycenter min def
		} if
		DrawPieChart
	 } def
/PaintPie {
    50 dict begin
	setcanvas 0 setgray
	erasepage testpie
    end
} def

/win framebuffer /new DefaultWindow send def	% Create a window
{							% Install my stuff.
    /PaintClient { ClientCanvas PaintPie } def
    /PaintIcon { IconCanvas PaintPie } def
    /FrameLabel (Pie Chart) def
} win send
/reshapefromuser win send				% Shape it.
/map win send  % Map the window. (Damage causes PaintClient to be called)

