如何使用 \node 命令向 pgfgantt 图表添加图例?

如何使用 \node 命令向 pgfgantt 图表添加图例?

我指的是Alain Matthes 的回答问题不久前问过。

我有一个甘特图,显示项目步骤。我基本上已经完成了,我非常喜欢图表设计,但我有一个大问题:我需要为这个图表添加图例。

我在问题上面提到的是 \node 命令。

所以我尝试了几个小时,但似乎无法找到解决方案阿兰·马特斯建议的工作。请阅读他的回答继续之前。我想在右上角的图表内有大约 5 个框。但我得到的只是 5 个框,它们位于前一个框的上方和右侧。我尝试了以下方法:

\documentclass[12pt]{article}
\usepackage{pgfgantt}
\begin{document}
\begin{center}
\begin{figure}[H]
\noindent\resizebox{480pt}{!}{
\begin{tikzpicture}
\begin{ganttchart}
[
vgrid,
vgrid style/.style=dotted,
hgrid,
hgrid style/.style=dotted,
y unit title=0.5cm,
y unit chart=0.8cm,
title/.append style={shape=rectangle, fill=black!20},
title height=1,
bar/.append style={fill=blue!80},
bar height=.5,
bar label font=\normalsize\color{black!80},
group top shift=.6,
group height=.3,
group peaks height=.2,
]
{1}{35}
\gantttitle{Some Title}{35}\\\\
\gantttitlelist{1,...,35}{1}\\\\
\gantttitle{Plan}{7}\gantttitle{Execute}{11}\gantttitle{Stabilize}{4}\gantttitle{Finish}{13}\\

\ganttgroup{Plan}{1}{6}\\
\ganttbar[name=kickoff1]{Kick-Off Meeting}{1}{1}\\
%This looks like the result I want but in the wrong place:
\node[fill=blue,draw] at ([yshift=-20pt]current bounding box.south){Person A};
\node[fill=yellow,draw] at ([yshift=-20pt]current bounding box.south){Person B};
\node[fill=green,draw] at ([yshift=-20pt]current bounding box.south){Person C};
\node[fill=red,draw] at ([yshift=-20pt]current bounding box.south){Person D};
\node[fill=purple,draw] at ([yshift=-20pt]current bounding box.south){Person E};
%This is where I want it to be but arranged as above:
\node[fill=blue,draw] at ([yshift=-20pt]current bounding box.north east){Person A};
\node[fill=yellow,draw] at ([yshift=-20pt]current bounding box.north east){Person B};
\node[fill=green,draw] at ([yshift=-20pt]current bounding box.north east){Person C};
\node[fill=red,draw] at ([yshift=-20pt]current bounding box.north east){Person D};
\node[fill=purple,draw] at ([yshift=-20pt]current bounding box.north east){Person E};
\end{ganttchart}
\end{tikzpicture}
}
\end{figure}
\end{center}
\end{document}

我认为这是一个非常愚蠢的错误,但即使读完官方文档pgfgantt,我不太明白如何正确放置这些盒子。任何帮助我都非常感谢,因为我快要从桌子上咬下来了。

提前致谢!

以下代码基于回答成本加运费并完美地解决了上面描述的问题:

\documentclass[12pt]{article}
\usepackage{pgfgantt}
\begin{document}
\begin{center}
\begin{figure}[H]
\noindent\resizebox{480pt}{!}{
\begin{tikzpicture}
\begin{ganttchart}
[
vgrid,
vgrid style/.style=dotted,
hgrid,
hgrid style/.style=dotted,
y unit title=0.5cm,
y unit chart=0.8cm,
title/.append style={shape=rectangle, fill=black!20},
title height=1,
bar/.append style={fill=blue!80},
bar height=.5,
bar label font=\normalsize\color{black!80},
group top shift=.6,
group height=.3,
group peaks height=.2,
]
{1}{35}
\gantttitle{Some Title}{35}\\\\
\gantttitlelist{1,...,35}{1}\\\\
\gantttitle{Plan}{7}\gantttitle{Execute}{11}\gantttitle{Stabilize}{4}\gantttitle{Finish}{13}\\
\ganttgroup{Plan}{1}{6}\\
\ganttbar{Kick-Off Meeting}{1}{1}\\
\ganttset{bar/.append style={fill=yellow!80}}
\ganttbar{}{1}{1}\\
\ganttset{bar/.append style={fill=green!80}}
\ganttbar{}{1}{1}\\
\ganttset{bar/.append style={fill=red!80}}
\ganttbar{}{1}{1}\\
\ganttset{bar/.append style={fill=purple!80}}
\ganttbar{}{1}{1}\\
\ganttset{bar/.append style={fill=blue!80}}
\ganttlinkedbar{Current state}{2}{2}\\
\ganttlinkedbar{Define goals}{3}{4}\\
\ganttlinkedbar{Choose solution}{5}{6}\\
%This is the solution that works:
\node (a) [fill=blue,draw,anchor=east] at ([xshift=-40pt]current bounding box.8){Person A};
\node (b) [fill=yellow,draw,anchor=west] at ([yshift=-20pt]a.south west){Person B};
\node (c) [fill=green,draw,anchor=west] at ([yshift=-20pt]b.south west){Person C};
\node (d) [fill=red,draw,anchor=west] at ([yshift=-20pt]c.south west){Person D};
\node[fill=purple,draw,anchor=west] at ([yshift=-20pt]d.south west){Person E};
\end{ganttchart}
\end{tikzpicture}
}
\end{figure}
\end{center}
\end{document}

答案1

好的。我用过Alain Matthes 的图表,将其更新为软件包的当前版本。

每次添加节点时,都会current bounding box更改以包含该节点。随着图片变宽,每个节点最终都会向右进一步移动。最简单的方法是命名您添加的第一个节点,然后相对于该节点而不是相对于边界框添加下一个节点。

例如:

\documentclass[border=10pt,tikz,multi]{standalone}
\usepackage{pgfgantt}
\begin{document}
% updated from Alain Matthes's answer at https://tex.stackexchange.com/a/64863/
\begin{ganttchart}[y unit title=0.4cm,
  y unit chart=0.5cm,
  vgrid,hgrid,
  title label anchor/.style={below=-1.6ex},
  title left shift=.05,
  title right shift=-.05,
  title height=1,
  bar/.style={fill=gray!50},
  incomplete/.style={fill=white},
  progress label text={},
  bar height=0.7,
  group right shift=0,
  group top shift=.6,
  group height=.3,
  group peaks height={.2}
  ]{1}{24}
  %labels
  \gantttitle{Week}{24} \\
  \gantttitle{Monday}{4}
  \gantttitle{Tuesday}{4}
  \gantttitle{Wednesday}{4}
  \gantttitle{Thursday}{4}
  \gantttitle{Friday}{4}
  \gantttitle{Saturday}{4} \\
  %tasks
  \ganttbar{first task}{1}{2} \\
  \ganttbar{task 2}{3}{8} \\
  \ganttbar{task 3}{9}{10} \\
  \ganttbar{task 4}{11}{15} \\
  \ganttbar[progress=33]{task 5}{20}{22} \\
  \ganttbar{task 6}{18}{19} \\
  \ganttbar{task 7}{16}{18} \\
  \ganttbar[progress=0]{task 8}{21}{24}

  %relations
  \ganttlink{elem0}{elem1}
  \ganttlink{elem0}{elem3}
  \ganttlink{elem1}{elem2}
  \ganttlink{elem3}{elem4}
  \ganttlink{elem1}{elem5}
  \ganttlink{elem3}{elem5}
  \ganttlink{elem2}{elem6}
  \ganttlink{elem3}{elem6}
  \ganttlink{elem5}{elem7}
  \node (a) [fill=white,draw,anchor=west] at (current bounding box.north east){Box North East};
  \node (b) [fill=red,draw,anchor=west] at ([yshift=-20pt]a.south west){Box North East};
  \node (c) [fill=blue,draw,anchor=west] at ([yshift=-20pt]b.south west){Box North East};
  \node (d) [fill=yellow,draw,anchor=west] at ([yshift=-20pt]c.south west){Box North East};
  \node[fill=purple,draw,anchor=west] at ([yshift=-20pt]d.south west){Box North East};
\end{ganttchart}
\end{document}

右侧的图例框

相关内容