gantt.sty 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. \RequirePackage{tikz,pgffor,xkeyval,ifthen,calc,forloop}
  35. \usetikzlibrary{patterns}
  36. \usetikzlibrary{arrows}
  37. \newcounter{ganttnum}
  38. \newcounter{ganttwidth}
  39. \newlength{\ganttlastx}
  40. \setlength{\ganttlastx}{0cm}
  41. \newlength{\nextganttlastx}
  42. \newlength{\gantttmpa}
  43. \newlength{\gantttmpb}
  44. \newlength{\ganttunitlength}
  45. \setlength{\ganttunitlength}{1cm}
  46. \newlength{\titleunitlength}
  47. \setlength{\titleunitlength}{1cm}
  48. \newcounter{gantttitlenum}
  49. \define@key{ganttx}{xunitlength}{%
  50. \setlength{\ganttunitlength}{#1}
  51. }
  52. \define@boolkey{ganttx}{drawledgerline}{}
  53. \define@key{ganttx}{fontsize}{\def\ganttfontsize{#1}}
  54. \define@key{ganttx}{titlefontsize}{\def\gantttitlefontsize{#1}}
  55. \presetkeys{ganttx}{drawledgerline=false,xunitlength=1cm,titlefontsize=\small,fontsize=\normalsize}{}
  56. \define@key{ganttbarx}{pattern}{\def\ganttbarpattern{#1}}
  57. \define@key{ganttbarx}{color}{\def\ganttbarcolor{#1}}
  58. \presetkeys{ganttbarx}{pattern=north west lines,color=black}{}
  59. % The gantt environment draws the canvas of a gantt figure (realized as tikzpicture)
  60. % The usage is \begin{gantt}[...]{no of Tasks to plot}{no of time slots}
  61. % The optional argument [...] can be filled in a key=value syntax, using one or more of the following keys:
  62. %
  63. % xunitlength - length of one time slot (default: 1 cm)
  64. % fontsize - fontsize of labels (default: \normalsize)
  65. % titlefontsize - fontsize of title section (default: \small)
  66. % drawledgerline - Switch to enable/disable the drawing of horizontal ledger lines (default value: false)
  67. \newenvironment{gantt}[3][]{%
  68. \begin{tikzpicture}[draw=black, yscale=.7,xscale=1]
  69. \tikzstyle{time}=[coordinate]
  70. \setkeys{ganttx}{#1}{%
  71. \setcounter{ganttwidth}{#3}
  72. \setcounter{ganttnum}{0}
  73. \newcount\ganttx
  74. \newcount\ganttheight
  75. \ganttheight=#2 %define the number of Y ticks
  76. \advance\ganttheight by 1
  77. \def\ganttxstringtop{};
  78. \def\ganttxstringbottom{};
  79. \ganttx=0
  80. \draw (0,0.5) node[above] {\ganttxstringtop} -- (0,1.4-\ganttheight) node[below] {\ganttxstringbottom};
  81. \draw (\value{ganttwidth}*\ganttunitlength,0.5) node[above] {\ganttxstringtop} -- (\value{ganttwidth}*\ganttunitlength,1.4-\ganttheight) node[below] {\ganttxstringbottom};
  82. %draw grid
  83. \foreach \t in {1,2,...,\value{ganttwidth}}{
  84. \ganttx=1
  85. \multiply\ganttx by \t
  86. \draw[dotted] (\ganttunitlength*\t,-0.5) node[above] {\ganttxstringtop} -- (\ganttunitlength*\t,1.4-\ganttheight) node[below] {\ganttxstringbottom};
  87. }
  88. %draw x axis
  89. \draw[] (0,-#2+0.4) -- (\value{ganttwidth}*\ganttunitlength,-#2+0.4);
  90. }
  91. }{\end{tikzpicture}}
  92. % ganttitle is the environment for drawing the title section
  93. \newenvironment{ganttitle}[1][]{%
  94. \setlength{\ganttlastx}{0 cm}
  95. }{%
  96. \setlength{\ganttlastx}{0 cm}
  97. \addtocounter{ganttnum}{-1}%
  98. }
  99. % \titleelement draws one element of the title
  100. % usage: \titleelement{label}{length}
  101. \newcommand{\titleelement}[2]{
  102. \setlength{\gantttmpa}{\ganttunitlength*#2}
  103. \divide\gantttmpa by 2;
  104. \draw (\the\ganttlastx,\value{ganttnum}) rectangle (\the\ganttlastx+#2*\ganttunitlength,\value{ganttnum}+0.6);
  105. \node [text height=1.5ex,text depth=0.7ex] at (\the\ganttlastx+\the\gantttmpa,\value{ganttnum}+0.25) {%
  106. \gantttitlefontsize #1%
  107. };
  108. \setlength{\ganttlastx}{\ganttlastx+\ganttunitlength*\real{#2}}
  109. }
  110. % \numtitle draws a numbered sequence of title elements
  111. % usage: \numtitle{start number}{increment}{end number}{length of each title element}
  112. \newcommand{\numtitle}[4]{
  113. \forloop[#2]{gantttitlenum}{#1}{\(\value{gantttitlenum} < #3\) \or \(\value{gantttitlenum} = #3\)}%
  114. {%
  115. \titleelement{\thegantttitlenum}{#4}
  116. }
  117. }
  118. % \ganttbar draws a single, unconnected bar for representing a task
  119. % usage: \ganttbar[pattern=<pattern>,color=<color>]{label}{start}{length}
  120. % where the optional comma separated arguments are:
  121. % pattern - is a tikz pattern (e.g. north west lines (default), north east lines, crosshatch, crosshatch dots, grid, ...)
  122. % color - a tikz color of the pattern (e.g. red, green blue gray, dark gray)
  123. % for more information see the tikz documentation
  124. \newcommand{\ganttbar}[4][]{
  125. \setlength{\gantttmpa}{#3\ganttunitlength}
  126. \setlength{\gantttmpb}{#4\ganttunitlength}
  127. \setlength{\gantttmpb}{\gantttmpa+\gantttmpb}
  128. \ifKV@ganttx@drawledgerline
  129. \draw[dotted] (0,\value{ganttnum}-0.2) -- (\value{ganttwidth}*\ganttunitlength,\value{ganttnum}-0.2);
  130. \fi
  131. \node at (0,\value{ganttnum}) [anchor=base east] {%
  132. \ganttfontsize #2%
  133. };
  134. \setkeys{ganttbarx}{#1}{%
  135. \draw[pattern=\ganttbarpattern,pattern color=\ganttbarcolor] (\gantttmpa,\value{ganttnum}+0.1) rectangle (\gantttmpb,\value{ganttnum}+0.5);
  136. }
  137. \setlength{\ganttlastx}{\gantttmpb}
  138. \addtocounter{ganttnum}{-1}
  139. }
  140. % \ganttcon draws an arrow between to bars with specified coordinates
  141. % usage: \ganttcon{startx}{starty}{endx}{endy}
  142. \newcommand{\ganttcon}[4]{
  143. \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);
  144. }
  145. % \ganttbarcon draws a single bar *and* connects the bar with the previous bar for
  146. % consecutive tasks
  147. % usage: \ganttbarcon[pattern=<pattern>,color=<color>]{label}{start}{length}
  148. % where the optional pattern argument are the same as for \ganttbar
  149. \newcommand{\ganttbarcon}[4][]{
  150. \setlength{\nextganttlastx}{\the\ganttlastx+1\ganttunitlength}
  151. \ifdim\nextganttlastx>#3\ganttunitlength
  152. \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);
  153. \else
  154. \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);
  155. \fi
  156. \ganttbar[#1]{#2}{#3}{#4}
  157. }
  158. % \ganttgroup draws a bar to group tasks
  159. % usage: \ganttgroup{label}{start}{length}
  160. \newcommand{\ganttgroup}[3]{%
  161. \setlength{\gantttmpa}{#2\ganttunitlength}
  162. \setlength{\gantttmpb}{#3\ganttunitlength}
  163. \setlength{\gantttmpb}{\gantttmpa+\gantttmpb}
  164. \ifKV@ganttx@drawledgerline
  165. \draw[dotted] (0,\value{ganttnum}-0.2) -- (\value{ganttwidth}*\ganttunitlength,\value{ganttnum}-0.2);
  166. \fi
  167. \node at (0,\value{ganttnum}) [anchor=base east] {%
  168. \ganttfontsize\bfseries #1%
  169. };
  170. \fill[black] (\gantttmpa-0.14cm,\value{ganttnum}+0.2) rectangle (\gantttmpb+0.14cm,\value{ganttnum}+0.4);
  171. \draw[diamond-diamond] (\gantttmpa-0.14cm,\value{ganttnum}+0.2) -- (\gantttmpb+0.14cm,\value{ganttnum}+0.2);
  172. \setlength{\ganttlastx}{\gantttmpb}
  173. \addtocounter{ganttnum}{-1}
  174. }
  175. % \ganttmilestone, draw a diamond to represent a milestone
  176. % usage: \ganttgroup[fill=<color>]{label}{start}
  177. % color - a tikz color of the pattern (e.g. red, green blue gray, dark gray)
  178. % for more information see the tikz documentation
  179. % Declaring layers to abvoid superposition when you connect a \ganttmileston with a task
  180. \pgfdeclarelayer{background}
  181. \pgfdeclarelayer{foreground}
  182. \pgfsetlayers{background,main,foreground}
  183. \newcommand{\ganttmilestone}[3][]{
  184. \setlength{\gantttmpa}{#3\ganttunitlength}
  185. \setlength{\gantttmpb}{0\ganttunitlength}
  186. \setlength{\gantttmpb}{\gantttmpa+\gantttmpb}
  187. \ifKV@ganttx@drawledgerline
  188. \draw[dotted] (0,\value{ganttnum}-0.2) -- (\value{ganttwidth}*\ganttunitlength,\value{ganttnum}-0.2);
  189. \fi
  190. \node at (0,\value{ganttnum}) [anchor=base east] {%
  191. \ganttfontsize #2%
  192. };
  193. \setkeys{ganttbarx}{#1}{%
  194. \pgfonlayer{foreground}
  195. \draw[diamond-, color=\ganttbarcolor] (\gantttmpa,\value{ganttnum}+0.1) -- (\gantttmpb,\value{ganttnum}+0.4);
  196. \endpgfonlayer
  197. }
  198. \setlength{\ganttlastx}{\gantttmpb}
  199. \addtocounter{ganttnum}{-1}
  200. }
  201. % \ganttmilestonecon draws a single diamond *and* connects the diamond with the previous task
  202. % usage: \ganttmilestonecon[color=<color>]{label}{start}{length}
  203. % where the optional color argument are the same as for \ganttbar and \ganttmilestone
  204. \newcommand{\ganttmilestonecon}[3][]{
  205. \setlength{\nextganttlastx}{\the\ganttlastx+1\ganttunitlength}
  206. \ifdim\nextganttlastx>#3\ganttunitlength
  207. \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);
  208. %
  209. \else
  210. \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);
  211. \fi
  212. % \fi
  213. \ganttmilestone[#1]{#2}{#3}
  214. }
  215. \endinput