\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,%
main node/.style={draw,circle,minimum size=20pt, inner sep =1pt},node distance=0.5cm]
% the vertices
\node[draw,circle,inner sep=3pt,minimum size=3pt] [label=left:Source] (source) at (0,-0.9) {};
\node[main node] (three) at (1.5,0) {E1};
\node[main node] [above=of three] (a) {D8};
\node[main node] [above=of a] (two) {D1};
\node[main node] [below=of three] (four) {N1};
\node[main node] [below=of four, label=center:RD] (fourteen) {\phantom{0}};
\node[main node] [below=of fourteen] (b) {DO};
\node[main node] [right=of three] (six) {E1};
\node[main node] [above=of six] (five) {D8};
\node[main node] [above=of five] (g) {D1};
\node[main node] [below=of six] (seven) {N1};
\node[main node][right=of six] (eight) {E1};
\node[main node] [below=of seven] (fifthteen) {RD};
\node[main node] [below=of fifthteen] (d) {DO};
\node[main node] [above=of eight] (nine) {D8};
\node[main node] [above=of nine] (c) {D1};
\node[main node] [below=of eight] (ten) {N1};
\node[main node] [below=of ten] (sixteen) {RD};
\node[main node] [below=of sixteen] (e) {DO};
\node[main node] [right=of nine] (seventeen) {D8};
\node[main node] [above=of seventeen] (c) {D1};
\node[main node] [below=of seventeen] (elleven) {E1};
\node[main node] [below=of elleven] (twelwe) {N1};
\node[main node] [below=of twelwe] (thirteen) {RD};
\node[main node] [below=of thirteen] (f) {DO};
\node[main node] [right=of c] (h) {D1};
\node[main node] [below=of h] (i) {D8};
\node[main node] [below=of i] (j) {E1};
\node[main node] [below=of j] (k) {N1};
\node[main node] [below=of k] (l) {RD};
\node[main node] [below=of l] (m) {DO};
\node[main node] [right=of h] (n) {D1};
\node[main node] [below=of n] (o) {D8};
\node[main node] [below=of o] (p) {E1};
\node[main node] [below=of p] (q) {N1};
\node[main node] [below=of q] (r) {RD};
\node[main node] [below=of r] (s) {DO};
\node[main node] [right=of n] (t) {D1};
\node[main node] [below=of t] (u) {D8};
\node[main node] [below=of u] (v) {E1};
\node[main node] [below=of v] (w) {N1};
\node[main node] [below=of w] (x) {RD};
\node[main node] [below=of x] (y) {DO};
% the edges
\path[every node/.style={font=\sffamily\small}]
(four) edge [dashed] node {} (seven)
edge node {} (fifthteen)
edge [dashed] node {} (d)
(fifthteen) edge [dashed] node {} (sixteen)
edge [dashed] node {} (ten)
edge node {} (e)
(e) edge [dashed] node {} (elleven)
edge [dashed] node {} (seventeen)
edge [dashed] node {} (twelwe)
edge node {} (c)
edge [dashed] node {} (thirteen)
edge [dashed] node {} (f)
(source) edge [dashed] node {} (two)
edge [dashed] node {} (three)
edge node {} (four)
edge [dashed] node {} (fourteen)
edge [dashed] node {} (a)
edge [dashed] node {} (b)
(c) edge [dashed] node {} (h)
edge [dashed] node {} (i)
edge node {} (j)
edge [dashed] node {} (k)
edge [dashed] node {} (l)
edge [dashed] node {} (m)
(j) edge [dashed] node {} (n)
edge [dashed] node {} (s)
edge node {} (o)
edge [dashed] node {} (p)
edge [dashed] node {} (q)
edge [dashed] node {} (r)
(o) edge node {} (x)
;
\draw [|-|] (0.6,-6.5) -- (2.3,-6.5);
\draw [-|] (2.3,-6.5) -- (4.0,-6.5);
\draw [-|] (4.0,-6.5) -- (5.7,-6.5);
\draw [-] (5.7,-6.5) -- (7.4,-6.5);
\draw [|-|] (7.4,-6.5) -- (9.1,-6.5);
\node [below] at (1.4,-6.5) {Day 1};
\node [below] at (3.1,-6.5) {Day 2};
\node [below] at (4.8,-6.5) {Day 3};
\node [below] at (6.5,-6.5) {Day 4};
\node [below] at (8.2,-6.5) {Day 5};
\end{tikzpicture}}
上面的代码给出了下图。但是我希望边不跨越节点。例如,来自 DO 的边跨越 RD 和 D1。我如何移动边以使它们不跨越任何节点?
答案1
将以下内容添加到您的代码中
\makeatletter
\tikzset{
to path={
\pgfextra{
\tikz@scan@one@point\relax(\tikztostart)
\pgfmathsetmacro\start@x{\pgf@x}
\pgfmathsetmacro\start@y{\pgf@y}
\tikz@scan@one@point\relax(\tikztotarget)
\pgfmathsetmacro\target@x{\pgf@x}
\pgfmathsetmacro\target@y{\pgf@y}
\pgfmathsetmacro\middle@x{(\start@x+\target@x)/2}
\pgfmathsetmacro\middle@ya{(4*\start@y+\target@y)/5}
\pgfmathsetmacro\middle@yb{(\start@y+4*\target@y)/5}
}
..controls(\middle@x pt,\middle@ya pt)and(\middle@x pt,\middle@yb pt)..(\tikztotarget)
}
}
主意
为了绘制避开其他节点的曲线,我们需要两个辅助点:A和乙。当然,你可以自由决定在哪里A和乙是。
在我的代码中,它们的 x 坐标是平均值。y 坐标分别是两端差值的 1:5 和 4:5。这就是你为什么可以看到/2
和4*
和 的原因/5
。