summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/bchart/bchart.tex
blob: 68a87ea7a4a195ed638e776756e08a8445c82190 (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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
\documentclass{article}

\usepackage{bchart}

\frenchspacing

\title{
  \texttt{bchart}: Simple Bar Charts in \LaTeX
  \medskip\\
  \large Version 0.1.2
}
\author{Tobias Kuhn}
\date{22 August 2012}

\begin{document}

\maketitle


\section{Introduction}

\texttt{bchart} is a {\LaTeX} package for drawing simple bar charts with horizontal bars on a numerical x-axis. It is based on the TikZ drawing package. The focus of this package is on simplicity and aesthetics.

To use the package, you have to make sure that {\LaTeX} is able find the file \texttt{bchart.sty}, e.g. by placing a copy of it into the directory of the source file that is using the package. In order to load the package, you have to place the following command at the beginning of your {\LaTeX} source file:
\begin{quote}\small
\begin{verbatim}
\usepackage{bchart}
\end{verbatim}
\end{quote}


\section{Charts}

Charts are created with the \texttt{bchart}-environment. Within this environment, you can put one or more bars by using the \texttt{bcbar}-command. The code of a very simple chart and the resulting picture are shown here:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[max=50]
  \bcbar{45}
  \bcbar{26}
  \bcbar{31}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[max=50]
  \bcbar{45}
  \bcbar{26}
  \bcbar{31}
\end{bchart}
\end{quote}
The option \texttt{max} defines the maximum value on the x-axis (the default is 100). The number given to the \texttt{bcbar}-command defines the value of the respective bar, visualized by the width of the bar and printed as a number to the right of it. The minimum value on the x-axis defaults to 0, but this can be changed by the use of the option \texttt{min}. Furthermore, the option \texttt{step} can be used to show marks at regular intervals:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[min=2.5,step=0.25,max=3.75]
  \bcbar{2.6}
  \bcbar{3.7}
  \bcbar{3.1}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[min=2.5,step=0.25,max=3.75]
  \bcbar{2.6}
  \bcbar{3.7}
  \bcbar{3.1}
\end{bchart}
\end{quote}
For marks at irregular intervals, the \texttt{steps}-option can be used instead:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[min=1,max=18,steps={1,3,7,15}]
  \bcbar{13}
  \bcbar{4}
  \bcbar{7}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[min=1,max=18,steps={1,3,7,15}]
  \bcbar{13}
  \bcbar{4}
  \bcbar{7}
\end{bchart}
\end{quote}
The \texttt{plain}-flag hides all marks:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[max=8,plain]
  \bcbar{6.2}
  \bcbar{1.8}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[max=8,plain]
  \bcbar{6.2}
  \bcbar{1.8}
\end{bchart}
\end{quote}
The font style is sans-serif by default. This can be changed by redefining the command \texttt{bcfontstyle}:
\begin{quote}\small
\begin{verbatim}
\renewcommand{\bcfontstyle}{\bfseries}
\begin{bchart}[max=10,step=1]
  \bcbar{5.7}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\renewcommand{\bcfontstyle}{\bfseries}
\begin{bchart}[max=10,step=1]
  \bcbar{5.7}
\end{bchart}
\end{quote}
With an empty argument, the default font of the document is used:
\begin{quote}\small
\begin{verbatim}
\renewcommand{\bcfontstyle}{}
\begin{bchart}[max=10,step=1]
  \bcbar{5.7}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\renewcommand{\bcfontstyle}{}
\begin{bchart}[max=10,step=1]
  \bcbar{5.7}
\end{bchart}
\end{quote}


\section{Bars}

The bars of a bar chart can be modified in several ways. The \texttt{text}-option prints text within the inside of the bar:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[step=2,max=8]
  \bcbar[text=Year 1]{6}
  \bcbar[text=Year 2]{3}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[step=2,max=8]
  \bcbar[text=Year 1]{6}
  \bcbar[text=Year 2]{3}
\end{bchart}
\end{quote}

The bars can be given different background colors by the use of the \texttt{color}-option:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[max=8]
  \bcbar[text=A,color=yellow]{6}
  \bcbar[text=B,color=red!50]{3}
  \bcbar[text=C,color=green!60!blue]{4}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[max=8]
  \bcbar[text=A,color=yellow]{6}
  \bcbar[text=B,color=red!50]{3}
  \bcbar[text=C,color=green!60!blue]{4}
\end{bchart}
\end{quote}
In addition to predefined colors like \texttt{yellow} and \texttt{red}, new colors can be defined and existing ones can be combined. \texttt{red!50}, for example, stands for a 50\% saturated red, whereas \texttt{green!60!blue} stands for a color obtained by blending green and blue at a ratio of 60:40. See the TikZ manual for more information on how to manipulate colors. The default color for bars is \texttt{blue:20}.

The \texttt{plain}-flag can be used to prevent the value of the bar to be displayed:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[step=1,max=8]
  \bcbar[plain]{6}
  \bcbar[plain]{3}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[step=1,max=8]
  \bcbar[plain]{6}
  \bcbar[plain]{3}
\end{bchart}
\end{quote}

With the \texttt{value}-option, the value to be displayed to the right of the bar can be modified:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[step=2,max=8]
  \bcbar[value=six]{6}
  \bcbar[value=III]{3}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[step=2,max=8]
  \bcbar[value=six]{6}
  \bcbar[value=III]{3}
\end{bchart}
\end{quote}


\section{Skips}

The common commands \texttt{smallskip}, \texttt{medskip} and \texttt{bigskip} are redefined within the bar chart environment and can be used to insert skips of different sizes between two bars:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[step=2,max=10]
  \bcbar{3.4}
  \smallskip
  \bcbar{5.6}
  \medskip
  \bcbar{7.2}
  \bigskip
  \bcbar{9.9}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[step=2,max=10]
  \bcbar{3.4}
  \smallskip
  \bcbar{5.6}
  \medskip
  \bcbar{7.2}
  \bigskip
  \bcbar{9.9}
\end{bchart}
\end{quote}
With the \texttt{bcskip}-command, skips of arbitrary size can be inserted:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[step=10,max=100]
  \bcbar{83}
  \bcskip{3pt}
  \bcbar{25}
  \bcskip{15mm}
  \bcbar{69}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[step=10,max=100]
  \bcbar{83}
  \bcskip{3pt}
  \bcbar{25}
  \bcskip{15mm}
  \bcbar{69}
\end{bchart}
\end{quote}


\section{Labels}

The x-axis can be labeled by calling the \texttt{bcxlabel}-command:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[step=200,max=1000]
  \bcbar[text=Company A]{346}
  \bcbar[text=Company B]{873}
  \bcxlabel{number of employees}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[step=200,max=1000]
  \bcbar[text=Company A]{346}
  \bcbar[text=Company B]{873}
  \bcxlabel{number of employees}
\end{bchart}
\end{quote}
The option \texttt{label} can be used to label bars and skips:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[step=2,max=16]
  \bcbar[label=1st bar]{8.5}
  \bigskip[label=skip]
  \bcbar[label=2nd bar]{4.5}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[step=2,max=16]
  \bcbar[label=1st bar]{8.5}
  \bigskip[label=skip]
  \bcbar[label=2nd bar]{4.5}
\end{bchart}
\end{quote}
The command \texttt{bclabel} generates ``free'' labels at the respective position in the chart:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[step=2,max=16]
  \bcbar{8.5}
  \bclabel{free label}
  \bcbar{4.5}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[step=2,max=16]
  \bcbar{8.5}
  \bclabel{free label}
  \bcbar{4.5}
\end{bchart}
\end{quote}


\section{Units}

The \texttt{unit}-option of the chart environment attaches a unit to all values, like ``kg'', ``m\textsuperscript{2}'', or ``\%'':
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[min=50,max=100,step=10,unit=\%]
  \bcbar{72.3}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[min=50,max=100,step=10,unit=\%]
  \bcbar{72.3}
\end{bchart}
\end{quote}
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[min=50,max=100,step=10,unit=m\textsuperscript{2}]
  \bcbar{72.3}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[min=50,max=100,step=10,unit=m\textsuperscript{2}]
  \bcbar{72.3}
\end{bchart}
\end{quote}


\section{Width and Scaling}

The \texttt{width}-option of the chart environment allows for adjusting the width of the chart:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[max=10,step=2,width=4cm]
  \bcbar{7.5}
  \bcbar{3.2}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[max=10,step=2,width=4cm]
  \bcbar{7.5}
  \bcbar{3.2}
\end{bchart}
\end{quote}
Note that the height of the chart is not affected by this, nor is the size or shape of the text. The default width is 8cm.

In order to scale the complete chart (i.e. to change width and height in a proportional way), the \texttt{scale}-option can be used:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[max=10,step=2,scale=0.7]
  \bcbar{7.5}
  \bcbar{3.2}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[max=10,step=2,scale=0.7]
  \bcbar{7.5}
  \bcbar{3.2}
\end{bchart}
\end{quote}
Note that this does not change the size of the text either, only the bars and axes. To scale everything, including text, the chart environment has to be put into a \texttt{scalebox}-command instead:
\begin{quote}\small
\begin{verbatim}
\scalebox{0.7}{
\begin{bchart}[max=10,step=2]
  \bcbar{7.5}
  \bcbar{3.2}
\end{bchart}}
\end{verbatim}
\end{quote}
\begin{quote}
\scalebox{0.7}{
\begin{bchart}[max=10,step=2]
  \bcbar{7.5}
  \bcbar{3.2}
\end{bchart}}
\end{quote}

These three ways to change the size of the chart can all be combined to produce different kinds of scaling effects:
\begin{quote}\small
\begin{verbatim}
\scalebox{0.7}{
\begin{bchart}[max=10,step=2,width=4cm,scale=0.7]
  \bcbar{7.5}
  \bcbar{3.2}
\end{bchart}}
\end{verbatim}
\end{quote}
\begin{quote}
\scalebox{0.7}{
\begin{bchart}[max=10,step=2,width=4cm,scale=0.7]
  \bcbar{7.5}
  \bcbar{3.2}
\end{bchart}}
\end{quote}


\section{Known Issues}

There are some known issues with this package. Below, they are discussed and workarounds are presented.

When using fractions in the \texttt{step}-option, sometimes rounding errors become apparent:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[step=0.2,max=1]
  \bcbar{0.76}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[step=0.2,max=1]
  \bcbar{0.76}
\end{bchart}
\end{quote}
As a workaround, the \texttt{steps}-option can be used instead, where all marks must be listed explicitly:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[steps={0.2,0.4,0.6,0.8,1},max=1]
  \bcbar{0.76}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[steps={0.2,0.4,0.6,0.8,1},max=1]
  \bcbar{0.76}
\end{bchart}
\end{quote}

Another known issue is the fact that you can get a \LaTeX{} error message saying ``Dimension too large'' when using large numbers:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[max=20000]
  \bcbar{18000}
\end{bchart}
\end{verbatim}
\end{quote}
A possible workaround is moving the decimal point of all numbers by a couple of digits and manually adding the real value with the \texttt{value}-option:
\begin{quote}\small
\begin{verbatim}
\begin{bchart}[max=20]
  \bcbar[value=18000]{18}
\end{bchart}
\end{verbatim}
\end{quote}
\begin{quote}
\begin{bchart}[max=20]
  \bcbar[value=18000]{18}
\end{bchart}
\end{quote}
This is only a partial workaround though, since the numbers on the x-axis cannot be manually modified.

\end{document}