我的问题是,我似乎无法对齐甘特图中的列和行。也就是说,第一列应该具有如示例中的背景颜色(紫色),第二列(持续时间)应具有灰色背景,并且两列中的字母都应为白色。第一列中的 Vellica 字体应该相同 - 但在我的示例中并非如此。
如果有人有解决这个问题的想法和例子,那将非常有用。
\documentclass[a4]{article}
\usepackage[a4paper,
top=25mm,bottom=25mm,left=10mm,right=10mm,
footskip=7mm,marginparwidth=0in]{geometry}
\usepackage{pgfgantt}
\definecolor{Violet}{RGB}{164,84,138}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{pdflscape}
\NewDocumentCommand\textganttbarC{O{} O{} mmmm}{%
\ganttgroup[#1,group/.append style={alias=tmp,draw=red!10,line width=3pt}]{#3}{#5}
{#6}
\node [Violet,font=\bfseries\footnotesize,at={(tmp)},#2] {#4};
}
\NewDocumentCommand\textganttbarA{O{} O{} mmmm}{%
\ganttgroup[#1,group/.append style={alias=tmp},
group incomplete/.append style={draw=black!50,line width=3pt, fill=black!50}]{#3}
{#5}{#6}
\node [white,fill=black!50,font=\footnotesize,at={(tmp)},#2] {#4};
}
\NewDocumentCommand\textganttbarB{O{} O{} mmmm}{%
\ganttgroup[#1,group/.append style={alias=tmp},
group incomplete/.append style={draw=black!50,line width=3pt, fill=black!50}]{#3}
{#5}{#6}
\node [white,fill=black!50,font=\footnotesize,at={(tmp)},#2] {#4};
}
\begin{document}
\begin{landscape}
\begin{ganttchart}[%
title label font=\tiny,
group label font={\bfseries\small},
bar label font={\itshape\small},
y unit title=4mm,
y unit chart=5mm,
expand chart=1.3\textwidth,
title height=1.2,
title/.style={draw,fill=black!10},
bar top shift=0.2, bar height=0.9,
group right shift=0,
group left shift=0,
group top shift=0,
group height=.8,
group peaks width={0},
]{1}{20}
\gantttitle[title label node/.append style=
{anchor=east,align=center,fill=Violet,text=white,
text width=40mm}]{Tasks}{1}
\gantttitle[title/.style={anchor=east,align=center,draw=black!50,fill=black!50}]
{Duration}{1}
\gantttitle[inline,title/.style={draw=black!50,fill=black!50}]{2022}{6}
\gantttitle[inline,title/.style={draw=black!50,fill=black!50}]{2023}{6}
\gantttitle[inline,title/.style={draw=black!50,fill=black!50}]{2024}{6}
\\
\gantttitle[title label node/.append style=
{anchor=east,align=left,fill=Violet,text=white,
text width=40mm}]{PhD Research}{1}
\gantttitle[title/.style={anchor=east,draw=black!50,fill=black!50},
title label node/.append style=
{anchor=east,align=center,text=white,font=\footnotesize\bfseries}]{36}{1}
\gantttitle{Jan-Feb}{1}
\gantttitle{Mar-Apr}{1}
\gantttitle{May-Jun}{1}
\gantttitle{Jul-Aug}{1}
\gantttitle{Sep-Oct}{1}
\gantttitle{Nov-Dec}{1}
\gantttitle{Jan-Feb}{1}
\gantttitle{Mar-Apr}{1}
\gantttitle{May-Jun}{1}
\gantttitle{Jul-Aug}{1}
\gantttitle{Sep-Oct}{1}
\gantttitle{Nov-Dec}{1}
\gantttitle{Jan-Feb}{1}
\gantttitle{Mar-Apr}{1}
\gantttitle{May-Jun}{1}
\gantttitle{Jul-Aug}{1}
\gantttitle{Sep-Oct}{1}
\gantttitle{Nov-Dec}{1}
\\
%
\textganttbarA[progress=0]{Literature Review}{12}{1}{1}
\textganttbarC[progress=0]{}{100\%}{2}{7}
\\
\textganttbarB[progress=0]{~~\em Blockchain}{9}{1}{1}
\textganttbarC[progress=0]{}{100\%}{2}{4} \\
\textganttbarB[progress=0]{~~\em Cryptography}{9}{1}{1}
\textganttbarC[progress=0]{}{100\%}{2}{4} \\
\textganttbarB[progress=0]{~~\em Initial review}{1}{1}{1}
\textganttbarC[progress=0]{}{100\%}{3}{3} \\
\textganttbarB[progress=0]{~~\em Internet of Things}{6}{1}{1}
\textganttbarC[progress=0]{}{100\%}{3}{4}
\end{ganttchart}
\end{landscape}
\end{document}
答案1
这是让你开始pgfgantt
和Tikz
独自重复使用的方法Jaspers 回答了关于引用对象的问题在这个包中。
基本结构:
tikzset
用于文本和节点外观的样式- 引入隐形甘特图
\newganttchartelement{invis}
- 放上标题,和你做的类似
- 放置
\ganttbar
s 和\ganttinvis
s - 以相对方式将节点放置在左侧,替换标准任务标签
关于隐形酒吧,对两者来说都只是一个技巧:
- 将实际条向右移动
- 放置任务节点时,有一个参考坐标
为了演示,我将不可见样式设置为invis/.style={draw=red!20}
,以便您可以看到其效果。invis/.style={draw=none}
稍后将其替换为 ,这也会删除持续时间内的伪影。
让我们看看它是如何工作的。
首先,放置一个隐形条和一个常规条。在此处为隐形条命名B
。
\ganttinvis[name=B]{}{1}{1} \ganttbar[progress=15]{}{2}{4}\\
其次,放置一个具有持续时间样式的节点,并将其锚定到上述不可见栏上:
\node[dur] (B2) at (B.west) {9};
最后,锚定一个提供任务名称的主任务或子任务节点:
\node[sub] at (B2.west) {Blockchain};
当然,这一系列命令可以进一步重构为单个命令。此外,“一些”微调仍与分离、对齐等有关。
您可以使用相同的概念在年份、月份等的左侧添加复杂的条目。要为甘特图标题着色,只需通过 tikzset 定义并使用颜色。
从开发的角度来看,经历这些阶段很有用,您可能希望保存多个版本:
- 使用默认设置正确设置基本结构(完毕)
- 引入一些颜色,调整一些尺寸等(进行中)
- 重构样式、代码、命令等(要做)
\documentclass[10pt,border=3mm]{standalone}
\usepackage{pgfgantt}
\begin{document}
\begin{ganttchart}[
x unit=15mm, % Jan-Feb width
y unit chart=6mm,
toptitl/.style={title top shift=-.4}, % moving 2nd row up
]{1}{12}
\tikzset{ % styles needed
% ~~~ color ~~~~~~~~~~~~
ttxt/.style={fill=red!50!blue!80,
text=white,
font=\bfseries,
},
dtxt/.style={fill=black!50,text=yellow},
% ~~~ node dimensions ~~~~~~~~
nddim/.style={minimum height=6mm},
% ~~~ duration node ~~~~~~~~~~~~~
dur/.style={dtxt,nddim,minimum width=8mm,anchor=east},
% ~~~ main text ~~~~~~~~~~~
main/.style={ttxt,nddim,minimum width=36mm,anchor=east},
% ~~~ subitem text ~~~~~~
sub/.style={ttxt,
nddim,
minimum width=36mm,
anchor=east,
font=\itshape,
},
}
% ~~~ invisible ganttbar ~~~~
\newganttchartelement{invis}{
invis/.style={draw=red!20} % set to: draw=none
}
% ~~~ years ~~~~~~~~~~~~~~~~~
\gantttitle{2022}{6}
\gantttitle{2023}{6}\\
% ~~~ months ~~~~~~~~~~~~~~~~~
\foreach \i in {1,2} % being lazy to copy
\gantttitle[toptitl]{Jan-Feb}{1}
\gantttitle[toptitl]{Mar-Apr}{1}
\gantttitle[toptitl]{May-Jun}{1}
\gantttitle[toptitl]{Jul-Aug}{1}
\gantttitle[toptitl]{Sep-Oct}{1}
\gantttitle[toptitl]{Nov-Dez}{1}; % ending the foreach path
\\
% ~~~ some tasks ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\ganttbar[progress=100,name=A] {}{1}{2}\\
\ganttinvis[name=B]{}{1}{1} \ganttbar[progress=15]{}{2}{4}\\
\ganttinvis[name=C]{}{1}{2} \ganttbar[progress=5]{}{3}{7}\\
% ~~~ labels for the tasks ~~~~~~~~~~~~~~~~~~~
\node[dur] (A2) at (A.west) {12};
\node[main](A3) at (A2.west) {Literature review};
\node[dur] (B2) at (B.west) {9};
\node[sub] at (B2.west) {Blockchain};
\node[dur] (C2) at (C.west) {5};
\node[sub] at (C2.west) {Cryptography};
\end{ganttchart}
\end{document}
附言
将概念扩展到标题行有点不同,因为\gantttitle
不接受name=
选项。因此,作为演示者,我更改了这些行,并在最后添加了新的行:
为了固定第一个标题行,首先放置一个不可见的栏,它可以接受name=
:
% ~~~ years ~~~~~~~~~~~~~~~~~
\ganttinvis[name=T]{}{1}{1} \gantttitle{2022}{6} % <<<
...
在循环内部也是一样,创建 2 个标签M1
,M2
在这种情况下,我们只需要M1
:
% ~~~ months ~~~~~~~~~~~~~~~~~
\foreach \i in {1,2}
\ganttinvis[name=M\i]{}{1}{1} \gantttitle[toptitl]{Jan-Feb}{1} % <<<
...
最后重复节点放置,如前所述:它会覆盖之前的 M2,但这无关紧要
% ~~~ trials on the titles ~~~ % <<<
\node[dur] (T2) at (T.west) {[d]};
\node[dur] (M2) at (M1.west) {36};
结果:draw=none
再次更换时,红线和其他伪影就会消失
答案2
这表格数组包就可以使用了。
第二列使用来自包的 S 列希尼奇,选项用按键设置,例如,column{1}={bg=Violet,fg=white}
设置第一列为紫色背景,白色前景,同理,cell{2-Z}{2}={fg=white}
设置第二行至最后一行第二列单元格为白色前景。
\documentclass[border=6pt]{standalone}
\usepackage{tabularray}
\UseTblrLibrary{siunitx}
\usepackage{xcolor}
\definecolor{Violet}{RGB}{164,84,138}
\begin{document}
\begin{tblr}{
width=36cm,
colspec={lS[table-format=2.0]cccccccccccccccccc},
column{1}={bg=Violet,fg=white},
column{2}={bg=black!50},
cell{1}{1}=c,
cell{3-Z}{1}={font=\em},
cell{2-Z}{2}={fg=white},
cell{1}{3-Z}={bg=black!50}
}
Tasks & {{{Duration}}} & \SetCell[c=6]{c} 2022 & & & & & & \SetCell[c=6]{c} 2023 & & & & & & \SetCell[c=6]{c} 2024\\
PhD Research & 36 & Jan-Feb & Mar-Apr & May-Jun & Jul-Aug & Sep-Oct & Nov-Dec & Jan-Feb & Mar-Apr & May-Jun & Jul-Aug & Sep-Oct & Nov-Dec & Jan-Feb & Mar-Apr & May-Jun & Jul-Aug & Sep-Oct & Nov-Dec\\
Literature Review & 12 & \SetCell[c=6]{c,bg=black!10,fg=Violet} \qty{100}{\percent}\\
Blockchain & 9 & \SetCell[c=5]{c,bg=black!10,fg=Violet} \qty{100}{\percent}\\
Cryptography & 9 & \SetCell[c=5]{c,bg=black!10,fg=Violet} \qty{100}{\percent}\\
Initial review & 1 & & \SetCell[c=1]{c,bg=black!10,fg=Violet} \qty{100}{\percent}\\
Internet of Things & 6 & & \SetCell[c=3]{c,bg=black!10,fg=Violet} \qty{100}{\percent}\\
\end{tblr}
\end{document}
答案3
使用一些表格来编写图表似乎比使用pgfgantt
包更简单。例如,通过使用tblr
包tabularray
表,您可以获得以下结果:
MWE,其中为绘制条形图定义了新命令\SCC
(您可以选择不同的名称,这对您来说更有意义),它是:
\documentclass{article}
\usepackage[a4paper,
hmargin=10mm, vmargin=25mm,
footskip=7mm,
marginparwidth=0in]{geometry}
\usepackage{tabularray}
\UseTblrLibrary{siunitx}
\NewTableCommand\SCC[1]{\SetCell[c=#1]{c, bg=gray!50, fg=violet}}
\usepackage{xcolor}
\usepackage{pdflscape}
\begin{document}
\begin{landscape}
\begin{tblr}{hlines, vlines,
colsep=4pt,
colspec = {Q[l, bg=violet, fg=white, font=\itshape]
X[c, si={table-format=2.0}, bg=gray, fg=white]
*{18}{X[c, m]}},
row{1} = {c, m, font=\textup, fg=white, guard}
}
Tasks
& \SCC{1} Dura\-tion
& \SCC{6} 2022
& & & & & & \SCC{6} 2023
& & & & & & \SCC{6} 2024
& & & & & \\
Ph.D research
& 36
& Jan-Feb & Mar-Apr & May-Jun & Jul-Aug & Sep-Oct & Nov-Dec
& Jan-Feb & Mar-Apr & May-Jun & Jul-Aug & Sep-Oct & Nov-Dec
& Jan-Feb & Mar-Apr & May-Jun & Jul-Avg & Sep-Oct & Nov-Dec \\
Literature Review
& 12 & \SCC{6} \qty{100}{\percent}
& & & & & & & & & & & & & & & & & \\
Blockchain
& 9 & \SCC{5} \qty{100}{\percent}
& & & & & & & & & & & & & & & & & \\
Cryptography
& 9 & \SCC{5} \qty{100}{\percent}
& & & & & & & & & & & & & & & & & \\
Initial review
& 1 & & \SCC{6} \qty{100}{\percent}
& & & & & & & & & & & & & & & & \\
Internet of Things
& 6 & & \SCC{6} \qty{100}{\percent}
& & & & & & & & & & & & & & & & \\
\end{tblr}
\end{landscape}
\end{document}
我假设您只提供了图表的一部分,因为去年没有显示任何条形图,您将在实际文档中添加它们。请注意,行中的tabularray
所有 & 符号都按照列规范中的定义按照语法书写,无论它们之间的单元格是否为空。这也将使以后插入条形图更加容易。