我有以下示例:
\newcounter{yval}
\newenvironment{bardiagram}
{
\setcounter{yval}{0}
\newcommand{\skill}[2]{
\filldraw[lightgray] (0,\value{yval}) rectangle (10,\value{yval}+0.4);
\shade[fill=white,left color = white, right color=red](0,\value{yval}+0.4) rectangle (##1, \value{yval});
\stepcounter{yval}
}
\begin{tikzpicture}
}
{
\end{tikzpicture}
}
我希望得到以下结果,但我不知道如何获得它:
我已经尝试将节点定位在矩形中,但我认为这不是正确的方法。
谢谢你的帮助!
编辑:
我已经尝试制作表格,但是文本的移动非常奇怪。
答案1
您可能希望每个图表中的条形图始终从相同的水平位置开始,并延伸到文本区域的东侧。
\documentclass{article}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc,fit}
\newcounter{yval}
\newenvironment{bardiagram}
{
\noindent\begin{tikzpicture}[remember picture]
\setcounter{yval}{0}
\newcommand{\Skill}[2]{\stepcounter{yval}
\ifnum\value{yval}=1
\node[align=left,font=\sffamily] (n-\number\value{yval}){##1};
\edef\LstSkills{##2}
\edef\LstNodes{(n-\number\value{yval})}
\else
\node[align=left,font=\sffamily,anchor=north west] (n-\number\value{yval})
at ([yshift=-1ex]n-\the\numexpr\value{yval}-1\relax.south west)
{##1};
\edef\LstSkills{\LstSkills,##2}
\edef\LstNodes{\LstNodes (n-\number\value{yval})}
\fi
}
}
{
\node[fit=\LstNodes,inner sep=0pt] (aux){};
\foreach \X [count=\Y] in \LstSkills
{\path[overlay,fill=lightgray] let
\p1=($(current page text area.east)-(aux.east)$) in
([xshift=\x1*\X/10]n-\Y.south-|aux.east) rectangle
(n-\Y.north-|current page text area.east);
\draw[overlay,left color = white, right color=red] let
\p1=($(current page text area.east)-(aux.east)$) in
([xshift=\x1*\X/10]n-\Y.south-|aux.east) rectangle
(n-\Y.north-|aux.east);}
\end{tikzpicture}
}
\begin{document}
\begin{bardiagram}
\Skill{Hibernating}{4}
\Skill{Purring}{9}
\Skill{Preparing\\ espresso}{3}
\end{bardiagram}
\bigskip
\begin{bardiagram}
\Skill{Using Ti\emph{k}Z}{6}
\Skill{Elementary geometry}{8}
\Skill{Writing and spelling}{2}
\end{bardiagram}
\end{document}