我正在使用节点作为图表上的标签,但它们重叠的问题。我知道我可以手动移动它们,但这将是更大文档的一部分,该文档将为许多指标生成相同的图表,并且每个标签的相对位置可能不同。有没有办法编写这个,以便它根据需要移动每个图表的标签节点?出于空间考虑,我希望它们向左/向右移动,而不是向上/向下移动。
梅威瑟:
\documentclass[12pt]{article}
%\usepackage{etoolbox} %NEW
\usepackage{tikz}
\usepackage{xcolor}
\definecolor{RYGgreen}{RGB}{143,185,53}
\definecolor{RYGyellow}{RGB}{230,226,46}
\definecolor{RYGred}{RGB}{230,71,71}
% goals
\newcommand\NormalGoal{0.9}
\newcommand\NormalGoalWarning{0.91}
\newcommand\CurrentMeasureValue{0.7127625}
\newcommand\PreviousMeasureValue{0.653847}
%%% PercentWithDecimal %%%
% turns a value into a percent with the specified number of digits after the decimal point
% input 1 - the number to be rounded
% input 2 - the number of digits after the decimal point
\newcommand\PercentValue{}
\newcommand\PercentWithDecimal[2]{%
\pgfmathparse{100*#1}%
\pgfmathprintnumberto[precision=#2]{\pgfmathresult}{\PercentValue}%
\PercentValue\%%
}
\begin{document}
\begin{center}
\begin{tikzpicture}{h}% normal goal
% draw line
\draw[line width=5pt, RYGred](0,0) -- (14,0); % not meeting goal
\draw[line width=5pt, RYGyellow](14*\NormalGoal,0) -- (14,0); % warning zone
\draw[line width=5pt, RYGgreen](14*\NormalGoalWarning,0) -- (14,0); % meeting goal
\node[left] at (0,0) {0\%}; % label
\node[right] at (14,0) {100\%}; % label
% draw goal marker
\draw[draw=black, line width=2pt] (14*\NormalGoal,-0.5)% goal line
node[below] at (14*\NormalGoal,-0.5){90\%}%
node[above] at (14*\NormalGoal,0.5){goal}%
-- (14*\NormalGoal,0.5); %
% current value line
\draw[draw=RYGred, line width=2pt] (14*\CurrentMeasureValue, -0.5)
node[below] at (14*\CurrentMeasureValue, -0.5){\PercentWithDecimal{\CurrentMeasureValue}{2}}%
node[above] at (14*\CurrentMeasureValue, 0.5){current}%
-- (14*\CurrentMeasureValue,0.5); %
% previous measurement period value line
\draw[draw=gray, line width=2pt] (14*\PreviousMeasureValue, -0.5)
node[below] at (14*\PreviousMeasureValue, -0.5){\PercentWithDecimal{\PreviousMeasureValue}{2}}%
node[above] at (14*\PreviousMeasureValue, 0.5){previous}%
-- (14*\PreviousMeasureValue,0.5); %
\end{tikzpicture}
\end{center}
\end{document}
输出结果如下:
答案1
百分比是否可能相等或非常接近?我建议旋转 30°
用于节点的垂直对齐 \vphantom{bp}
\documentclass[12pt]{article} %\usepackage{etoolbox} %NEW \usepackage{tikz} \usepackage{xcolor} \definecolor{RYGgreen}{RGB}{143,185,53} \definecolor{RYGyellow}{RGB}{230,226,46} \definecolor{RYGred}{RGB}{230,71,71} % goals \newcommand\NormalGoal{0.9} \newcommand\NormalGoalWarning{0.91} \newcommand\CurrentMeasureValue{0.7127625} \newcommand\PreviousMeasureValue{0.653847} %%% PercentWithDecimal %%% % turns a value into a percent with the specified number of digits after the decimal point % input 1 - the number to be rounded % input 2 - the number of digits after the decimal point \newcommand\PercentValue{} \newcommand\PercentWithDecimal[2]{% \pgfmathparse{100*#1}% \pgfmathprintnumberto[precision=#2]{\pgfmathresult}{\PercentValue}% \PercentValue\%% } \begin{document} \begin{center} \begin{tikzpicture}{h}% normal goal % draw line \draw[line width=5pt, RYGred](0,0) -- (14,0); % not meeting goal \draw[line width=5pt, RYGyellow](14*\NormalGoal,0) -- (14,0); % warning zone \draw[line width=5pt, RYGgreen](14*\NormalGoalWarning,0) -- (14,0); % meeting goal \node[left] at (0,0) {0\%}; % label \node[right] at (14,0) {100\%}; % label % draw goal marker \draw[draw=black, line width=2pt] (14*\NormalGoal,-0.5)% goal line node[rotate=30,below] at (14*\NormalGoal,-0.5){90\%}% node[rotate=30,above] at (14*\NormalGoal,0.5){\vphantom{bp}goal}% -- (14*\NormalGoal,0.5); % % current value line \draw[draw=RYGred, line width=2pt] (14*\CurrentMeasureValue, -0.5) node[rotate=30,below] at (14*\CurrentMeasureValue, -0.5){\PercentWithDecimal{\CurrentMeasureValue}{2}}% node[rotate=30,above] at (14*\CurrentMeasureValue, 0.5){\vphantom{bp}current}% -- (14*\CurrentMeasureValue,0.5); % % previous measurement period value line \draw[draw=gray, line width=2pt] (14*\PreviousMeasureValue, -0.5) node[rotate=30,below] at (14*\PreviousMeasureValue, -0.5){\PercentWithDecimal{\PreviousMeasureValue}{2}}% node[rotate=30,above] at (14*\PreviousMeasureValue, 0.5){\vphantom{bp}previous}% -- (14*\PreviousMeasureValue,0.5); % \end{tikzpicture} \end{center} \end{document}
答案2
像这样?
您已经手动定位了文本。因此,我只需在需要的地方添加“左”或“右”选项:
\documentclass[12pt]{article}
\usepackage{tikz}
\definecolor{RYGgreen}{RGB}{143,185,53}
\definecolor{RYGyellow}{RGB}{230,226,46}
\definecolor{RYGred}{RGB}{230,71,71}
% goals
\newcommand\NormalGoal{0.9}
\newcommand\NormalGoalWarning{0.91}
\newcommand\CurrentMeasureValue{0.7127625}
\newcommand\PreviousMeasureValue{0.653847}
\newcommand\PercentValue{}
\newcommand\PercentWithDecimal[2]{%
\pgfmathparse{100*#1}%
\pgfmathprintnumberto[precision=#2]{\pgfmathresult}{\PercentValue}%
\PercentValue\%%
}
\begin{document}
\begin{center}
\begin{tikzpicture}[
arr/.style args = {#1/#2}{draw=#2, line width=#1},
every node/.style = {text height=2ex, text depth=0.5ex,
inner xsep=0pt, text=black}% normal goal
]
% draw line
\draw[arr=5pt/RYGred] (0,0) node[left] {0\%}-- (14,0); % not meeting goal
\draw[arr=5pt/RYGyellow](14*\NormalGoal,0) -- (14,0); % warning zone
\draw[arr=5pt/RYGgreen](14*\NormalGoalWarning,0) -- (14,0) node[right] {100\%}; % meeting
% draw goal marker
\draw[arr=2pt/black]
(14*\NormalGoal,-0.5) node[below] {90\%} --
(14*\NormalGoal,0.5) node[above] {goal};
% current value line
\draw[arr=2pt/RYGred]
(14*\CurrentMeasureValue, -0.5) node[below right] {\PercentWithDecimal{\CurrentMeasureValue}{2}} --
(14*\CurrentMeasureValue,0.5) node[above right] {current}; %
% previous measurement period value line
\draw[arr=2pt/gray]
(14*\PreviousMeasureValue, -0.5) node[below left] {\PercentWithDecimal{\PreviousMeasureValue}{2}} --
(14*\PreviousMeasureValue,0.5) node[above left] {previous}; %
\end{tikzpicture}
\end{center}
\end{document}
正如您所看到的,我还自由地定义了线条和节点的样式,从而可以编写更短的图像代码。