summaryrefslogtreecommitdiff
path: root/graphics/pstricks/contrib/piechart/piechart.sh
blob: 5a3696889ef3f2779a767a0ea5a024e10cdeec24 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#! /bin/sh

#                               -*- Mode: Ksh -*-
# piechart.sh --- Creation of pie charts in (La)TeX + PostScript with PSTricks
# Author          : Denis GIROU (CNRS/IDRIS - France) <Denis.Girou@idris.fr>
# Created the     : Fri Oct 16 21:53:41 1992
# Last mod. by    : Denis GIROU (CNRS/IDRIS - France) <Denis.Girou@idris.fr>
# Last mod. the   : Mon Oct 28 17:48:11 1996
# Version         : 2.3
#
# Description     :   piechart.sh is a simple SHELL and AWK program to
#                   realize hight quality, greatly customizable, grayscaled
#                   or colored pie charts in PostScript.
#                     It uses (La)TeX and the splendid PSTricks package of
#                   Timothy Van Zandt <tvz@princeton.edu>, available on CTAN
#                   in graphics/pstricks directory.
#                     It creates the (La)TeX (+ PSTricks) code for direct
#                   insertion in a (La)TeX document of the pie chart generated.
#                   It use a data file which describe the parts and the labels
#                   for them.
#                     You always can modify the generated code for more
#                   personal and sophisticated result.
#
# Syntax          : piechart.sh [scale_factor] ([PLAIN] | [FIGURE] [BOXIT]) \
#                               <data_file >output_file
#                     Default scale factor is 1.
#                     Default mode is LaTeX. In this case, use the FIGURE
#                   option if you want to define the figure LaTeX environment
#                   for the pie chart, and use the BOXIT option if you want
#                   a box around this figure.
#                     Use the PLAIN option if you want to generate a plain TeX
#                   code (in this case, you cannot used the FIGURE and BOXIT
#                   options).
#
# Input file      :   The fill styles are: none, solid, vlines, vlines*,
#                   hlines, hlines*, crosshatch, crosshatch*
#                     The predefined colors are : black, darkgray, gray,
#                   lightgray, white, red, green, blue, cyan, magenta, yellow
#                     You can easily define new gray or colors, or access to a
#                   rgb palette with the palette.sty file of PSTricks.
#                     You can also change a lot of parameters. See the
#                   documentation of PSTricks for more customization.
#
# Input file format:  You can add blanks lines and comments (beginning by #).
#                     The default field separator is | You can change it at
#                   the AWK level.
#                     The first non-comment line has 3 fields for titles
#                   (only the first is required), and after you must have one
#                   line by part of the pie chart, with 5 or 6 fields each
#                   (percentage, inside label, outside label, filling, color,
#                   explode). The last one is optional and must be "yes" if
#                   you want that this part will be explode in the pie chart.
#                     Obviously, the total of the percentages must be 100, or
#                   less.
#                     You can add (La)TeX commands in the labels (\em, \small,
#                   etc.), and separate lines by \\ in LaTeX and \cr in TeX.
#                   The text attributes cannot act on multiple lines : you
#                   must type {\em Paris}\\{\em France} for example in LaTeX.
#                     Be careful of some (La)TeX special characters (see $
#                   and % in the examples).
#                     Here are two examples, using a LaTeX syntax (suppress
#                   the first 2 characters of each line):
#
# # Data for the first example of pie charts
#
# # Title of the figure      | Title for the table of figures | Label
# Example 1 of pie chart
#
# # Value | Inside label  | Outside label                 | Type of    | Color
# #                                                       |  filling   |  fill
#      20 | \small\$9.0M  | {\em Paris}\\{\em France}     | hlines     | black
#      35 | \small\$16.7M | {\em London}\\Great Britain   | vlines     | black
#      45 | {\small\$23.1M}\\1991 | {\em Berlin}\\Germany | crosshatch | black
#
#
# # Data for the second example of pie charts
#
# # Title of the figure      | Title for the table of figures | Label
# \bf Example 2 of pie chart | Pie Chart 2                    | piechart2
#
# # Value | Inside | Outside label | Type of    | Color of | Explode part
# #       |  label |               |  filling   |  filling |
#       2 |        |  2\% \\ Last  | solid      | red      | yes
#      28 |        | 28\%          | solid      | blue
#       4 |        |  4\%          | solid      | green
#      47 |        | 47\% \\ First | solid      | magenta  | yes
#      19 |        | 19\%          | solid      | cyan
#
#
# Examples        :   If you name these files piechart1.dat and piechart2.dat,
#                   you can simply type:
#                   piechart.sh 1.5 <piechart1.dat >piechart1.tmp
#                   piechart.sh 0.7 figure boxit <piechart2.dat >pie2.tmp
#                   and just insert the two result files in a LaTeX document
#                   at the right place...
#                     There are no difference if you want to produce plain TeX
#                   code. You only must put valid plain TeX commands in the
#                   data files.
#
# Portability     :   Expected great. Tested on RS6000 under AIX 3.2 and
#                   DECStation 3100 under Ultrix 4.3
#
# Known problems  :   - Obviously, take care of the labels for the small
#                   parts. If they are too long or in a too big font, the
#                   result will not be pretty...
#                     - You must have the LANG environment variable to En_US
#                   if your UNIX has the National Language Support (NLS)
#                   (by instance, amusing problems arrive with LANG=Fr_FR ...)
#
# Thanks          :   Timothy Van Zandt for the great work of PSTricks, tests
#                   and ideas for the plain TeX mode.
#
# History         : (10/16/92) - rev. 1.0
#                   (10/20/92) - rev. 1.1:
#                      * increase the portability by suppression of the usage
#                        of the sub builtin function of AWK
#                      * better tracking of blank/null and comments lines
#                   (12/04/92) - rev. 2.0:
#                      * increase the portability by transformation of the
#                        unique AWK program in a SHELL script calling an
#                        AWK program :
#                          - the program is renamed form piecharts.awk to
#                            piechart.sh
#                          - input data file now given as parameter, not as
#                            standard input
#                          - the parameters are obtained in the SHELL script
#                          - the options (SCALE, FIGURE and BOXIT) are now
#                            treated by post-processing the AWK output file by
#                            SED (ugly but it's because some AWK versions
#                            doesn't accept the definition of external
#                            variables...)
#                      * change the unit for the values of the parts
#                        from fractions of 360 degrees to percentages
#                      * definition of the figure environment now optional,
#                        using the FIGURE option
#                      * new possibility to declare some explode parts in the
#                        piechart
#                      * use of the new \uput PSTricks macro for easy
#                        treatment of explode parts
#                      * possibility to have multiple (centered) line labels
#                        (inside and outside labels)
#                      * better tracking of syntax errors in the command line
#                   (12/11/92) - rev. 2.1:
#                      * generation of the tabular environment for labels only
#                        if they have multi-lines
#                      * the comment lines which begin with the ## are copied
#                        (without the ##) at the beginning of the result file.
#                        This allow automatic generation of input data file
#                        with a good specification of the colors, by automatic
#                        generation of the \newgray commands. It's to work in
#                        conjonction with the pie-data.sh script.
#                      * input data file read on the standard input
#                      * diminution of the width of the minipage to avoid
#                        the "overfull \hbox" problems
#                      * change the test of the sum of percentages from 100 to
#                        100.0001, because it can occurs problem with 100...
#                   (01/26/93) - rev. 2.2:
#                      * suppression of lines longer than 78 characters
#                      * change the obsolete \Polar PSTricks order to
#                        \SpecialCoor
#                      * add the PLAIN option and the possibility to generate
#                        plain TeX code
#                   (03/28/96) - rev. 2.2a:
#                        by Frank BENNETT <fbennett@rumple.soas.ac.uk>
#                      * changed break to continue in test for comment
#                        lines for compatibility with newer versions of
#                        awk and gawk
#                   (10/28/96) - rev. 2.3:
#                      * changed continue to next in test for comment
#                        lines (Sebastian Rahtz)
#                      * modification of the test "greater than 100%"
#                        (rounding problem)

# Interpretation of the parameters
if [ $# -gt 3 ]
   then echo "You cannot give more than 3 parameters."
        echo "Syntax: piechart.sh [scale factor] ([PLAIN] | [FIGURE] \
[BOXIT]) <data_file  >out_file"
        exit
fi

SCALE=1
FIGURE="xx"
BOXIT="xx"
TEX="LATEX"
while [ $# -ne 0 ]
  do if [ $1 = "BOXIT" ] || [ $1 = "boxit" ]
        then BOXIT="BOXIT"
        elif [ $1 = "FIGURE" ] || [ $1 = "figure" ]
              then FIGURE="FIGURE"
              elif [ $1 = "PLAIN" ] || [ $1 = "plain" ]
                   then TEX="PLAIN"
                   else SCALE=$1
     fi
     shift
done

# Verifications for the plain TeX mode
if [ $TEX = "PLAIN" ] && [ $BOXIT = "BOXIT" ]
   then echo "You cannot give the BOXIT parameter in PLAIN TEX mode."
        exit
fi
if [ $TEX = "PLAIN" ] && [ $FIGURE = "FIGURE" ]
   then echo "You cannot give the FIGURE parameter in PLAIN TEX mode."
        exit
fi

# Beginning of the AWK program
awk '
BEGIN{FS="|"                              # Fields separator
      PARAM="NO"
      BEGIN_VALUE=0
      END_VALUE=0
      TOTAL_PERCENT=0
      #   Definition of a figure environment (if FIGURE is not use, this line
      # will be remove later - ugly but it is because some AWK versions does
      # not accept the definition of external variables...)
      # if FIGURE
        print "%figure\\begin{figure}[htbp]"
      #   Framebox around the figure, (if BOXIT is not use, this line will be
      # remove later)
      # if BOXIT
        print "%boxit  \\psframebox[framesep=8mm]{"
      print "%latex  {\\addtolength{\\textwidth}{-2.3cm}"
      print "%latex   \\begin{minipage}{\\textwidth}"
      print "%latex    \\begin{center}"
      # if FIGURE
        print "%figure     \\vspace{6mm}"
      print "%latex     {\\setlength{\\tabcolsep}{0cm}"
      print "%tex     {%"
      print "      \\psset{unit=cm,xunit=cm,yunit=cm}"
      print "%latex      \\begin{pspicture}(-2,-2)(2,2)"
      print "        \\SpecialCoor"
      print "%tex      \\pspicture(-2,-2)(2,2)"
      print "%tex      \\def\\PieLabel\#1{\\vbox{\\halign{\\hfil\#\#"\
"\\hfil\\cr\#1\\cr}}}%"
      print "        \\psset{framesep=1.5pt}"
      }

{
 # Suppression of the right and left blanks of the variables
 for (I=1;I<=NF+1;I++) {
     # On the left...
     for (J=1;J<=length($I)+1;J++)
         if (substr($I,1,1) == " ")
            $I=substr($I,2)
          else
            break
     # On the right...
     for (J=length($I);J>0;J--)
         if (substr($I,J,1) == " ")
            $I=substr($I,1,J-1)
          else
            break
 }

 #   Special comments before title (beginning by ##): they are copied
 # at the beginning of the result file, without the ##
 if (substr($1,1,2) == "##")
    print substr($1,3,length($1))

 # Comments, null and blank lines
 if (substr($1,1,1) == "#" || $1 == "" || $1 == " ")
    next

 if (PARAM == "NO") {
     #   Generic parameters: title, title for the table of figures, label
     # (the last two are optional)
     PARAM="YES"
     TITLE=$1
     TABLE_TITLE=$2
     LABEL=$3}
   else {
     # Verification of the sum of the parts
     TOTAL_PERCENT=TOTAL_PERCENT+$1
     if (TOTAL_PERCENT > 100.01) {
        print "Error! Total of percentages greater than 100! (" \
              TOTAL_PERCENT ")"
        exit}
     # Transformation of percentages in fractions of 360 degrees
     $1=$1*3.6
     END_VALUE=END_VALUE+$1
     VAL=BEGIN_VALUE+$1/2
     #Explode part
     if ($6 == "YES" || $6 == "yes")
         print "       \\uput{0.3}[" VAL "](0;0){"
     print "        \\pswedge[fillstyle=" $4 ",fillcolor=" $5 "]{2}{" \
           BEGIN_VALUE "}{" END_VALUE "}"
     # Inside label
     if ($2 != "") {
        print "%tex        \\rput(1.2;" VAL "){\\psframebox*{\\PieLabel{" \
$2 "}}}"
        if (index($2,"\\\\") == 0)
           print "%latex        \\rput(1.2;" VAL "){\\psframebox*{" $2 "}}"
         else {
           print "%latex        \\rput(1.2;" VAL \
                 "){\\psframebox*{\\begin{tabular}{c}"
           print "%latex                                      " $2
           print "%latex                                    \\end{tabular}}}"
           }
        }
     # Outside label
     if ($3 != "") {
        print "%tex        \\uput{2.2}[" VAL "](0;0){\\PieLabel{" $3 "}}"
        if (index($3,"\\\\") == 0)
           print "%latex        \\uput{2.2}[" VAL "](0;0){" $3 "}"
         else {
           print "%latex        \\uput{2.2}[" VAL "](0;0){\\begin{tabular}{c}"
           print "%latex                               " $3
           print "%latex                             \\end{tabular}}"
           }
        }
     # End of explode treatment
     if ($6 == "YES" || $6 == "yes")
        print "       }"
     BEGIN_VALUE=END_VALUE}
 $1=""
}

END{
    print "%latex      \\end{pspicture}"
    print "%tex      \\endpspicture"
    print "     }"
    # For the case of a multiple line outside label at the bottom...
    # if FIGURE
      print "%figure     \\vspace{4mm}"
    print "%latex    \\end{center}"
    # if FIGURE
      if (TABLE_TITLE == "")
         print "%figure  \\caption{" TITLE "}"
       else
         print "%figure  \\caption[" TABLE_TITLE "]{" TITLE "}"
      if (LABEL != "")
         print "%figure  \\label{f:" LABEL "}"
    print "%latex   \\end{minipage}"
    print "%latex  }"
    # if BOXIT
      print "%boxit }"
    # if FIGURE
      print "%figure\\end{figure}"
      }' >/tmp/piechart.tmp

#   Post-processing for the "scale factor", PLAIN, FIGURE and BOXIT parameters
# (ugly but it's because some AWK versions doesn't accept the definition
#  of external variables...)
if [ $FIGURE = "FIGURE" ]
   then sed -e /unit=/s/unit=/unit=$SCALE/g -e /\^%figure/s/// \
            /tmp/piechart.tmp >/tmp/piechart.tmp2
   else sed -e /unit=/s/unit=/unit=$SCALE/g -e /\^%figure/d \
            /tmp/piechart.tmp >/tmp/piechart.tmp2
fi
mv /tmp/piechart.tmp2 /tmp/piechart.tmp
if [ $BOXIT = "BOXIT" ]
   then sed -e /\^%boxit/s/// /tmp/piechart.tmp >/tmp/piechart.tmp2
   else sed -e /\^%boxit/d    /tmp/piechart.tmp >/tmp/piechart.tmp2
fi

# plain TeX or LaTeX mode
mv /tmp/piechart.tmp2 /tmp/piechart.tmp
if [ $TEX = "LATEX" ]
   then sed -e /\^%tex/d      /tmp/piechart.tmp >/tmp/piechart.tmp2
        mv /tmp/piechart.tmp2 /tmp/piechart.tmp
        sed -e /\^%latex/s/// /tmp/piechart.tmp >/tmp/piechart.tmp2
   else sed -e /\^%latex/d    /tmp/piechart.tmp >/tmp/piechart.tmp2
        mv /tmp/piechart.tmp2 /tmp/piechart.tmp
        sed -e /\^%tex/s///   /tmp/piechart.tmp >/tmp/piechart.tmp2
fi

# Deliver of the result!
cat /tmp/piechart.tmp2

rm /tmp/piechart.tmp /tmp/piechart.tmp2