请看一下这个 MWE:
\documentclass[letterpaper]{article}
\usepackage{amsmath, tikz}
\usetikzlibrary{calc}
\newcommand{\test}[3][1]{%
% #1 = optional height
\begin{tikzpicture}
\foreach \Stop/\Nenner/\Options/\Number [count=\c] in {#3} {
% Abstand zwischen Zeilen % {#1*\c} altern.
\pgfmathsetmacro{\YCoord}{1.75*\c}
\pgfmathsetmacro{\Xincrement}{#2/10}
\pgfmathsetmacro{\Zincrement}{#2/\Nenner}
\foreach \x in {1,...,10} {
\pgfmathsetmacro{\Xcoord}{\x*\Xincrement}
\pgfmathsetmacro{\XcoordLabel}{(\x-0.5)*\Xincrement}
%Farbe
\pgfmathsetmacro{\XCoordFill}{ifthenelse(\x > \Stop, "none", "\Options")}
%\pgfmathsetmacro{\XCoordFill}{white}
\draw[fill=white]
($(\Xcoord-\Xincrement,-\YCoord)$) rectangle ($(\Xcoord,-\YCoord+#1)$);
\node at ($(\x*\Xincrement,-\YCoord-0.3)$) { {\footnotesize \x0 \%}};
\node at ($(\x*\Xincrement,-\YCoord-0.6)$) { {\footnotesize von}};
\node at ($(\x*\Xincrement,-\YCoord-0.9)$) { {\footnotesize \Number}};
}%
\foreach \z in {1,...,\Nenner} {
\pgfmathsetmacro{\Zcoord}{\z*\Zincrement}
\pgfmathsetmacro{\ZcoordLabel}{(\z-0.5)*\Zincrement}
\pgfmathsetmacro{\ZCoordFill}{ifthenelse(\z > \Stop, "none", "\Options")}
\draw[draw=\ZCoordFill, fill=\ZCoordFill]
($(\Zcoord-\Zincrement,-\YCoord)$) rectangle ($(\Zcoord,-\YCoord+#1)$);
\node at (\Stop/\Nenner*#2,-\YCoord+1.6) {{\footnotesize \Stop\% von \Number}};
}
}%
\end{tikzpicture}
}
\begin{document}
\test{14}{27/100/cyan/250}
\end{document}
它创建了以下图片:
你能看到图片吗?
无论如何我还有两个问题:
- 上面的节点(代码中的最后一行)有一个奇怪的字体,但我不知道为什么
- 我希望黑线出现在整个图形上,而不仅仅是蓝色部分旁边的部分
我需要改变什么?
答案1
我不确定我是否正确理解了你想要实现的目标。无论如何,你的两个问题可以很简单地回答:
奇怪的字体是因为您在同一个位置多次(准确地说是 100 次)打印了相同的文本。您可能不想使用循环
\foreach
来绘制青色矩形。至少,您不需要循环。如果黑线应该覆盖青色矩形,则需要事后排版。并且您不应将稍后放置的矩形填充为白色,因为这当然会覆盖青色矩形并使其不可见。
因此,这也许可以作为起点(这里不需要加载库calc
,因为您可以直接在坐标中进行基本计算):
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\newcommand{\test}[3][1]{%
% #1 = optional height
\begin{tikzpicture}
\foreach \Stop/\Nenner/\Options/\Number [count=\c] in {#3} {
% Abstand zwischen Zeilen % {#1*\c} altern.
\pgfmathsetmacro{\YCoord}{1.75*\c}
\pgfmathsetmacro{\Xincrement}{#2/10}
\draw[\Options, fill=\Options]
(0,{-\YCoord}) rectangle ({\Stop/\Nenner*#2},{-\YCoord+#1});
\node[above, font=\footnotesize] at ({\Stop/\Nenner*#2},{-\YCoord+#1})
{\Stop\% von \Number};
\foreach \x in {1,...,10} {
\pgfmathsetmacro{\Xcoord}{\x*\Xincrement}
\draw ({\Xcoord-\Xincrement},{-\YCoord}) rectangle ({\Xcoord},{-\YCoord+#1});
\node[below, align=center, font=\footnotesize] at ({\x*\Xincrement},{-\YCoord})
{\x0 \% \\ von \\ \Number};
}
}
\end{tikzpicture}%
}
\begin{document}
\test{14}{27/100/cyan/250}
\end{document}
代码当然可以优化,但由于我不确定真正的用例,所以我只编辑了我认为必要的部分。
答案2
另一种可能性是,使用重写命令从头开始绘制图像,目的是使代码更简单、更简短、更容易理解(这比在代码中搜索问题的根源更容易)。
在命令中定义了三种节点样式,节点标签样式和图像标题的新样式(通过它删除了节点标签的重复,你可能会喜欢它)。在样式定义中使用 Ti钾Z 库backgrounds
(该栏可以位于百分比刻度旁边,chains
(用于绘制代表百分比刻度的节点序列)和positioning
(用于支持节点放置):
\documentclass[letterpaper]{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,
chains,
positioning}
\newcommand{\test}[4][8]{% 1: optional nodes heights
% #2: length of higlighted part
% #3: color of heighlighting
% #4: number of samples
\begin{tikzpicture}[
node distance = 4mm and 0mm,
start chain = going right,
base/.style = {minimum height=#1 mm, inner sep=0pt, outer sep=0pt},
lbl/.style = {text width=2.2em, align=center,
font=\scriptsize, text=red},
M/.style = {base, %name=m,
draw, minimum width=10mm,
label = {[lbl, anchor=north]south east:\s\;\%},
node contents={}, on chain},
P/.style = {base, minimum width=#2 mm, fill=#3,
label = {[lbl, anchor=south]north east:#2\;\%},
node contents={},
right=of m1.west},
T/.style = {inner xsep=0pt, font=\bfseries, above right=of m1.north west},
]
\foreach \x in {1,...,10}
{
\pgfmathsetmacro{\s}{int(10*\x)}
\node (m\x) [M];
}
\node[T] {percentages of number #4:};
\scoped[on background layer]
\node [P=#2/#3];
\end{tikzpicture}
}% \test
\begin{document}
\test{27}{teal!30}{250}
\bigskip
\test[12]{27}{teal!30}{250}
\end{document}
如果您不喜欢图像标题,只需删除T
样式定义和插入节点T
,并在定义M
和N
节点的标签中添加文本“von #4:
\newcommand{\test}[4][8]{% 1: optional nodes heights
% #2: length of higlighted part
% #3: color of heighlighting
% #4: number of samples
\begin{tikzpicture}[
node distance = 4mm and 0mm,
start chain = going right,
base/.style = {minimum height=#1 mm, inner sep=0pt, outer sep=0pt},
lbl/.style = {text width=2.2em, align=center,
font=\scriptsize, text=red},
M/.style = {base,
draw, minimum width=10mm,
label = {[lbl, anchor=north]south east:\s\;\% von #4},
node contents={}, on chain},
P/.style = {base, minimum width=#2 mm, fill=#3,
label = {[lbl, anchor=south]north east:#2\;\% von #4},
node contents={},
right=of m1.west},
]
\foreach \x in {1,...,10}
{
\pgfmathsetmacro{\s}{int(10*\x)}
\node (m\x) [M];
}
\scoped[on background layer]
\node [P=#2/#3];
\end{tikzpicture}
}% \test
经过这些改变后,你将获得`