summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/bchart/bchart.sty
blob: 49a2f2b3fff48d43c725878c8535f0430dc958f6 (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
%-------------------------------------------------------------------------------
% bchart (LaTeX package)
%-------------------------------------------------------------------------------
% Version: 0.1.2
% Date: 22 August 2012
% Author: Tobias Kuhn
%-------------------------------------------------------------------------------

\ProvidesPackage{bchart}

\usepackage{ifthen}
\usepackage{tikz}
\usetikzlibrary{calc}


% Lengths

\newlength{\bcpos}
\newlength{\bcwidth}


% Global Arguments

\newcommand{\bcfontstyle}{\sffamily}


% Chart Arguments

\newcommand{\bcunit}{}
\newcommand{\bcmin}{}
\newcommand{\bcmax}{}
\newcommand{\bcstep}{}
\newcommand{\bcsteps}{}
\newcommand{\bcscale}{}
\newcommand{\bcplainchart}{}
\makeatletter
\define@key{bchart}{unit}{\renewcommand{\bcunit}{#1}}
\define@key{bchart}{width}{\setlength{\bcwidth}{#1}}
\define@key{bchart}{min}{\renewcommand{\bcmin}{#1}}
\define@key{bchart}{max}{\renewcommand{\bcmax}{#1}}
\define@key{bchart}{step}{\renewcommand{\bcstep}{#1}}
\define@key{bchart}{steps}{\renewcommand{\bcsteps}{#1}}
\define@key{bchart}{scale}{\renewcommand{\bcscale}{#1}}
\define@key{bchart}{plain}[false]{\renewcommand{\bcplainchart}{true}}
\makeatother


% Bar Arguments

\newcommand{\bcbarcolor}{}
\newcommand{\bcbartext}{}
\newcommand{\bcbarlabel}{}
\newcommand{\bcbarvalue}{}
\newcommand{\bcplainbar}{}
\makeatletter
\define@key{bcbar}{color}{\renewcommand{\bcbarcolor}{#1}}
\define@key{bcbar}{text}{\renewcommand{\bcbartext}{#1}}
\define@key{bcbar}{label}{\renewcommand{\bcbarlabel}{#1}}
\define@key{bcbar}{value}{\renewcommand{\bcbarvalue}{#1}}
\define@key{bcbar}{plain}[false]{\renewcommand{\bcplainbar}{true}}
\makeatother


% Skip Arguments

\newcommand{\bcskiplabel}{}
\makeatletter
\define@key{bcskip}{label}{\renewcommand{\bcskiplabel}{#1}}
\makeatother


% Bar Charts

\newenvironment{bchart}[1][]{%
  % Bars:
  \newcommand{\bcbar}[2][]{
    % Set defaults:
    \renewcommand{\bcbarcolor}{blue!20}
    \renewcommand{\bcbartext}{}
    \renewcommand{\bcbarlabel}{}
    \renewcommand{\bcbarvalue}{##2\bcunit}
    \renewcommand{\bcplainbar}{false}
    % Read parameters:
    \setkeys{bcbar}{##1}
    % Draw bar:
    \fill[color=\bcbarcolor,fill,draw] (0,\bcpos) rectangle ($##2-\bcmin*(\bcwidth/\bcrange,0) + (0,\bcpos-5mm)$);
    \draw (0,\bcpos) rectangle ($##2-\bcmin*(\bcwidth/\bcrange,0) + (0,\bcpos-5mm)$);
    \ifthenelse{\equal{\bcplainbar}{true}}{}{
      % Write value:
      \node[anchor=west] at ($##2-\bcmin*(\bcwidth/\bcrange,0) + (0,\bcpos-2.5mm)$) {\bcfontstyle\bcbarvalue};
    }
    % Write text:
    \node[anchor=west] at (0,\bcpos-2.5mm) {\bcfontstyle\bcbartext};
    % Write label:
    \node[anchor=east] at (0,\bcpos-2.5mm) {\bcfontstyle\bcbarlabel};
    % Move vertical position downward:
    \addtolength{\bcpos}{-5mm}
  }%
  % Labels:
  \newcommand{\bclabel}[1]{
    % Write label:
    \node[anchor=east] at (0,\bcpos) {\bcfontstyle##1};
  }%
  % General skips:
  \newcommand{\bcskip}[2][]{
    % Set defaults:
    \renewcommand{\bcskiplabel}{}
    % Read parameters:
    \setkeys{bcskip}{##1}
    % Write label:
    \node[anchor=east] at ($(0,\bcpos) - 0.5*(0,##2)$) {\bcfontstyle\bcskiplabel};
    % Move vertical position downward:
    \addtolength{\bcpos}{-##2}
  }%
  % Small skips:
  \renewcommand{\smallskip}[1][]{\bcskip[##1]{2.5mm}}%
  % Medium skips:
  \renewcommand{\medskip}[1][]{\bcskip[##1]{5.0mm}}%
  % Large skips:
  \renewcommand{\bigskip}[1][]{\bcskip[##1]{7.5mm}}%
  % X-axis label:
  \newcommand{\getbcxlabel}{}%
  \newcommand{\bcxlabel}[1]{
    \renewcommand{\getbcxlabel}{##1}
  }%
  \newcommand{\bcrange}{\bcstripunit{\dimexpr\bcmax pt-\bcmin pt\relax}}
  % Set defaults:
  \renewcommand{\bcunit}{}%
  \renewcommand{\bcmin}{0}%
  \renewcommand{\bcmax}{100}%
  \renewcommand{\bcstep}{\bcrange}%
  \renewcommand{\bcsteps}{0,\bcstep,...,\bcrange}%
  \renewcommand{\bcscale}{1}%
  \renewcommand{\bcplainchart}{false}%
  \setlength{\bcpos}{-2.5mm}%
  \setlength{\bcwidth}{8cm}%
  % Read parameters:
  \setkeys{bchart}{#1}%
  % Draw chart:
  \begin{tikzpicture}[scale=\bcscale]
}{
    % Draw axes:
    \addtolength{\bcpos}{-2.5mm}
    \draw (0,\bcpos) -- (\bcwidth,\bcpos);
    \draw (0,0) -- (0,\bcpos);
    % Draw scale:
    \ifthenelse{\equal{\bcplainchart}{true}}
    { % Scale off
      % Set position for x-axis label:
      \coordinate (labelpos) at (0,\bcpos-2mm);
    }{ % Scale on
      % Draw start value and set position for x-axis label:
      \draw (0,\bcpos) -- (0,\bcpos-1mm);
      \node[anchor=north] (n) at (0,\bcpos-1mm)
        {\bcfontstyle\bcstripunit{\dimexpr\bcmin pt\relax}\bcunit};
      \coordinate (labelpos) at (n.south);
      % Draw other values:
      \foreach \x in \bcsteps {
        \ifthenelse{\equal{\x}{0}}{}{
          \draw ($\x*(\bcwidth/\bcrange,0) + (0,\bcpos)$) -- ($\x*(\bcwidth/\bcrange,0) + (0,\bcpos-1mm)$);
          \node[anchor=north] at ($\x*(\bcwidth/\bcrange,0) + (0,\bcpos-1mm)$)
            {\bcfontstyle\bcstripunit{\dimexpr\bcmin pt+\x pt\relax}\bcunit};
        }
      }
    }
    % Write x-axis label:
    \ifthenelse{\equal{\getbcxlabel}{}}{}{
      \node[anchor=north,inner sep=0.5mm] at ($0.5*(\bcwidth,0) + (labelpos)$) {\bcfontstyle\getbcxlabel};
    }
  \end{tikzpicture}%
}


% Auxiliary commands:

\makeatletter
\newcommand*{\bcstripunit}[1]{\strip@pt#1}
\makeatother