Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132928 views
License: OTHER
1
\ProvidesPackage{gantt}[2011/03/20 Some macros for drawing gantt diagrams using tkiz v.1.3]
2
% Some macros for drawing gantt diagrams using tkiz.
3
%
4
% Martin Kumm, 17.05.2010 (Version 1.0)
5
% Alan Munn, Martin Kumm, 14.03.2011 (Version 1.2)
6
% Andrés Silva Marambio, 20.03.2011 (Version 1.3)
7
%
8
% Update 20.03.2011, Version 1.3
9
%
10
% changes from v1.2:
11
% change \ProvidesPackage{gantt.sty} for \ProvidesPackage{gantt}
12
% add \bfseries for the command \ganttgroup
13
% add two newcommands for doing Gantt Milestones: \ganttmilestone and \ganttmilestonecon
14
% usage: \ganttmilestone[color=<color>]{label}{start} - Create a gantt milestone using a diamond
15
% usage: \ganttmilestonecon[color=<color>]{label}{start} - Create a gantt milestone using a diamond and conect with the previuos itemg
16
% fixed the issue when \ganttbarcon or \ganttmilestonecon connect in decimal positions
17
%
18
% Update 15.03.2011, Version 1.2
19
%
20
% changes from v1.1:
21
% px sizes has ben changed ot pt due to problems with some LaTeX distributions (thanks to Michael Kubovy and Loïc Le Guyader!)
22
% Correction of the text aligned in \titleelement (thanks to Josh Hykes!)
23
% \unitlength was changed to \ganttunitlength (thanks to Alan Munn!)
24
% \RequirePackage was added (thanks to Alan Munn!)
25
%
26
% Update 05.08.2010, Version 1.1
27
%
28
% changes from v1.0:
29
% \ganttbar and \ganttbarcon have been extended with the xkeyval syntax
30
% The pattern of the bar is now independent of the color and can be set by
31
% e.g. \ganttbar[pattern=horizontal lines,color=blue]{...}{1}{1}
32
% This syntax is incompatible with the previous ones!
33
% replace \ganttbar[<pattern>] with \ganttbar[pattern=<pattern>]
34
35
\RequirePackage{tikz,pgffor,xkeyval,ifthen,calc,forloop}
36
37
\usetikzlibrary{patterns}
38
\usetikzlibrary{arrows}
39
40
\newcounter{ganttnum}
41
\newcounter{ganttwidth}
42
43
\newlength{\ganttlastx}
44
\setlength{\ganttlastx}{0cm}
45
46
\newlength{\nextganttlastx}
47
48
\newlength{\gantttmpa}
49
\newlength{\gantttmpb}
50
\newlength{\ganttunitlength}
51
52
\setlength{\ganttunitlength}{1cm}
53
54
\newlength{\titleunitlength}
55
\setlength{\titleunitlength}{1cm}
56
57
\newcounter{gantttitlenum}
58
59
\define@key{ganttx}{xunitlength}{%
60
\setlength{\ganttunitlength}{#1}
61
}
62
\define@boolkey{ganttx}{drawledgerline}{}
63
64
\define@key{ganttx}{fontsize}{\def\ganttfontsize{#1}}
65
\define@key{ganttx}{titlefontsize}{\def\gantttitlefontsize{#1}}
66
67
\presetkeys{ganttx}{drawledgerline=false,xunitlength=1cm,titlefontsize=\small,fontsize=\normalsize}{}
68
69
\define@key{ganttbarx}{pattern}{\def\ganttbarpattern{#1}}
70
\define@key{ganttbarx}{color}{\def\ganttbarcolor{#1}}
71
72
\presetkeys{ganttbarx}{pattern=north west lines,color=black}{}
73
74
75
% The gantt environment draws the canvas of a gantt figure (realized as tikzpicture)
76
% The usage is \begin{gantt}[...]{no of Tasks to plot}{no of time slots}
77
% The optional argument [...] can be filled in a key=value syntax, using one or more of the following keys:
78
%
79
% xunitlength - length of one time slot (default: 1 cm)
80
% fontsize - fontsize of labels (default: \normalsize)
81
% titlefontsize - fontsize of title section (default: \small)
82
% drawledgerline - Switch to enable/disable the drawing of horizontal ledger lines (default value: false)
83
84
\newenvironment{gantt}[3][]{%
85
\begin{tikzpicture}[draw=black, yscale=.7,xscale=1]
86
\tikzstyle{time}=[coordinate]
87
\setkeys{ganttx}{#1}{%
88
\setcounter{ganttwidth}{#3}
89
90
\setcounter{ganttnum}{0}
91
\newcount\ganttx
92
\newcount\ganttheight
93
\ganttheight=#2 %define the number of Y ticks
94
\advance\ganttheight by 1
95
96
\def\ganttxstringtop{};
97
\def\ganttxstringbottom{};
98
99
\ganttx=0
100
\draw (0,0.5) node[above] {\ganttxstringtop} -- (0,1.4-\ganttheight) node[below] {\ganttxstringbottom};
101
\draw (\value{ganttwidth}*\ganttunitlength,0.5) node[above] {\ganttxstringtop} -- (\value{ganttwidth}*\ganttunitlength,1.4-\ganttheight) node[below] {\ganttxstringbottom};
102
103
%draw grid
104
\foreach \t in {1,2,...,\value{ganttwidth}}{
105
\ganttx=1
106
\multiply\ganttx by \t
107
108
\draw[dotted] (\ganttunitlength*\t,-0.5) node[above] {\ganttxstringtop} -- (\ganttunitlength*\t,1.4-\ganttheight) node[below] {\ganttxstringbottom};
109
}
110
%draw x axis
111
\draw[] (0,-#2+0.4) -- (\value{ganttwidth}*\ganttunitlength,-#2+0.4);
112
}
113
}{\end{tikzpicture}}
114
115
116
% ganttitle is the environment for drawing the title section
117
\newenvironment{ganttitle}[1][]{%
118
\setlength{\ganttlastx}{0 cm}
119
}{%
120
\setlength{\ganttlastx}{0 cm}
121
\addtocounter{ganttnum}{-1}%
122
}
123
124
% \titleelement draws one element of the title
125
% usage: \titleelement{label}{length}
126
\newcommand{\titleelement}[2]{
127
\setlength{\gantttmpa}{\ganttunitlength*#2}
128
\divide\gantttmpa by 2;
129
130
\draw (\the\ganttlastx,\value{ganttnum}) rectangle (\the\ganttlastx+#2*\ganttunitlength,\value{ganttnum}+0.6);
131
\node [text height=1.5ex,text depth=0.7ex] at (\the\ganttlastx+\the\gantttmpa,\value{ganttnum}+0.25) {%
132
\gantttitlefontsize #1%
133
};
134
\setlength{\ganttlastx}{\ganttlastx+\ganttunitlength*\real{#2}}
135
}
136
137
138
% \numtitle draws a numbered sequence of title elements
139
% usage: \numtitle{start number}{increment}{end number}{length of each title element}
140
\newcommand{\numtitle}[4]{
141
\forloop[#2]{gantttitlenum}{#1}{\(\value{gantttitlenum} < #3\) \or \(\value{gantttitlenum} = #3\)}%
142
{%
143
\titleelement{\thegantttitlenum}{#4}
144
}
145
}
146
147
% \ganttbar draws a single, unconnected bar for representing a task
148
% usage: \ganttbar[pattern=<pattern>,color=<color>]{label}{start}{length}
149
% where the optional comma separated arguments are:
150
% pattern - is a tikz pattern (e.g. north west lines (default), north east lines, crosshatch, crosshatch dots, grid, ...)
151
% color - a tikz color of the pattern (e.g. red, green blue gray, dark gray)
152
% for more information see the tikz documentation
153
\newcommand{\ganttbar}[4][]{
154
\setlength{\gantttmpa}{#3\ganttunitlength}
155
\setlength{\gantttmpb}{#4\ganttunitlength}
156
\setlength{\gantttmpb}{\gantttmpa+\gantttmpb}
157
158
\ifKV@ganttx@drawledgerline
159
\draw[dotted] (0,\value{ganttnum}-0.2) -- (\value{ganttwidth}*\ganttunitlength,\value{ganttnum}-0.2);
160
\fi
161
\node at (0,\value{ganttnum}) [anchor=base east] {%
162
\ganttfontsize #2%
163
};
164
\setkeys{ganttbarx}{#1}{%
165
\draw[pattern=\ganttbarpattern,pattern color=\ganttbarcolor] (\gantttmpa,\value{ganttnum}+0.1) rectangle (\gantttmpb,\value{ganttnum}+0.5);
166
}
167
\setlength{\ganttlastx}{\gantttmpb}
168
\addtocounter{ganttnum}{-1}
169
}
170
171
172
% \ganttcon draws an arrow between to bars with specified coordinates
173
% usage: \ganttcon{startx}{starty}{endx}{endy}
174
\newcommand{\ganttcon}[4]{
175
\draw[-latex,rounded corners=1pt] (#1\ganttunitlength,-#2+0.1 + 0.2) -- (#1\ganttunitlength+0.4*\ganttunitlength,-#2+0.1+0.2) -- (#1\ganttunitlength+0.4*\ganttunitlength,-#2-0.4+0.2) -- (#1\ganttunitlength-0.4*\ganttunitlength,-#2-0.4+0.2) -- (#1\ganttunitlength-0.4*\ganttunitlength,-#4+0.1+0.2) -- (#3\ganttunitlength,-#4+0.1+0.2);
176
}
177
178
179
% \ganttbarcon draws a single bar *and* connects the bar with the previous bar for
180
% consecutive tasks
181
% usage: \ganttbarcon[pattern=<pattern>,color=<color>]{label}{start}{length}
182
% where the optional pattern argument are the same as for \ganttbar
183
\newcommand{\ganttbarcon}[4][]{
184
\setlength{\nextganttlastx}{\the\ganttlastx+1\ganttunitlength}
185
\ifdim\nextganttlastx>#3\ganttunitlength
186
\draw[-latex,rounded corners=1pt] (\the\ganttlastx,\value{ganttnum}+1.1 + 0.2) -- (\the\ganttlastx+0.4*\ganttunitlength,\value{ganttnum}+1.1+0.2) -- (\the\ganttlastx+0.4*\ganttunitlength,\value{ganttnum}+0.6+0.2) -- (\the\ganttlastx-0.4*\ganttunitlength,\value{ganttnum}+0.6+0.2) -- (\the\ganttlastx-0.4*\ganttunitlength,\value{ganttnum}+0.1+0.2) -- (#3\ganttunitlength,\value{ganttnum}+0.1+0.2);
187
\else
188
\draw[-latex,rounded corners=1pt] (\the\ganttlastx,\value{ganttnum}+1.1 + 0.2) -- (\the\ganttlastx+0.4*\ganttunitlength,\value{ganttnum}+1.1+0.2) -- (\the\ganttlastx+0.4*\ganttunitlength,\value{ganttnum}+0.1+0.2) -- (#3\ganttunitlength,\value{ganttnum}+0.1+0.2);
189
\fi
190
\ganttbar[#1]{#2}{#3}{#4}
191
}
192
193
194
% \ganttgroup draws a bar to group tasks
195
% usage: \ganttgroup{label}{start}{length}
196
\newcommand{\ganttgroup}[3]{%
197
\setlength{\gantttmpa}{#2\ganttunitlength}
198
\setlength{\gantttmpb}{#3\ganttunitlength}
199
\setlength{\gantttmpb}{\gantttmpa+\gantttmpb}
200
201
\ifKV@ganttx@drawledgerline
202
\draw[dotted] (0,\value{ganttnum}-0.2) -- (\value{ganttwidth}*\ganttunitlength,\value{ganttnum}-0.2);
203
\fi
204
\node at (0,\value{ganttnum}) [anchor=base east] {%
205
\ganttfontsize\bfseries #1%
206
};
207
\fill[black] (\gantttmpa-0.14cm,\value{ganttnum}+0.2) rectangle (\gantttmpb+0.14cm,\value{ganttnum}+0.4);
208
\draw[diamond-diamond] (\gantttmpa-0.14cm,\value{ganttnum}+0.2) -- (\gantttmpb+0.14cm,\value{ganttnum}+0.2);
209
210
\setlength{\ganttlastx}{\gantttmpb}
211
\addtocounter{ganttnum}{-1}
212
}
213
214
215
% \ganttmilestone, draw a diamond to represent a milestone
216
% usage: \ganttgroup[fill=<color>]{label}{start}
217
% color - a tikz color of the pattern (e.g. red, green blue gray, dark gray)
218
% for more information see the tikz documentation
219
220
% Declaring layers to abvoid superposition when you connect a \ganttmileston with a task
221
\pgfdeclarelayer{background}
222
\pgfdeclarelayer{foreground}
223
\pgfsetlayers{background,main,foreground}
224
225
\newcommand{\ganttmilestone}[3][]{
226
\setlength{\gantttmpa}{#3\ganttunitlength}
227
\setlength{\gantttmpb}{0\ganttunitlength}
228
\setlength{\gantttmpb}{\gantttmpa+\gantttmpb}
229
230
\ifKV@ganttx@drawledgerline
231
\draw[dotted] (0,\value{ganttnum}-0.2) -- (\value{ganttwidth}*\ganttunitlength,\value{ganttnum}-0.2);
232
\fi
233
\node at (0,\value{ganttnum}) [anchor=base east] {%
234
\ganttfontsize #2%
235
};
236
\setkeys{ganttbarx}{#1}{%
237
\pgfonlayer{foreground}
238
\draw[diamond-, color=\ganttbarcolor] (\gantttmpa,\value{ganttnum}+0.1) -- (\gantttmpb,\value{ganttnum}+0.4);
239
\endpgfonlayer
240
}
241
\setlength{\ganttlastx}{\gantttmpb}
242
\addtocounter{ganttnum}{-1}
243
}
244
245
246
% \ganttmilestonecon draws a single diamond *and* connects the diamond with the previous task
247
% usage: \ganttmilestonecon[color=<color>]{label}{start}{length}
248
% where the optional color argument are the same as for \ganttbar and \ganttmilestone
249
250
\newcommand{\ganttmilestonecon}[3][]{
251
\setlength{\nextganttlastx}{\the\ganttlastx+1\ganttunitlength}
252
\ifdim\nextganttlastx>#3\ganttunitlength
253
\draw[-latex,rounded corners=1pt] (\the\ganttlastx,\value{ganttnum}+1.1 + 0.2) -- (\the\ganttlastx+0.4*\ganttunitlength,\value{ganttnum}+1.1+0.2) -- (\the\ganttlastx+0.4*\ganttunitlength,\value{ganttnum}+0.6+0.2) -- (\the\ganttlastx-0.4*\ganttunitlength-1.8,\value{ganttnum}+0.6+0.2) -- (\the\ganttlastx-0.4*\ganttunitlength-1.8,\value{ganttnum}+0.1+0.2) -- (#3\ganttunitlength-1.8,\value{ganttnum}+0.1+0.2);
254
%
255
\else
256
\draw[-latex,rounded corners=1pt] (\the\ganttlastx,\value{ganttnum}+1.1 + 0.2) -- (\the\ganttlastx+0.4*\ganttunitlength,\value{ganttnum}+1.1+0.2) -- (\the\ganttlastx+0.4*\ganttunitlength,\value{ganttnum}+0.1+0.2) -- (#3\ganttunitlength-1.8,\value{ganttnum}+0.1+0.2);
257
\fi
258
% \fi
259
\ganttmilestone[#1]{#2}{#3}
260
}
261
262
263
\endinput
264
265