[Tex/LaTex] Alignment of the bar labels of ganttbar

pgfgantttikz-pgf

The labels of \ganttbar in pgfgantt are right aligned by default (at the border of chart).
Is it possible to have it left aligned?

Best Answer

If the question is about the label text align then you can use a coordinate intersection as follows:

\documentclass{article}
\usepackage{tikz,pgfgantt}
\begin{document}
\begin{tikzpicture}[node distance=0cm]
\node[align=center] at (-4,0) (o) {This is\\ adjust node}; %Remove the text later or change it to a \coordinate
\begin{ganttchart}[vgrid, hgrid, bar label font=\Large,bar label text={--#1$\rightarrow$}]{12}
    \gantttitle{Title}{12} \\
    \ganttbar[name=b1,bar label anchor/.style={draw,right = of o|-b1}]{Task 1}{1}{3} \\
    \ganttbar[name=b2,bar label anchor/.style={draw,right = of o|-b2},bar label font=\color{orange}]{Task 2}{4}{10} \\
    \ganttbar[name=b3,bar label anchor/.style={draw,right = of o|-b3},bar label font=\MakeUppercase]{Final task}{11}{12}
\end{ganttchart}
    \node[fill,circle,inner sep=2pt] at (b1) {}; %This line is for demo
    \draw[dashed] (o) |- (b1); % This too!
\end{tikzpicture}
\end{document}

This would give

enter image description here

As seen from the image we first provide an adjusting node. Since the north west point of the chart is the origin we just go left from the origin where I placed the node (o). Then we take the orthogonal intersection of this point and the bar center which is shown with a black dot and the intersection is shown with a dashed line. We put the bar labels' left side at these intersections hence they become left aligned. To be able to refer to each bar label we need to provide some names as can be seen from code.

Note that, I placed node distance=0cm to snap to the point without extra padding otherwise the labels would be as far as this key dictates. Moreover, I put the draw option to the anchor style keys to see if it is working properly and simply can be removed later.