pgfgantt - 甘特图中一个条形图有两个标签

pgfgantt - 甘特图中一个条形图有两个标签

在我的甘特图中(我使用的是包pgfgantt),每行只有一个任务,每个任务一个条形图。每个条形图都有其对应的标签,用于标识图表左侧对齐的任务;例如,

\documentclass[10pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{pgfgantt}

\begin{document}
\begin{ganttchart}[y unit title=0.4cm,
                    y unit chart=0.5cm,
                    vgrid={draw=none, dotted},
                    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={}{}{.2}]{12}

    \gantttitle{2012}{12} \\
    \gantttitle{October}{4}
    \gantttitle{November}{4} 
    \gantttitle{December}{4}  

    \ganttgroup{Group 1}{1}{8}\\
    \ganttbar{Task 1}{1}{2} \\ 
    \ganttbar{Task 2}{3}{4} \\ 
    \ganttbar{Task 3}{5}{6}\\ 
    \ganttbar{Task 4}{1}{8} \\ 
\end{ganttchart}
\end{document}

现在,我希望能够通过在每个栏的右侧插入内联标签来添加分配给每个任务的资源。我查看了/pgfgantt/bar label,它提供了许多用于调整与栏相关联的标签的配置,但我找不到一种方法来使用它在一个标签中有两个标签(离线和内联)。有没有办法使用 来做到这一点pgfgantt

这张图片显示了上面的编译代码加上红色标签,表示我想要实现的目标。

在此处输入图片描述

答案1

代码(更新)

\documentclass[10pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{pgfgantt}

\makeatletter
\ganttset{
    prog default/.initial=100,
    prog/.code={
        \pgfutil@in@{:}{#1}
        \ifpgfutil@in@
            \pgfqkeysalso{/pgfgantt}{@prog={#1}}
        \else
            \pgfqkeysalso{/pgfgantt}{@prog={\pgfkeysvalueof{/pgfgantt/prog default}:#1}}
        \fi
    },
    @prog/.code args={#1:#2}{
        \pgfqkeysalso{/pgfgantt}{progress=100, progress label text={#2 (#1\,\%)}}
    }
}
\makeatother
\ganttset{progress label anchor/.append style={text=red}}
\begin{document}
\begin{ganttchart}[y unit title=0.4cm,
                    y unit chart=0.5cm,
                    vgrid={draw=none, dotted},
                    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={}{}{.2},
                    ]{12}

    \gantttitle{2012}{12} \\
    \gantttitle{October}{4}
    \gantttitle{November}{4} 
    \gantttitle{December}{4} \\

    \ganttgroup{Group 1}{1}{8}\\
    \ganttbar[prog=50:  Resource 1]{Task 1}{1}{2} \\ 
    \ganttbar[prog=     Resource 2]{Task 2}{3}{4} \\ 
    \ganttbar[prog=     Resource 3]{Task 3}{5}{6}\\ 
    \ganttbar[prog=50:  Resource 1, progress label anchor/.append style={below=4pt}]{Task 4}{1}{8} \\ 
\end{ganttchart}
\end{document}

输出

在此处输入图片描述

代码

\documentclass[10pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{pgfgantt}

\makeatletter
\ganttset{
    prog default/.initial=100,
    prog/.code={
        \pgfutil@in@{:}{#1}
        \ifpgfutil@in@
            \pgfqkeysalso{/pgfgantt}{@prog={#1}}
        \else
            \pgfqkeysalso{/pgfgantt}{@prog={\pgfkeysvalueof{/pgfgantt/prog default}:#1}}
        \fi
    },
    @prog/.code args={#1:#2}{
        \edef\pgf@tempa{#1}%
        \ifx\pgf@tempa\tikz@nonetext
            \pgfqkeysalso{/pgfgantt}{progress={100},progress label text={#2}}
        \else
            \pgfqkeysalso{/pgfgantt}{progress={#1},progress label text={#2 (##1\,\%)}}
        \fi
    }
}
\makeatother
\ganttset{progress label anchor/.append style={text=red}}
\begin{document}
\begin{ganttchart}[y unit title=0.4cm,
                    y unit chart=0.5cm,
                    vgrid={draw=none, dotted},
                    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={}{}{.2},
                    ]{12}

    \gantttitle{2012}{12} \\
    \gantttitle{October}{4}
    \gantttitle{November}{4} 
    \gantttitle{December}{4} \\

    \ganttgroup{Group 1}{1}{8}\\
    \ganttbar[prog=50:  Resource 1]{Task 1}{1}{2} \\ 
    \ganttbar[prog=     Resource 2]{Task 2}{3}{4} \\ 
    \ganttbar[prog=none:Resource 3]{Task 3}{5}{6}\\ 
    \ganttbar[prog=50:  Resource 1, progress label anchor/.append style={below=4pt}]{Task 4}{1}{8} \\ 
\end{ganttchart}
\end{document}

输出

在此处输入图片描述

相关内容