我有以下甘特图,但我想对其进行以下修改。
- 目前,WP1 和 WP2 之间存在链接(第一条和第二条)。我想保留该链接,但不希望在两者之间使用实线链接,而是希望使用虚线链接。
- 我想在 WP1-WP3 和 WP2-WP3 之间添加两个自定义链接。更准确地说,链接应从 WP1 和 WP3 栏的 Q1 部分的中间开始,并在 WP3 栏的开头结束。此外,链接应具有双箭头(即链接两侧都有箭头)。如下图所示。
对如何实现这些有什么想法吗?
\documentclass[a4paper,11pt]{article}
\usepackage{xcolor}
\usepackage{tikz}
\tikzset{>=latex}
\usepackage{pgfgantt}
\usepackage{relsize}
\definecolor{cbg}{HTML}{fdf6e3}
\definecolor{c1}{HTML}{CB4B16}
\definecolor{c2}{HTML}{586E75}
\definecolor{c3}{HTML}{6C71C4}
\definecolor{c4}{HTML}{D33682}
\newlength{\mynodedist}
\setlength{\mynodedist}{2pt}
\newlength{\mytextwidth}
\setlength{\mytextwidth}{50pt}
\newlength{\mytextheight}
\setlength{\mytextheight}{50pt}
\newcommand{\filllegendentry}[1]{\begin{tikzpicture}[scale=0.5, transform shape] \filldraw[draw=#1, fill=#1, thin] (0,0) rectangle (0.8,0.4); \end{tikzpicture}}
\newcommand{\legendentry}[2]{\begin{tikzpicture}[scale=0.5, transform shape] \filldraw[pattern color=#1, draw=#1, fill=#1, pattern=#2, thin] (0,0) rectangle (0.8,0.4); \end{tikzpicture}}
\begin{document}
\begin{figure}[ht]
\centering
\begin{ganttchart}[
x unit=1cm,
y unit title=0.5cm,
y unit chart=0.6cm,
hgrid=true,
vgrid={{dotted},{solid},*7{dotted},{solid},{dotted},{solid}},
title label anchor/.style={below=-1.6ex},
title height=1,
incomplete/.style={fill=white},
progress label text={},
bar height=0.75,
]{1}{12}
\gantttitle{\textbf{Time Schedule}}{12} \\
\gantttitle{\textbf{Year 1}}{4}
\gantttitle{\textbf{Year 2}}{4}
\gantttitle{\textbf{Year 3}}{4}\\
\gantttitle{Q1}{1}
\gantttitle{Q2}{1}
\gantttitle{Q3}{1}
\gantttitle{Q4}{1}
\gantttitle{Q1}{1}
\gantttitle{Q2}{1}
\gantttitle{Q3}{1}
\gantttitle{Q4}{1}
\gantttitle{Q1}{1}
\gantttitle{Q2}{1}
\gantttitle{Q3}{1}
\gantttitle{Q4}{1}\\
\ganttbar[bar height=0.6,bar/.append style={fill=c1, draw=c1},name=WP1]{\textbf{WP1}}{1}{6}\\
\ganttbar[bar height=0.6,bar/.append style={fill=c2, draw=c2},name=WP2]{\textbf{WP2}}{3}{8}\\
\ganttbar[bar height=0.6,bar/.append style={fill=c3, draw=c3}]{\textbf{WP3}}{2}{3}
\ganttbar[bar height=0.6,bar/.append style={fill=c3, draw=c3}]{\textbf{}}{6}{7}\\
\ganttbar[bar height=0.6,bar/.append style={fill=c4, draw=c4}]{\textbf{WP4}}{9}{12}\\
\ganttlink{WP1}{WP2}
\end{ganttchart}
\caption[Gantt Chart of the Project]{Gantt chart of the project.}
\label{fig:gantt}
\end{figure}
\end{document}
答案1
可以通过改变样式来改变线型/pgfgantt/link
,为了更容易访问,我定义了/pgfgantt/links = <styles>
键,以便您可以直接写入links = dashed
(或任何虚线图案你喜欢)。
bars
我对→做了类似的事情bar
。我还将条形图的绘制设置为无,因为您始终对两者使用相同的颜色。
PGFGantt 带有链接类型dr
,但它默认使用南锚点。
我将定义一个dr*
链接类型,它还使用 的值on bottom fraction
来查找节点南部边界上的正确位置。(如果我们可以参考这个应该发布的日期那就太好了,但这还需要做更多的工作。)
我们需要至少评估一次该值,因为它实际上用于低级 TeX 计算(只允许小数),但我想使用 1/12 这样的分数,因为它可以非常轻松地指定列的中间值。这是由处理程序完成的,.evaluated
它还解决了否则会出现的扩展问题。
代码
\documentclass[tikz]{standalone}
%\documentclass[a4paper,11pt]{article}
%\usepackage{xcolor}
%\usepackage{tikz}
\tikzset{>=latex}
\usepackage{pgfgantt}
\definecolor{cbg}{HTML}{fdf6e3}
\definecolor{c1}{HTML}{CB4B16}
\definecolor{c2}{HTML}{586E75}
\definecolor{c3}{HTML}{6C71C4}
\definecolor{c4}{HTML}{D33682}
\ganttset{
links/.style={link/.append style={#1}},
bars/.style={bar/.append style={#1}},
start fraction/.initial=.5}
\newganttlinktype{dr*}{%
\ganttsetstartanchor{on bottom/.evaluated=% a bit annoying
\pgfkeysvalueof{/pgfgantt/on bottom fraction}}%
\ganttsetendanchor{west}%
\draw [/pgfgantt/link] (\xLeft, \yUpper) -- (\xLeft, \yLower)
node [pos=.5, /pgfgantt/link label node] {\ganttlinklabel} -- (\xRight, \yLower);}
\begin{document}
%\begin{figure}[ht]
%\centering
\begin{ganttchart}[
x unit=1cm,
y unit title=0.5cm,
y unit chart=0.6cm,
hgrid=true,
vgrid={{dotted},{solid},*7{dotted},{solid},{dotted},{solid}},
title label anchor/.style={below=-1.6ex},
title height=1,
incomplete/.style={fill=white},
progress label text={},
bar height=0.75,
bars={draw=none}, % ← !
]{1}{12}
\gantttitle{\textbf{Time Schedule}}{12} \\
\gantttitle{\textbf{Year 1}}{4}
\gantttitle{\textbf{Year 2}}{4}
\gantttitle{\textbf{Year 3}}{4}\\
\foreach \y in {1, 2, 3}
\foreach \Q in {1, 2, 3, 4} {\gantttitle{Q\Q}{1}} \\
\ganttbar[bar height=0.6, bars=c1, name=WP1]{\textbf{WP1}}{1}{6}\\
\ganttbar[bar height=0.6, bars=c2, name=WP2]{\textbf{WP2}}{3}{8}\\
\ganttbar[bar height=0.6, bars=c3]{\textbf{WP3}}{2}{3}
\ganttbar[bar height=0.6, bars=c3]{\textbf{}}{6}{7}\\
\ganttbar[bar height=0.6, bars=c4]{\textbf{WP4}}{9}{12}\\
\ganttlink[links=dashed]{WP1}{WP2}
\ganttlink[links=<->, link type=dr*, on bottom fraction=1/12]{WP1}{elem2}
\ganttlink[links=<->, link type=dr*, on bottom fraction=5/12]{WP2}{elem3}
\end{ganttchart}
%\caption[Gantt Chart of the Project]{Gantt chart of the project.}
%\label{fig:gantt}
%\end{figure}
\end{document}
输出
答案2
对于虚线链接,添加dashed
到链接样式。
\ganttlink[link/.append style=dashed]{...}{...}
对于其他类型的链接,您可以添加一种新的链接类型related
。目前,我还没有找到一种方法来使结束位置参数化或自动化,所以我们需要根据链接应该结束的位置(on bottom=0.1
与on bottom=0.4
)来分离定义。也许你想问一个单独的问题来改进这一点。
\newganttlinktype{related1}{
\ganttsetstartanchor{on left=0.5}
\ganttsetendanchor{on bottom=0.1}
\draw[/pgfgantt/link,<->] (\xLeft,\yUpper) -| (\xRight,\yLower);
}
\newganttlinktype{related4}{
\ganttsetstartanchor{on left=0.5}
\ganttsetendanchor{on bottom=0.4}
\draw[/pgfgantt/link,<->] (\xLeft,\yUpper) -| (\xRight,\yLower);
}
然后,您可以添加链接
\ganttlink[link type=related1]{WP3a}{WP1}
\ganttlink[link type=related4]{WP3b}{WP2}
\documentclass[a4paper,11pt]{article}
\usepackage{xcolor}
\usepackage{tikz}
\tikzset{>=latex}
\usepackage{pgfgantt}
\newganttlinktype{related1}{
\ganttsetstartanchor{on left=0.5}
\ganttsetendanchor{on bottom=0.1}
\draw[/pgfgantt/link,<->] (\xLeft,\yUpper) -| (\xRight,\yLower);
}
\newganttlinktype{related4}{
\ganttsetstartanchor{on left=0.5}
\ganttsetendanchor{on bottom=0.4}
\draw[/pgfgantt/link,<->] (\xLeft,\yUpper) -| (\xRight,\yLower);
}
\usepackage{relsize}
\definecolor{cbg}{HTML}{fdf6e3}
\definecolor{c1}{HTML}{CB4B16}
\definecolor{c2}{HTML}{586E75}
\definecolor{c3}{HTML}{6C71C4}
\definecolor{c4}{HTML}{D33682}
\newlength{\mynodedist}
\setlength{\mynodedist}{2pt}
\newlength{\mytextwidth}
\setlength{\mytextwidth}{50pt}
\newlength{\mytextheight}
\setlength{\mytextheight}{50pt}
\newcommand{\filllegendentry}[1]{\begin{tikzpicture}[scale=0.5, transform shape] \filldraw[draw=#1, fill=#1, thin] (0,0) rectangle (0.8,0.4); \end{tikzpicture}}
\newcommand{\legendentry}[2]{\begin{tikzpicture}[scale=0.5, transform shape] \filldraw[pattern color=#1, draw=#1, fill=#1, pattern=#2, thin] (0,0) rectangle (0.8,0.4); \end{tikzpicture}}
\begin{document}
\begin{figure}[ht]
\centering
\begin{ganttchart}[
x unit=1cm,
y unit title=0.5cm,
y unit chart=0.6cm,
hgrid=true,
vgrid={{dotted},{solid},*7{dotted},{solid},{dotted},{solid}},
title label anchor/.style={below=-1.6ex},
title height=1,
incomplete/.style={fill=white},
progress label text={},
bar height=0.75,
]{1}{12}
\gantttitle{\textbf{Time Schedule}}{12} \\
\gantttitle{\textbf{Year 1}}{4}
\gantttitle{\textbf{Year 2}}{4}
\gantttitle{\textbf{Year 3}}{4}\\
\gantttitle{Q1}{1}
\gantttitle{Q2}{1}
\gantttitle{Q3}{1}
\gantttitle{Q4}{1}
\gantttitle{Q1}{1}
\gantttitle{Q2}{1}
\gantttitle{Q3}{1}
\gantttitle{Q4}{1}
\gantttitle{Q1}{1}
\gantttitle{Q2}{1}
\gantttitle{Q3}{1}
\gantttitle{Q4}{1}\\
\ganttbar[bar height=0.6,bar/.append style={fill=c1, draw=c1},name=WP1]{\textbf{WP1}}{1}{6}\\
\ganttbar[bar height=0.6,bar/.append style={fill=c2, draw=c2},name=WP2]{\textbf{WP2}}{3}{8}\\
\ganttbar[bar height=0.6,bar/.append style={fill=c3, draw=c3},name=WP3a]{\textbf{WP3}}{2}{3}
\ganttbar[bar height=0.6,bar/.append style={fill=c3, draw=c3},name=WP3b]{\textbf{}}{6}{7}\\
\ganttbar[bar height=0.6,bar/.append style={fill=c4, draw=c4}]{\textbf{WP4}}{9}{12}\\
\ganttlink[link/.append style=dashed]{WP1}{WP2}
\ganttlink[link type=related1]{WP3a}{WP1}
\ganttlink[link type=related4]{WP3b}{WP2}
\end{ganttchart}
\caption[Gantt Chart of the Project]{Gantt chart of the project.}
\label{fig:gantt}
\end{figure}
\end{document}