%
% 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 23.2 90/06/19
%
% Copyright (c) 1989 by Sun Microsystems, Inc.
%


/PieCanvas ClassCanvas []
classbegin
    
% Class Variables:

% Defaults:

    /FillColor 		1 1 1 rgbcolor def
    /StrokeColor        0 0 0 rgbcolor def
    /TextFamily 	/Helvetica def
    /TextSize 		24 def
    /LabelSize 		12 def
    /Title 		(January Pie Sales) def
    /PieArray [
        [(Blueberry) .12]
        [(Cherry) .30]
        [(Apple) .26]
        [(Boston Cream) .16]
        [(Other) .04]
        [(Peach) .12]
    ] def
   
% Methods:

    % Return the pie chart title.
    %
    /pietitle { % - => string
	Title 
    } def

    % Draw and label a pie slice.  Use startangle and endangle to size the
    % slice; use grayshade to color the slice; use thelabel to label the slice.
    %
    /DrawSlice { % string angle angle num => - 
	dictbegin
	    /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 LabelSize .8 mul 0 lineto stroke
	    	LabelSize 0 translate
	    	0 0 transform
	    grestore
	    itransform
	    exch dup 2 index moveto 
	    0 lt { 
		thelabel stringwidth pop neg 0 rmoveto 
	    } if
	    0 lt { 
		0 LabelSize neg rmoveto 
	    } if
	    thelabel show
	dictend pop
    } def

    % Use the number of slices in the pie and the current slice count
    % to generate a gray shade for coloring the current pie slice.
    %
    /FindGray {  % num num => num 
	dictbegin
	    /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
	dictend pop
    } def


    % Draw the piechart using the individual slices specified in the
    % PieArray.  For each slice, call FindGray to get the color and
    % DrawSlice to draw and label the slice.
    %
    /DrawPieChart { % - => -
	dictbegin
	    gsave
	    	xcenter ycenter translate
	    	TextFont setfont
	    	Title stringwidth pop 2 div neg radius neg
	    	TextSize 2.5 mul sub moveto
	    	Title show
	    	TextFamily findfont LabelSize 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 self send 
		    /DrawSlice self send
	    	    /curangle curangle perangle add def
	        } forall
	    grestore
	dictend pop
    } def

    % Paint the canvas with an appropriately sized pie chart.
    %
    /PaintCanvas { % - => -
        % if painting will do any good, set up the coordinate space
        % and paint
        clippath emptypath not {
            pathbbox points2rect
            % calculate center and radius to just fit the window
            2 div /ymid exch def
            2 div /xcenter exch def pop pop
            /ycenter ymid TextSize 1.25 mul add def
            /radius xcenter ymid min
		TextSize 1.25 mul sub
		LabelSize 2 mul sub def
    	    radius 10 lt { 
                /ycenter ymid def
                /radius xcenter ycenter min def
            } if
            StrokeColor 0 FillColor /StrokeAndFillCanvas self send
            /DrawPieChart self send
        } if
    } def

    /minsize { % - => w h
	/minsize super send
	64 max exch
	64 max exch
    } def

    /preferredsize { % - => width height
	gsave
	    /canvas self send setcanvas
	    TextFont setfont
	    Title stringwidth pop
	    truncate 5 add dup 20 sub
	grestore
    } def 

classend def
 

% create a window
% 
/win
    [PieCanvas] [/Footer false] framebuffer /newdefault ClassBaseFrame send
def

[PieCanvas] /seticon win send
null /seticonlabel win send
(Pie Chart) /setlabel win send

/place win send
/activate win send
/map win send

newprocessgroup
currentfile closefile
