Tikz:此图中节点和文本的正确对齐

Tikz:此图中节点和文本的正确对齐

我想完善下图。我想将此图左侧显示的文本左对齐,并且需要直线路径。任何帮助都将不胜感激。谢谢

在此处输入图片描述

\documentclass[tikz]{standalone}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows, automata, calc}
\usepackage[latin1]{inputenc}

\begin{document}

\begin{tikzpicture}[
      ->
    , >=stealth'
    , shorten >=1pt
    , auto
    , node distance=1.0cm
    , semithick
    ]

  \tikzstyle{every state}=[
      fill=white
    , draw=none
    , align=left
    %, text width=9cm
    , text=black
    ]



  \node[state] (TotalNi)                           {Total Ni};
  \node[state] (AN) [below of=TotalNi]  {AN};
  \node[state] (CC) [below of=AN]  {CC};
  \node[state] (SC) [below of=CC]  {SC};
  \node[state] (EDTA) [below of=SC]  {EDTA};
  \node[state] (ABDTPA) [below of=EDTA]  {ABDTPA};
  \node[state] (DTPA) [below of=ABDTPA]  {DTPA};

\node at (5,-6.0) [draw,name=u1,rectangle, minimum width=2cm,minimum height=5.75cm,anchor=south,transform shape]  {Test};

 \path[sloped, above, pos=0.4, yshift=0.1cm, anchor=south, auto=false]
        (TotalNi)    edge   node {\textcolor{red}{$-0.06557$}}  (u1.north west) ;

\path[sloped, above, pos=0.4, yshift=0.1cm, anchor=south, auto=false]
        (AN)    edge   node {\textcolor{blue}{$0.04305$}}  (u1) ;

\path[sloped, above, pos=0.4, yshift=0.1cm, anchor=south, auto=false]
        (CC)    edge   node {\textcolor{red}{$-0.34954$}}  (u1) ;


\path[sloped, above, pos=0.4, yshift=0.1cm, anchor=south, auto=false]
        (SC)    edge   node {\textcolor{red}{$-0.34954$}}  (u1) ;

\path[sloped, above, pos=0.4, yshift=0.1cm, anchor=south, auto=false]
        (EDTA)    edge   node {\textcolor{red}{$-0.34954$}}  (u1) ;

\path[sloped, above, pos=0.4, yshift=0.1cm, anchor=south, auto=false]
        (ABDTPA)    edge   node {\textcolor{red}{$-0.34954$}}  (u1) ;

\path[sloped, above, pos=0.4, yshift=0.1cm, anchor=south, auto=false]
        (DTPA)    edge   node {\textcolor{red}{$-0.34954$}}  (u1.south west) ;  



\end{tikzpicture}

\end{document}

答案1

\documentclass[tikz,margin=5]{standalone}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows, automata, calc,fit}
\usepackage[latin1]{inputenc}

\begin{document}

\begin{tikzpicture}[
      ->
    , >=stealth'
    , shorten >=1pt
    , auto
    , node distance=1.0cm
    , semithick
    ]

  \tikzset{every state/.style ={
     % fill=white
    , draw=none
    , anchor = west
    , rectangle
    , minimum width= 1.5cm
    , text width = 1.4cm
    , text=black
    }
    }



  \node[state] (TotalNi)                           {Total Ni};
  \node[state] (AN) [below of=TotalNi]  {AN};
  \node[state] (CC) [below of=AN]  {CC};
  \node[state] (SC) [below of=CC]  {SC};
  \node[state] (EDTA) [below of=SC]  {EDTA};
  \node[state] (ABDTPA) [below of=EDTA]  {ABDTPA};
  \node[state] (DTPA) [below of=ABDTPA]  {DTPA};
  \node[draw,rectangle,xshift=5cm]  [fit = (TotalNi) (AN)(CC) (SC) (EDTA) (ABDTPA) (ABDTPA) (DTPA)] (u1)  {Test};


 \path[sloped, above, pos=0.4, yshift=0.1cm, anchor=south, auto=false]
        (TotalNi.east)    edge   node {\textcolor{red}{$-0.06557$}}  ($(u1.north west)!(TotalNi.east)!(u1.south west)$) ;

\path[sloped, above, pos=0.4, yshift=0.1cm, anchor=south, auto=false]
        (AN)    edge   node {\textcolor{blue}{$0.04305$}}  ($(u1.north west)!(AN.east)!(u1.south west)$) ;

\path[sloped, above, pos=0.4, yshift=0.1cm, anchor=south, auto=false]
        (CC)    edge   node {\textcolor{red}{$-0.34954$}}  ($(u1.north west)!(CC)!(u1.south west)$) ;


\path[sloped, above, pos=0.4, yshift=0.1cm, anchor=south, auto=false]
        (SC)    edge   node {\textcolor{red}{$-0.34954$}}  ($(u1.north west)!(SC)!(u1.south west)$) ;

\path[sloped, above, pos=0.4, yshift=0.1cm, anchor=south, auto=false]
        (EDTA)    edge   node {\textcolor{red}{$-0.34954$}}  ($(u1.north west)!(EDTA)!(u1.south west)$) ;

\path[sloped, above, pos=0.4, yshift=0.1cm, anchor=south, auto=false]
        (ABDTPA)    edge   node {\textcolor{red}{$-0.34954$}}  ($(u1.north west)!(ABDTPA.east)!(u1.south west)$) ;

\path[sloped, above, pos=0.4, yshift=0.1cm, anchor=south, auto=false]
        (DTPA)    edge   node {\textcolor{red}{$-0.34954$}}  ($(u1.north west)!(DTPA.east)!(u1.south west)$) ;
\end{tikzpicture}

\end{document}

在此处输入图片描述

这是另一个版本。

\documentclass[tikz,margin=5]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,fit}

\tikzset{
      ->
    , >=stealth'
    , shorten >=1pt,
    my node/.style = {align=left,anchor=west},
    your node/.style={above,pos=1,xshift=-2cm,text = red}
}

\begin{document}

\begin{tikzpicture}
  \node[my node] (TotalNi)   at (0,0)   {Total Ni};
  \node[my node] (AN) at (0,-1)  {AN};
  \node[my node] (CC) at (0,-2)  {CC};
  \node[my node] (SC) at (0,-3)  {SC};
  \node[my node] (EDTA) at (0,-4)  {EDTA};
  \node[my node] (ABDTPA) at (0,-5)  {ABDTPA};
  \node[my node] (DTPA) at (0,-6)  {DTPA};
  \node[draw,rectangle,xshift=5cm]  [fit = (TotalNi) (AN)(CC) (SC) (EDTA) (ABDTPA) (ABDTPA) (DTPA)] (u1)  {\rotatebox{90}{Test}};


 \draw[->](TotalNi.east)    --  node[your node] {$-0.06557$}  ($(u1.north west)!(TotalNi.east)!(u1.south west)$) ;

\draw[->] (AN) --  node[your node,text = blue] {$\phantom{-}0.04305$}  ($(u1.north west)!(AN.east)!(u1.south west)$) ;

\draw[->] (CC) --  node[your node] {$-0.34954$}  ($(u1.north west)!(CC)!(u1.south west)$) ;
\draw[->] (SC) --  node[your node] {$-0.34954$}  ($(u1.north west)!(SC)!(u1.south west)$) ;

\draw[->] (EDTA) -- node[your node] {$-0.34954$}  ($(u1.north west)!(EDTA)!(u1.south west)$) ;

\draw[->] (ABDTPA) -- node[your node] {$-0.34954$}  ($(u1.north west)!(ABDTPA.east)!(u1.south west)$) ;

\draw[->] (DTPA) -- node[your node] {$-0.34954$}  ($(u1.north west)!(DTPA.east)!(u1.south west)$) ;
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

我认为,通过在最后一部分使用 foreach 循环,可以减少(并且在我看来是改进)图形的代码。此外,语法(a-|b)还可用于指定每条线结束的坐标,即包含a标签的节点和b大框的“西”侧。

另外,由于我注意到数字的颜色是基于其符号的,所以这也可以实现自动化。

使用这些想法,代码可以简化为:

  % All the code up to the line
  % \node[draw,rectangle,xshift=5cm]  [fit = (TotalNi) (AN)(CC) (SC) (EDTA) (ABDTPA) (ABDTPA) (DTPA)] (u1)  {Test};
  % remains identical

  % This loop draws all the lines and numbers
  \foreach \label/\amount in {
     TotalNi/-0.06557,
     AN/0.04305,
     CC/-0.34954,
     SC/-0.34954,
     EDTA/-0.34954,
     ABDTPA/-0.34954,
     DTPA/-0.34954} {
       \pgfmathparse{ifthenelse(\amount<0,"red","blue")}
       \path[above, pos=0.4, yshift=0.1cm, anchor=south, auto=false]
           (\label)  edge   node {\textcolor{\pgfmathresult}{$\amount$}}
           (\label-|u1.west);
   }

结果:

结果

相关内容