summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/generic/pst-bar/pst-bar.orig
blob: 407c75d7496170fd6028304e89b7e19f9002aa7a (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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
\csname PSTBarLoaded\endcsname%
\let\PSTBarLoaded\endinput%
\ifx\PSTplotLoaded\endinput\else\input pst-plot.tex\fi%
\pstheader{pst-bar.pro}%
\input pst-key.tex%

\def\fileversion{0.7a}
\def\filedate{2004/09/24}
\message{`pst-bar' v\fileversion, \filedate\space (Alan Ristow)}%

\define@key{psset}{barsep}{\edef\psk@barsep{#1}}%
\define@key{psset}{barcolsep}{\edef\psk@barcolsep{#1}}%
\define@key{psset}{barstyle}{\edef\psk@barstyle{#1}}%
\define@key{psset}{barlabelrot}{\edef\psk@barlabelrot{#1}}%
\setkeys{psset}{barsep=0.0,barcolsep=0.4,barstyle=\@empty,barlabelrot=0}%
%
% chartstyle=cluster|stack|block  (0,1,2)
\def\psset@chartstyle#1{\pst@expandafter\psset@@chartstyle{#1}\@nil\psk@chartstyle}
\def\psset@@chartstyle#1#2\@nil#3{%
  \ifx#1c\let#3\z@\else
    \ifx#1s\let#3\@ne\else
      \ifx#1b\let#3\tw@\else
        \@pstrickserr{Bad argument: `#1#2'}\@ehpa
      \fi
    \fi
  \fi
}%
\psset@chartstyle{cluster}%
%
% orientation=vertical|horizontal (0,1)
\def\psset@orientation#1{\pst@expandafter\psset@@orientation{#1}\@nil\psk@orientation}
\def\psset@@orientation#1#2\@nil#3{%
  \ifx#1h\let#3\p@\else
    \ifx#1v\let#3\z@\else
      \@pstrickserr{Bad argument: `#1#2'}\@ehpa
    \fi
  \fi
}%
\psset@orientation{vertical}%

\def\psset@header#1{%
  \pst@expandafter\psset@@header{#1}\@nil\psk@header
}%
\def\psset@@header#1#2\@nil#3{%
  \ifx#1t\let#3\z@\else
    \ifx#1f\let#3\@ne\else
      \@pstrickserr{Bad argument: `#1#2'}\@ehpa
    \fi
  \fi
}%
\psset@header{true}%

\def\psbarchart{\def\pst@par{}\pst@object{psbarchart}}%
\def\pstbar@header{\@empty}%
\def\psbarlabel#1{#1}%
\def\psbarlabelsep{0pt}%
\def\psbarscale(#1)#2{%
  \def\psbar@mul{#1\space}%
  \def\psbar@psop{#2\space}%
}
\def\psbar@mul{1\space}%
\def\psbar@psop{\@empty}%

\newread\pstbar@file%

% Define bar chart styles

\def\psset@barstyle#1{\def\@barstylelist{#1}}%
\def\newpsbarstyle#1#2{\@namedef{psbarcs@#1}{\psset{#2}}}%

\def\begin@barstyle{\def\pst@tempd{/barstyles [ }}%
\def\add@barstyle{%
  \def\pst@code{}%
  \ifpsshadow%
    \pst@closedshadow%
  \fi%
  \ifdim\psk@border\p@>\z@%
    \pst@addborder%
  \fi%
  \psk@fillstyle%
  \pst@stroke%
  \ifpsdoubleline%
    \pst@doublestroke%
  \fi%
  \def\pst@tempa{\strip@pt\psk@orientation}%
  \edef\pst@tempd{ \pst@tempd (%
    \pst@tempc \psk@cornersize%
    xbar1 ybar1 xbar2 ybar2 4 -\pst@tempa\space roll
    \psk@dimen%
    \tx@Frame%
    \pst@code )%
  }%
}
\def\end@barstyle{\edef\pst@tempd{ \pst@tempd ] def }}%

\def\setbarstyle{%
  \begin@barstyle%
  \expandafter\@setbarstyle\@barstylelist,\@nil\ignorespaces%
  \end@barstyle%
}%
\def\@setbarstyle#1,{%
  \@@setbarstyle{#1}%
  \@ifnextchar\@nil{\@gobble}{\@setbarstyle}%
}%
\def\@@setbarstyle#1{%
  \@ifundefined{psbarcs@#1}%
  {\@pstrickserr{Custom bar chart style `#1' undefined}\@ehpa}%
  {\@nameuse{psbarcs@#1}\add@barstyle}%
}%

% Default bar chart styles

\newpsbarstyle{red}{fillcolor=red,fillstyle=solid,framearc=0}%
\newpsbarstyle{green}{fillcolor=green,fillstyle=solid,framearc=0}%
\newpsbarstyle{blue}{fillcolor=blue,fillstyle=solid,framearc=0}%
\newpsbarstyle{black}{fillcolor=black,fillstyle=solid,framearc=0}%
\newpsbarstyle{white}{fillcolor=white,fillstyle=solid,framearc=0}%
\newpsbarstyle{gray}{fillcolor=gray,fillstyle=solid,framearc=0}%
\newpsbarstyle{lightgray}{fillcolor=lightgray,fillstyle=solid,framearc=0}%
\newpsbarstyle{darkgray}{fillcolor=darkgray,fillstyle=solid,framearc=0}%

\psset@barstyle{black,darkgray,gray,lightgray,white,red,green,blue}%

\def\readpsbardata{\@ifnextchar[{\readpsbardata@i}{\readpsbardata@i[]}}

\def\readpsbardata@i[#1]#2#3{%
  \def\pst@tempa{#1}%
  \ifx\pst@tempa\@empty\else\psset{#1}\fi
  \openin\pstbar@file=#3\relax%
  \ifeof\pstbar@file%
    \@pstrickserr{Data file `#3' not found.}\@ehpa
  \else
    \def\pst@tempb{\@empty}%
    \ifx\psk@header\z@%
      \@readpsbarheader%
    \fi
    \readpsbardata@ii%
    \edef\pst@tempb{[\pst@tempb]}%
    \let#2\pst@tempb%
  \fi
  \closein\pstbar@file%
}%

\def\readpsbardata@ii{%
  \read\pstbar@file to \pst@tempa
  \ifeof\pstbar@file
    \relax
  \else
    \edef\pst@tempb{\pst@tempb [}%
    \expandafter\readpsbardata@iii\pst@tempa,\@nil\ignorespaces%
    \edef\pst@tempb{\pst@tempb ]}%
    \readpsbardata@ii%
  \fi
}%

\def\readpsbardata@iii#1,{%
  \edef\pst@tempb{\pst@tempb #1\space}%
  \@ifnextchar\@nil{\@gobble}{\readpsbardata@iii}%
}%

\def\@readpsbarheader{%
  \read\pstbar@file to \pst@tempa
  \ifeof\pstbar@file
    \relax
  \else
    \let\pstbar@header\pst@tempa
  \fi
}%

% Apply labels from file header to bar chart columns.
% TODO: Deal with column headings that contain commas.

% The trimspaces command is (c) Michael Downes 1994
% (http://www.tug.org/tex-archive/info/aro-bend/answer.015).
\catcode`\Q=3
\def\psbar@trimspaces#1{%
  \begingroup
  \aftergroup\toks\aftergroup0\aftergroup{%
  \expandafter\trimb\expandafter\noexpand#1Q Q}%
  \edef#1{\the\toks0}%
}
\def\trimb#1 Q{\trimc#1Q}
\def\trimc#1Q#2{\afterassignment\endgroup \vfuzz\the\vfuzz#1}
\catcode`\Q=11

\def\pstbar@xlabels{%
  \pst@dima=-0.5pt\relax%
  \expandafter\pstbar@xlabels@i\pstbar@header,\@nil\ignorespaces%
}%

\def\pstbar@xlabels@i#1,{%
  \def\pst@tempa{#1}%
  \psbar@trimspaces\pst@tempa%
  \advance\pst@dima by 1pt%
  \def\pst@tempb{\strip@pt\pst@dima}%
  \uput[-90]{\psk@barlabelrot}(\pst@tempb,-\psbarlabelsep){\psbarlabel{\pst@tempa}}%
  \@ifnextchar\@nil{\@gobble}{\pstbar@xlabels@i}%
}%

\def\pstbar@labelcount#1{%
  \pst@dima=0pt\relax%
  \expandafter\pstbar@labelcount@i\pstbar@header,\@nil\ignorespaces%
  \let#1\pst@dima%
}%

\def\pstbar@labelcount@i#1,{%
  \advance\pst@dima by 1pt%
  \@ifnextchar\@nil{\@gobble}{\pstbar@labelcount@i}%
}%

\def\pstbar@ylabels{%
  \pstbar@labelcount{\pst@dima}%
  \advance\pst@dima by 0.5pt%
  \expandafter\pstbar@ylabels@i\pstbar@header,\@nil\ignorespaces%
}%

\def\pstbar@ylabels@i#1,{%
  \def\pst@tempa{#1}%
  \psbar@trimspaces\pst@tempa%
  \advance\pst@dima by -1pt%
  \def\pst@tempb{\strip@pt\pst@dima}%
  \uput[180]{\psk@barlabelrot}(-\psbarlabelsep,\pst@tempb){\psbarlabel{\pst@tempa}}%
  \@ifnextchar\@nil{\@gobble}{\pstbar@ylabels@i}%
}%

\def\psbarchart@i#1{%
  \pst@killglue%
  \begingroup%
    \use@par%
    \psbarchart@ii{#1}%
  \endgroup%
  \ignorespaces%
}%

\def\psbarchart@ii#1{%
  \begin@SpecialObj%

    % Save contents of \pst@code and load start-of-path code
    % into \pst@tempc

    \let\pst@tempb\pst@code%
    \def\pst@code{}%
    \solid@star%
    \let\pst@tempc\pst@code%

    % Load end-of-path code into \pst@tempd and restore original
    % contents of \pst@code

    \begin@barstyle%
    \expandafter\@setbarstyle\@barstylelist,\@nil\ignorespaces%
    \end@barstyle%
    \let\pst@code\pst@tempb%

    % Draw bar chart

    \pst@checknum\psk@barcolsep\pst@tempa%
    \pst@checknum\psk@barsep\pst@tempb%
    \ifx\psk@orientation\z@             % vertical
      \pst@dima=\psxunit%
      \pst@dimb=\psyunit%
      \def\pst@tempc{true}%
    \else\ifx\psk@orientation\p@        % horizontal
      \pst@dima=\psyunit%
      \pst@dimb=\psxunit%
      \def\pst@tempc{false}%
    \else
      \@pstrickserr{Bad orientation specification}\@ehpa
    \fi\fi
    \ifcase\psk@chartstyle% chartstyle=cluster
      \addto@pscode{%
        \pst@tempd%
        /BARDATA #1 def
        /nbars BARDATA length def
        /ncols BARDATA 0 get length def
        /colwidth \pst@number\pst@dima def
        /barcolsep \pst@tempa \pst@number\pst@dima mul def
        /barsep \pst@tempb \pst@number\pst@dima mul def
        /barwidth colwidth barcolsep sub nbars 1 sub barsep mul sub nbars div def
        /bXoffset 0.5 barcolsep mul def
        /colcount \pst@tempc\space {0}{ncols 1 sub} ifelse def
        /barcount 0 def
        /ybar1 0 def
        BARDATA {
          /DATAVECTOR exch def
          DATAVECTOR {
            /ybar2 exch \psbar@psop \psbar@mul mul \pst@number\pst@dimb mul def
            /xoffset barwidth barsep add barcount mul bXoffset add def
            /xbar1 colcount colwidth mul xoffset add def
            /xbar2 xbar1 barwidth add def
            ybar1 ybar2 ne {  % if ybar1 == ybar2, don't stroke a path
              newpath
              barstyles barcount get cvx exec
            } if
            /colcount \pst@tempc\space {colcount 1 add}{colcount 1 sub} ifelse def
          } forall
          /colcount \pst@tempc\space {0}{ncols 1 sub} ifelse def
          /barcount barcount 1 add def
        } forall
      }%
    \or%  chartstyle=stack
      \addto@pscode{%
        \pst@tempd%
        tx@BarDict begin
        /BARDATA #1 transpose def
        /ncols BARDATA length def
        /colwidth \pst@number\pst@dima def
        /barcolsep \pst@tempa \pst@number\pst@dima mul def
        /barwidth colwidth barcolsep sub def
        /bXoffset 0.5 barcolsep mul def
        /colcount \pst@tempc\space {0}{ncols 1 sub} ifelse def
        /barcount 0 def
        /ybar1 0 def
        BARDATA {
          /DATAVECTOR exch def
          /xbar1 colcount colwidth mul bXoffset add def
          /xbar2 xbar1 barwidth add def
          DATAVECTOR {
            /ybar2 exch \psbar@psop \psbar@mul mul \pst@number\pst@dimb mul ybar1 add def
            ybar1 ybar2 ne {  % if ybar1 == ybar2, don't stroke a path
              newpath
              barstyles barcount get cvx exec
              closepath
            } if
            /ybar1 ybar2 def
            /barcount barcount 1 add def
          } forall
          /barcount 0 def
          /ybar1 0 def
          /colcount \pst@tempc\space {colcount 1 add}{colcount 1 sub} ifelse def
        } forall
        end
      }%
    \or% chartstyle=block
      \addto@pscode{%
        \pst@tempd%
        tx@BarDict begin
        /BARDATA #1 transpose def
        /ncols BARDATA length def
        /nbars BARDATA 0 get length 2 idiv def
        /colwidth \pst@number\pst@dima def
        /barcolsep \pst@tempa \pst@number\pst@dima mul def
        /barsep \pst@tempb \pst@number\pst@dima mul def
        /barwidth colwidth barcolsep sub nbars 1 sub barsep mul sub nbars div def
        /bXoffset 0.5 barcolsep mul def
        /colcount \pst@tempc\space {0}{ncols 1 sub} ifelse def
        /barcount 0 def
        /ybar1 0 def
        BARDATA {
          /DATAVECTOR exch def
          0 1 nbars 1 sub {
            dup
            /ybar1 exch 2 mul DATAVECTOR exch get \psbar@psop \psbar@mul mul \pst@number\pst@dimb mul def
            /ybar2 exch 2 mul 1 add DATAVECTOR exch get \psbar@psop \psbar@mul mul \pst@number\pst@dimb mul def
            /xoffset barwidth barsep add barcount mul bXoffset add def
            /xbar1 colcount colwidth mul xoffset add def
            /xbar2 xbar1 barwidth add def
            ybar1 ybar2 ne {  % if ybar1 == ybar2, don't stroke a path
              newpath
              barstyles barcount get cvx exec
            } if
            /barcount barcount 1 add def
          } for
          /barcount 0 def
          /colcount \pst@tempc\space {colcount 1 add}{colcount 1 sub} ifelse def
        } forall
        end
      }%
    \else%
      \@pstrickserr{Unknown chart style.}\@ehpa%
    \fi%
    \ifx\psk@orientation\z@%
      \pstbar@xlabels%
    \else
      \pstbar@ylabels%
    \fi
  \end@SpecialObj%
}%

\endinput

\iffalse
%% CHANGE-LOG
 v 0.7a   2004/09/24
    Removed some debugging code that was accidentally left in the last release.
    The removed code was harmless, but caused confusion for some users.

 v 0.7    2004/09/15
    Made a minor change to \psbarscale so its functionality would be more
    closely analogous to the \pstScale command from the pstricks-add pacakge.
    Improved documentation. Set default barstyle to list of all available
    barstyles. First release to CTAN.

 v 0.6    2004/09/09
    Added orientation parameter and horizontal bar-drawing capability. Removed
    erroneous correction factor for linewidth from stacked bar chart.

 v 0.5    2004/09/02
    Added header parameter for \readpsbardata. Improved handling of headers in
    the data file -- spaces may now be included in a header item, though commas
    still may not.

 v 0.4    2004/08/29
    Added "block" chartstyle. Updated \readpsbardata to use .csv for both
    header and data. Introduced \psbarscale command. First release to pstricks
    mailing list, 2004/09/01.

 v 0.3    2004/08/26
    Added command \readpsbardata to read data for bar chart directly from a
    .csv file with the first row containing the column labels.

 v 0.2a   2004/08/23
    Changed colsep parameter name to barcolsep to avoid clash with psmatrix.

 v 0.2    2004/08/22
    Added stacked bar chart, chartstyle parameter, and introduced pst-bar.pro
    prologue with transpose command.

 v 0.1    2004/08/21
    Clustered bar chart.
\fi