summaryrefslogtreecommitdiff
path: root/dviware/psprint/unix/piechart.ps
blob: 1d9d6137eabb18d81dc5e322a9d52f34c4362b8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
% This file is inserted into the PSPRINT User Guide using \special.
% It draws a simple pie chart (based on p.187 of the PostScript Cookbook).

/DrawSlice
{ % this routine expects 3 parameters on stack:
    /endangle exch def
    /startangle exch def
    /thelabel exch def
    newpath 0 0 moveto
       0 0 radius startangle endangle arc
    closepath
    1.415 setmiterlimit      % bevel the lines joining in the centre
    0.01 Inch setlinewidth
    stroke
    gsave
       % calculate starting position for label
       startangle endangle add 2 div rotate
       radius labelps add 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   % move label left
    y 0 lt { 0 labelps neg rmoveto } if                    % move label down
    thelabel show
} def

/DrawPieChart
{ % this routine expects 5 parameters on stack:
    /radius exch def
    /ycenter exch def
    /xcenter exch def
    /PieArray exch def
    /labelps exch def
    xcenter ycenter translate
    /Helvetica findfont labelps scalefont setfont
    /curangle 0 def
    PieArray
     { /slicearray exch def
       slicearray aload pop
       /percent exch def
       /label exch def
       /perangle percent 100 div 360 mul def
       label curangle curangle perangle add DrawSlice
       /curangle curangle perangle add def
     } forall    % repeat above procedure for each element in PieArray
} def

/Inch { 72 mul } def       % converts inches to points

% ----- MAIN PROGRAM -----

8                          % label point size
[                          % PieArray is an array of arrays
  [ (Blueberry) 12 ]
  [ (Apple)     30 ]
  [ (Cherry)    12 ]       % include any number of label, percentage pairs
  [ (Raspberry) 16 ]       % but make sure the numbers add up to 100
  [ (Apricot)   24 ]
  [ (Other)      6 ]
]
3.15 Inch 1.0 Inch         % x,y center of pie chart
                           % (3.15in * 2 = 6.3in = text width in User Guide)
.7 Inch                    % radius
DrawPieChart               % draw pie chart using above arguments

showpage