Используя следующий код, автоматизируйте процесс рисования от определенной точки оси X до пересечения с линией, а затем до оси Y.
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{}
\begin{tikzpicture}[scale=.8, transform shape]
\pgfmathsetmacro{\incrmnty}{1.}
\pgfmathsetmacro{\incrmntx}{2.}
\pgfmathsetmacro{\xlab}{2006}
\pgfmathsetmacro{\xn}{5}
\pgfmathsetmacro{\yn}{5}
\begin{scope}[xshift=0cm, yshift=0cm]
\draw [thick](0,0) -- (\incrmntx*\xn+2,0);
\node at (\incrmntx*\xn+2,\incrmnty*\yn-.4) (endx){};% to compensate for the shift of the x-axis labels, and to draw short from the end of the y-axis
\draw [thick](0,0) -- (0,\incrmnty*\yn);
\end{scope}
\begin{scope}[xshift=\incrmntx*.4cm, yshift=0cm]% to shift the x-axis labels
\foreach \x [evaluate=\x as \aff using int(\xlab+(1*\x))]in {0,...,\xn}{\draw [black!20!white] (\incrmntx*\x,0) node [black, xshift=0cm, yshift=-.68cm] {\footnotesize \aff} -- +(-90:.2);}
\end{scope}
\draw[blue!40!black, line width=.4mm] (-.1*\incrmntx,1.56*\incrmnty) -- (endx.center);
\draw[blue!80!white, thick, dashed] (\incrmntx*\xn+.8,0) --+(90:4.26cm) node (aa){} -| (aa.center-|0,0) node [black, xshift=-1cm, yshift=.06cm] {2011 value};
\draw[red!40!black, line width=.4mm] (-.1*\incrmntx,2.5*\incrmnty) -- (endx.center);
\draw[red!80!white, thick, dashed] (\incrmntx*\xn-3.2,0) --+(90:3.7cm) node (bb){} -| (bb.center-|0,0) node [black, xshift=-1cm, yshift=.06cm] {2009 value};
\end{tikzpicture}
\end{frame}
\end{document}
решение1
Предполагая, что графики не всегда представляют собой прямые линии, я рекомендую использовать библиотеку intersections
.
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{}
\begin{tikzpicture}[scale=.8, transform shape]
\pgfmathsetmacro{\incrmnty}{1.}
\pgfmathsetmacro{\incrmntx}{2.}
\pgfmathsetmacro{\xlab}{2006}
\pgfmathsetmacro{\xn}{5}
\pgfmathsetmacro{\yn}{5}
\begin{scope}[xshift=0cm, yshift=0cm]
\draw [thick](0,0) -- (\incrmntx*\xn+2,0);
\node at (\incrmntx*\xn+2,\incrmnty*\yn-.4) (endx){};% to compensate for the shift of the x-axis labels, and to draw short from the end of the y-axis
\draw [thick](0,0) -- (0,\incrmnty*\yn);
\end{scope}
\begin{scope}[xshift=\incrmntx*.4cm, yshift=0cm]% to shift the x-axis labels
\foreach \x [evaluate=\x as \aff using int(\xlab+(1*\x))]
in {0,...,\xn}{\draw [black!20!white] (\incrmntx*\x,0)
node [black, xshift=0cm, yshift=-.68cm,font=\footnotesize] {\aff} -- +(-90:.2);}
\end{scope}
\draw[blue!40!black, line width=.4mm,name path=blue plot] (-.1*\incrmntx,1.56*\incrmnty) -- (endx.center);
\path[overlay,name path=vert-1] (\incrmntx*\xn+.8,0) -- ++(0,10);
\draw[blue!80!white, thick, dashed,name intersections={of=blue plot and vert-1,
by=i1}] (\incrmntx*\xn+.8,0) |- (i1 -|0,0) node [black, left] {2011 value};
\draw[red!40!black, line width=.4mm,name path=red plot] (-.1*\incrmntx,2.5*\incrmnty) -- (endx.center);
\path[overlay,name path=vert-2] (\incrmntx*\xn-3.2,0) -- ++(0,10);
\draw[red!80!white, thick, dashed,name intersections={of=red plot and vert-2,
by=i2}] (\incrmntx*\xn-3.2,0) |- (i2 -|0,0)
node [black,left] {2009 value};
\end{tikzpicture}
\end{frame}
\end{document}
Однако если вы имеете дело с прямыми линиями, вы можете воспользоваться библиотекой calc
.
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{}
\begin{tikzpicture}[scale=.8, transform shape]
\pgfmathsetmacro{\incrmnty}{1.}
\pgfmathsetmacro{\incrmntx}{2.}
\pgfmathsetmacro{\xlab}{2006}
\pgfmathsetmacro{\xn}{5}
\pgfmathsetmacro{\yn}{5}
\begin{scope}[xshift=0cm, yshift=0cm]
\draw [thick](0,0) -- (\incrmntx*\xn+2,0);
\node at (\incrmntx*\xn+2,\incrmnty*\yn-.4) (endx){};% to compensate for the shift of the x-axis labels, and to draw short from the end of the y-axis
\draw [thick](0,0) -- (0,\incrmnty*\yn);
\end{scope}
\begin{scope}[xshift=\incrmntx*.4cm, yshift=0cm]% to shift the x-axis labels
\foreach \x [evaluate=\x as \aff using int(\xlab+(1*\x))]
in {0,...,\xn}{\draw [black!20!white] (\incrmntx*\x,0)
node [black, xshift=0cm, yshift=-.68cm,font=\footnotesize] {\aff} -- +(-90:.2);}
\end{scope}
\draw[blue!40!black, line width=.4mm]
(-.1*\incrmntx,1.56*\incrmnty) coordinate(blue-start) -- (endx.center);
\path (intersection cs:first line={(blue-start) -- (endx.center)},
second line={(\incrmntx*\xn+.8,0)--(\incrmntx*\xn+.8,10)}) coordinate (i1);
\draw[blue!80!white, thick, dashed] (\incrmntx*\xn+.8,0) |- (i1 -|0,0) node [black, left] {2011 value};
\draw[red!40!black, line width=.4mm] (-.1*\incrmntx,2.5*\incrmnty)
coordinate(red-start)-- (endx.center);
\path (intersection cs:first line={(red-start) -- (endx.center)},
second line={(\incrmntx*\xn-3.2,0)--(\incrmntx*\xn-3.2,10)}) coordinate (i2);
\draw[red!80!white, thick, dashed] (\incrmntx*\xn-3.2,0) |- (i2 -|0,0)
node [black,left] {2009 value};
\end{tikzpicture}
\end{frame}
\end{document}
Это приводит к тому же результату.
Общая стратегия заключается в определении пересечений между участками и вертикальными линиями, которые начинаются в указанных координатах x. Эти пересечения называются i1
и i2
в обоих примерах. И затем их можно использовать в
\draw[blue!80!white, thick, dashed] (\incrmntx*\xn+.8,0) |- (i1 -|0,0) node [black, left] {2011 value};
и
\draw[red!40!black, line width=.4mm] (-.1*\incrmntx,2.5*\incrmnty)
coordinate(red-start)-- (endx.center);
соответственно, нарисовать вертикальную, а затем горизонтальную линии и разместить узлы слева от них.
Пример расчета можно сделать стилем.
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{}
\begin{tikzpicture}[scale=.8, transform shape,-|/.style args={of #1 at #2}{%
insert path={(intersection cs:first line={#1},second line={(#2,0)--(#2,10)})
coordinate (aux) (#2,0) |- (0,0|-aux) }},vert/.style args={of #1 at #2}{%
insert path={(#2,0)
-- (intersection cs:first line={#1},second line={(#2,0)--(#2,10)}) }}]
\pgfmathsetmacro{\incrmnty}{1.}
\pgfmathsetmacro{\incrmntx}{2.}
\pgfmathsetmacro{\xlab}{2006}
\pgfmathsetmacro{\xn}{5}
\pgfmathsetmacro{\yn}{5}
\begin{scope}[xshift=0cm, yshift=0cm]
\draw [thick](0,0) -- (\incrmntx*\xn+2,0);
\node at (\incrmntx*\xn+2,\incrmnty*\yn-.4) (endx){};% to compensate for the shift of the x-axis labels, and to draw short from the end of the y-axis
\draw [thick](0,0) -- (0,\incrmnty*\yn);
\end{scope}
\begin{scope}[xshift=\incrmntx*.4cm, yshift=0cm]% to shift the x-axis labels
\foreach \x [evaluate=\x as \aff using int(\xlab+(1*\x))]
in {0,...,\xn}{\draw [black!20!white] (\incrmntx*\x,0)
node [black, xshift=0cm, yshift=-.68cm,font=\footnotesize] {\aff} -- +(-90:.2);}
\end{scope}
\draw[blue!40!black, line width=.4mm]
(-.1*\incrmntx,1.56*\incrmnty) -- (endx.center);
\draw[blue!80!white, thick, dashed,
-|={of {(-.1*\incrmntx,1.56*\incrmnty)--(endx.center)} at {\incrmntx*\xn+.8}}]
node [black, left] {2011 value};
\draw[red!40!black, line width=.4mm] (-.1*\incrmntx,2.5*\incrmnty) -- (endx.center);
\draw[red!80!white, thick, dashed,
-|={of {(-.1*\incrmntx,2.5*\incrmnty)--(endx.center)} at {\incrmntx*\xn-3.2}}]
node[black,left] {2009 value};
\draw[violet!80!white, thick, dashed,
vert={of {(-.1*\incrmntx,1.56*\incrmnty)--(endx.center)} at
{\incrmntx*\xn-2}}];
\end{tikzpicture}
\end{frame}
\end{document}
с чем вся история сводится к
\draw[red!80!white, thick, dashed,
-|={of {(-.1*\incrmntx,2.5*\incrmnty)--(endx.center)} at {\incrmntx*\xn-3.2}}]
node[black,left] {2009 value};
Содержит дополнительный стиль vert
, который можно использовать
\draw[violet!80!white, thick, dashed,
vert={of {(-.1*\incrmntx,1.56*\incrmnty)--(endx.center)} at
{\incrmntx*\xn-2}}];