📚 The CoCalc Library - books, templates and other resources
License: OTHER
\ProvidesPackage{gantt}[2011/03/20 Some macros for drawing gantt diagrams using tkiz v.1.3]1% Some macros for drawing gantt diagrams using tkiz.2%3% Martin Kumm, 17.05.2010 (Version 1.0)4% Alan Munn, Martin Kumm, 14.03.2011 (Version 1.2)5% Andrés Silva Marambio, 20.03.2011 (Version 1.3)6%7% Update 20.03.2011, Version 1.38%9% changes from v1.2:10% change \ProvidesPackage{gantt.sty} for \ProvidesPackage{gantt}11% add \bfseries for the command \ganttgroup12% add two newcommands for doing Gantt Milestones: \ganttmilestone and \ganttmilestonecon13% usage: \ganttmilestone[color=<color>]{label}{start} - Create a gantt milestone using a diamond14% usage: \ganttmilestonecon[color=<color>]{label}{start} - Create a gantt milestone using a diamond and conect with the previuos itemg15% fixed the issue when \ganttbarcon or \ganttmilestonecon connect in decimal positions16%17% Update 15.03.2011, Version 1.218%19% changes from v1.1:20% px sizes has ben changed ot pt due to problems with some LaTeX distributions (thanks to Michael Kubovy and Loïc Le Guyader!)21% Correction of the text aligned in \titleelement (thanks to Josh Hykes!)22% \unitlength was changed to \ganttunitlength (thanks to Alan Munn!)23% \RequirePackage was added (thanks to Alan Munn!)24%25% Update 05.08.2010, Version 1.126%27% changes from v1.0:28% \ganttbar and \ganttbarcon have been extended with the xkeyval syntax29% The pattern of the bar is now independent of the color and can be set by30% e.g. \ganttbar[pattern=horizontal lines,color=blue]{...}{1}{1}31% This syntax is incompatible with the previous ones!32% replace \ganttbar[<pattern>] with \ganttbar[pattern=<pattern>]3334\RequirePackage{tikz,pgffor,xkeyval,ifthen,calc,forloop}3536\usetikzlibrary{patterns}37\usetikzlibrary{arrows}3839\newcounter{ganttnum}40\newcounter{ganttwidth}4142\newlength{\ganttlastx}43\setlength{\ganttlastx}{0cm}4445\newlength{\nextganttlastx}4647\newlength{\gantttmpa}48\newlength{\gantttmpb}49\newlength{\ganttunitlength}5051\setlength{\ganttunitlength}{1cm}5253\newlength{\titleunitlength}54\setlength{\titleunitlength}{1cm}5556\newcounter{gantttitlenum}5758\define@key{ganttx}{xunitlength}{%59\setlength{\ganttunitlength}{#1}60}61\define@boolkey{ganttx}{drawledgerline}{}6263\define@key{ganttx}{fontsize}{\def\ganttfontsize{#1}}64\define@key{ganttx}{titlefontsize}{\def\gantttitlefontsize{#1}}6566\presetkeys{ganttx}{drawledgerline=false,xunitlength=1cm,titlefontsize=\small,fontsize=\normalsize}{}6768\define@key{ganttbarx}{pattern}{\def\ganttbarpattern{#1}}69\define@key{ganttbarx}{color}{\def\ganttbarcolor{#1}}7071\presetkeys{ganttbarx}{pattern=north west lines,color=black}{}727374% The gantt environment draws the canvas of a gantt figure (realized as tikzpicture)75% The usage is \begin{gantt}[...]{no of Tasks to plot}{no of time slots}76% The optional argument [...] can be filled in a key=value syntax, using one or more of the following keys:77%78% xunitlength - length of one time slot (default: 1 cm)79% fontsize - fontsize of labels (default: \normalsize)80% titlefontsize - fontsize of title section (default: \small)81% drawledgerline - Switch to enable/disable the drawing of horizontal ledger lines (default value: false)8283\newenvironment{gantt}[3][]{%84\begin{tikzpicture}[draw=black, yscale=.7,xscale=1]85\tikzstyle{time}=[coordinate]86\setkeys{ganttx}{#1}{%87\setcounter{ganttwidth}{#3}8889\setcounter{ganttnum}{0}90\newcount\ganttx91\newcount\ganttheight92\ganttheight=#2 %define the number of Y ticks93\advance\ganttheight by 19495\def\ganttxstringtop{};96\def\ganttxstringbottom{};9798\ganttx=099\draw (0,0.5) node[above] {\ganttxstringtop} -- (0,1.4-\ganttheight) node[below] {\ganttxstringbottom};100\draw (\value{ganttwidth}*\ganttunitlength,0.5) node[above] {\ganttxstringtop} -- (\value{ganttwidth}*\ganttunitlength,1.4-\ganttheight) node[below] {\ganttxstringbottom};101102%draw grid103\foreach \t in {1,2,...,\value{ganttwidth}}{104\ganttx=1105\multiply\ganttx by \t106107\draw[dotted] (\ganttunitlength*\t,-0.5) node[above] {\ganttxstringtop} -- (\ganttunitlength*\t,1.4-\ganttheight) node[below] {\ganttxstringbottom};108}109%draw x axis110\draw[] (0,-#2+0.4) -- (\value{ganttwidth}*\ganttunitlength,-#2+0.4);111}112}{\end{tikzpicture}}113114115% ganttitle is the environment for drawing the title section116\newenvironment{ganttitle}[1][]{%117\setlength{\ganttlastx}{0 cm}118}{%119\setlength{\ganttlastx}{0 cm}120\addtocounter{ganttnum}{-1}%121}122123% \titleelement draws one element of the title124% usage: \titleelement{label}{length}125\newcommand{\titleelement}[2]{126\setlength{\gantttmpa}{\ganttunitlength*#2}127\divide\gantttmpa by 2;128129\draw (\the\ganttlastx,\value{ganttnum}) rectangle (\the\ganttlastx+#2*\ganttunitlength,\value{ganttnum}+0.6);130\node [text height=1.5ex,text depth=0.7ex] at (\the\ganttlastx+\the\gantttmpa,\value{ganttnum}+0.25) {%131\gantttitlefontsize #1%132};133\setlength{\ganttlastx}{\ganttlastx+\ganttunitlength*\real{#2}}134}135136137% \numtitle draws a numbered sequence of title elements138% usage: \numtitle{start number}{increment}{end number}{length of each title element}139\newcommand{\numtitle}[4]{140\forloop[#2]{gantttitlenum}{#1}{\(\value{gantttitlenum} < #3\) \or \(\value{gantttitlenum} = #3\)}%141{%142\titleelement{\thegantttitlenum}{#4}143}144}145146% \ganttbar draws a single, unconnected bar for representing a task147% usage: \ganttbar[pattern=<pattern>,color=<color>]{label}{start}{length}148% where the optional comma separated arguments are:149% pattern - is a tikz pattern (e.g. north west lines (default), north east lines, crosshatch, crosshatch dots, grid, ...)150% color - a tikz color of the pattern (e.g. red, green blue gray, dark gray)151% for more information see the tikz documentation152\newcommand{\ganttbar}[4][]{153\setlength{\gantttmpa}{#3\ganttunitlength}154\setlength{\gantttmpb}{#4\ganttunitlength}155\setlength{\gantttmpb}{\gantttmpa+\gantttmpb}156157\ifKV@ganttx@drawledgerline158\draw[dotted] (0,\value{ganttnum}-0.2) -- (\value{ganttwidth}*\ganttunitlength,\value{ganttnum}-0.2);159\fi160\node at (0,\value{ganttnum}) [anchor=base east] {%161\ganttfontsize #2%162};163\setkeys{ganttbarx}{#1}{%164\draw[pattern=\ganttbarpattern,pattern color=\ganttbarcolor] (\gantttmpa,\value{ganttnum}+0.1) rectangle (\gantttmpb,\value{ganttnum}+0.5);165}166\setlength{\ganttlastx}{\gantttmpb}167\addtocounter{ganttnum}{-1}168}169170171% \ganttcon draws an arrow between to bars with specified coordinates172% usage: \ganttcon{startx}{starty}{endx}{endy}173\newcommand{\ganttcon}[4]{174\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);175}176177178% \ganttbarcon draws a single bar *and* connects the bar with the previous bar for179% consecutive tasks180% usage: \ganttbarcon[pattern=<pattern>,color=<color>]{label}{start}{length}181% where the optional pattern argument are the same as for \ganttbar182\newcommand{\ganttbarcon}[4][]{183\setlength{\nextganttlastx}{\the\ganttlastx+1\ganttunitlength}184\ifdim\nextganttlastx>#3\ganttunitlength185\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);186\else187\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);188\fi189\ganttbar[#1]{#2}{#3}{#4}190}191192193% \ganttgroup draws a bar to group tasks194% usage: \ganttgroup{label}{start}{length}195\newcommand{\ganttgroup}[3]{%196\setlength{\gantttmpa}{#2\ganttunitlength}197\setlength{\gantttmpb}{#3\ganttunitlength}198\setlength{\gantttmpb}{\gantttmpa+\gantttmpb}199200\ifKV@ganttx@drawledgerline201\draw[dotted] (0,\value{ganttnum}-0.2) -- (\value{ganttwidth}*\ganttunitlength,\value{ganttnum}-0.2);202\fi203\node at (0,\value{ganttnum}) [anchor=base east] {%204\ganttfontsize\bfseries #1%205};206\fill[black] (\gantttmpa-0.14cm,\value{ganttnum}+0.2) rectangle (\gantttmpb+0.14cm,\value{ganttnum}+0.4);207\draw[diamond-diamond] (\gantttmpa-0.14cm,\value{ganttnum}+0.2) -- (\gantttmpb+0.14cm,\value{ganttnum}+0.2);208209\setlength{\ganttlastx}{\gantttmpb}210\addtocounter{ganttnum}{-1}211}212213214% \ganttmilestone, draw a diamond to represent a milestone215% usage: \ganttgroup[fill=<color>]{label}{start}216% color - a tikz color of the pattern (e.g. red, green blue gray, dark gray)217% for more information see the tikz documentation218219% Declaring layers to abvoid superposition when you connect a \ganttmileston with a task220\pgfdeclarelayer{background}221\pgfdeclarelayer{foreground}222\pgfsetlayers{background,main,foreground}223224\newcommand{\ganttmilestone}[3][]{225\setlength{\gantttmpa}{#3\ganttunitlength}226\setlength{\gantttmpb}{0\ganttunitlength}227\setlength{\gantttmpb}{\gantttmpa+\gantttmpb}228229\ifKV@ganttx@drawledgerline230\draw[dotted] (0,\value{ganttnum}-0.2) -- (\value{ganttwidth}*\ganttunitlength,\value{ganttnum}-0.2);231\fi232\node at (0,\value{ganttnum}) [anchor=base east] {%233\ganttfontsize #2%234};235\setkeys{ganttbarx}{#1}{%236\pgfonlayer{foreground}237\draw[diamond-, color=\ganttbarcolor] (\gantttmpa,\value{ganttnum}+0.1) -- (\gantttmpb,\value{ganttnum}+0.4);238\endpgfonlayer239}240\setlength{\ganttlastx}{\gantttmpb}241\addtocounter{ganttnum}{-1}242}243244245% \ganttmilestonecon draws a single diamond *and* connects the diamond with the previous task246% usage: \ganttmilestonecon[color=<color>]{label}{start}{length}247% where the optional color argument are the same as for \ganttbar and \ganttmilestone248249\newcommand{\ganttmilestonecon}[3][]{250\setlength{\nextganttlastx}{\the\ganttlastx+1\ganttunitlength}251\ifdim\nextganttlastx>#3\ganttunitlength252\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);253%254\else255\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);256\fi257% \fi258\ganttmilestone[#1]{#2}{#3}259}260261262\endinput263264265