不使用 pgfplots 创建图例 tikzpicture

不使用 pgfplots 创建图例 tikzpicture

我已经尝试使用 pgfplots 创建图例,但似乎与 tikz-uml 库(我在同一个文件中的另一个 tikzpicture)冲突,所以我想知道如何使用此示例创建图例(取自http://www.woggie.net/2012/07/16/drawing-feature-models-in-pgf-tikz/):

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix,arrows,positioning,shadows}
\tikzset{
    feature/.style={draw, inner sep=1.5mm, font=\small\sffamily, fill=white, drop shadow},
    opt/.style={fill=white}}

\begin{document}

\begin{tikzpicture}[node distance=.8cm]
  \node[feature] (phone) {Phone};

  \matrix (sub)[matrix of nodes,
                below=of phone,
                column sep=3mm, row sep=0mm, nodes=feature]{
  Processor & %sub-1-1
  NFC & %sub-1-2
  Camera & %sub-1-3
  4G \\ %sub-1-4
  };

  \matrix (group)[matrix of nodes, 
                  below=of sub-1-1,
                  column sep=3mm, row sep=0mm, nodes=feature]{
  ARM & %group-1-1
  OMAP & %group-1-2
  Snapdragon \\ %group-1-3
  };

  \draw (phone.south) -- (sub-1-1.north);
  \draw (phone.south) -- (sub-1-2.north);
  \draw (phone.south) -- (sub-1-3.north);
  \draw (phone.south) -- (sub-1-4.north);

  \draw (sub-1-1.south) -- (group-1-1);
  \draw (sub-1-1.south) -- (group-1-2);
  \draw (sub-1-1.south) -- (group-1-3);

  %cross-tree constraint
  \draw[-triangle 45,dashed] (sub-1-4) |- (group-1-3)
    node[pos=.6,anchor=south east] {implies};

  %optional / mandatory
  \draw[opt] (sub-1-2.north) circle (.8mm); 
  \draw[opt] (sub-1-4.north) circle (.8mm); 
  \fill[draw] (sub-1-1.north) circle (.8mm); 
  \fill[draw] (sub-1-3.north) circle (.8mm); 

  %Group arc
  \begin{scope}
    \path[clip] (sub-1-1.south) -- (group-1-1.center) -- (group-1-3.center) -- cycle;
    \draw (sub-1-1.south) circle (.5cm);
  \end{scope}
\end{tikzpicture}

\end{document}

这个想法是在右上角创建一个图例,像这个例子一样(取自维基百科)在此处输入图片描述

有人能帮帮我吗?谢谢!

答案1

由于缺乏更好的解决方案,人们总是可以手动完成,例如使用\matrix

\matrix [draw=black,fill=brown!20,left=1.5cm of phone] {
  \node[fill=black,draw,circle,inner sep=2pt,outer sep=0pt] (m) at (0,0){}; \draw (m) -- +(0,0.4); & \node[legendtext]{Mandatory}; & 
   \filldraw[fill=white,draw=black] (0,0.2) -- ++(225:0.2) arc[start angle=225,end angle=315,radius=0.2]; 
 \draw (0,0.2) ++(225:0.5) -- (0,0.2) -- ++(315:0.5);& \node[legendtext]{Alternative}; \\
  \node[fill=white,draw=black,circle,inner sep=2pt,outer sep=0pt] (o) at (0,0){}; \draw (m) -- +(0,0.4); & \node[legendtext]{Optional}; & 
  \draw (0,0.2) ++(225:0.5) -- (0,0.2) -- ++(315:0.5);
  \filldraw[black] (0,0.2) -- ++(225:0.2) arc[start angle=225,end angle=315,radius=0.2]; & \node[legendtext]{Or}; 
\\
};

我认为没有或者在您的图表中,但我将其包括在内以作参考。注释掉上面矩阵中的最后一个和会删除它。\draw样式只有,并在选项中定义,请参阅下面的完整代码。\filldrawlegendtexttext width=2cmtikzpicture

在此处输入图片描述

\documentclass[tikz,border=4mm]{standalone}
\usetikzlibrary{matrix,arrows,positioning,shadows}
\tikzset{
    feature/.style={draw, inner sep=1.5mm, font=\small\sffamily, fill=white, drop shadow},
    opt/.style={fill=white}}

\begin{document}

\begin{tikzpicture}[
  node distance=.8cm,
  legendtext/.style={text width=2cm}]
  \node[feature] (phone) {Phone};

  \matrix (sub)[matrix of nodes,
                below=of phone,
                column sep=3mm, row sep=0mm, nodes=feature]{
  Processor & %sub-1-1
  NFC & %sub-1-2
  Camera & %sub-1-3
  4G \\ %sub-1-4
  };

  \matrix (group)[matrix of nodes, 
                  below=of sub-1-1,
                  column sep=3mm, row sep=0mm, nodes=feature]{
  ARM & %group-1-1
  OMAP & %group-1-2
  Snapdragon \\ %group-1-3
  };

  \draw (phone.south) -- (sub-1-1.north);
  \draw (phone.south) -- (sub-1-2.north);
  \draw (phone.south) -- (sub-1-3.north);
  \draw (phone.south) -- (sub-1-4.north);

  \draw (sub-1-1.south) -- (group-1-1);
  \draw (sub-1-1.south) -- (group-1-2);
  \draw (sub-1-1.south) -- (group-1-3);

  %cross-tree constraint
  \draw[-triangle 45,dashed] (sub-1-4) |- (group-1-3)
    node[pos=.6,anchor=south east] {implies};

  %optional / mandatory
  \draw[opt] (sub-1-2.north) circle (.8mm); 
  \draw[opt] (sub-1-4.north) circle (.8mm); 
  \fill[draw] (sub-1-1.north) circle (.8mm); 
  \fill[draw] (sub-1-3.north) circle (.8mm); 

  %Group arc
  \begin{scope}
    \path[clip] (sub-1-1.south) -- (group-1-1.center) -- (group-1-3.center) -- cycle;
    \draw (sub-1-1.south) circle (.5cm);
  \end{scope}

\matrix [draw=black,fill=brown!20,left=1.5cm of phone] {
  \node[fill=black,draw,circle,inner sep=2pt,outer sep=0pt] (m) at (0,0){}; \draw (m) -- +(0,0.4); & \node[legendtext]{Mandatory}; & 
   \filldraw[fill=white,draw=black] (0,0.2) -- ++(225:0.2) arc[start angle=225,end angle=315,radius=0.2]; 
 \draw (0,0.2) ++(225:0.5) -- (0,0.2) -- ++(315:0.5);& \node[legendtext]{Alternative}; \\
  \node[fill=white,draw=black,circle,inner sep=2pt,outer sep=0pt] (o) at (0,0){}; \draw (m) -- +(0,0.4); & \node[legendtext]{Optional}; & 
  \draw (0,0.2) ++(225:0.5) -- (0,0.2) -- ++(315:0.5);
  \filldraw[black] (0,0.2) -- ++(225:0.2) arc[start angle=225,end angle=315,radius=0.2]; & \node[legendtext]{Or}; 
\\
};
\end{tikzpicture}
\end{document}

相关内容