从一组输入中选择_图例线条样式_网格样式图例

从一组输入中选择_图例线条样式_网格样式图例

\foreach在以下宏中有一个循环,用于为雷达图创建图例框。首先,我怎样才能使图例分为两列和两行?其次,我已将第 4 个输入用于\LegendBox线条样式。但我不知道如何在每个循环中选择第四个数组的单个元素。简而言之,我怎样才能使图例线条按{solid, dashed, dotted, dashed}顺序排列?要运行以下代码,您需要添加tkz-kiviat.sty文件!谢谢

在此处输入图片描述

% Add tkz-kiviat.sty file first!!!!!!!!!!
\documentclass[tikz,border=9]{standalone}
%%%%%Make sure to add "tkz-kiviat.sty file   !!!!!!!!!

\usepackage{tkz-kiviat} 
\newcommand{\LegendBox}[4][]{%
    \coordinate[#1] (LegendBox_anchor) at (#2) ;
    
    \foreach \col/\item [count=\hi from 0] in {#3} {
           
\draw[line width=0.5mm,color=\col,style={#4}] ([yshift=\hi*15mm]LegendBox_anchor) -- ++(1.5,0)
                                node[anchor=west][color=\col] {\item}
                                ;}
 %  \draw ([shift={(-2,-.4)}]LegendBox_anchor)rectangle([shift={(4.5,2)}]LegendBox_anchor);
  }
%%%%!!!!!!!!!!!!End of part  for Radar chart !!!!!!!%%%%%%%%
\usetikzlibrary{arrows}
\begin{document}

\pgfkeys{
    /kiviatgrad/simplify label/.code={
        \ifx\nv\undefined\else
            \pgfmathparse{Mod(\nv,5)}
            \ifdim\pgfmathresult pt>0pt
                \tikzset{opacity=0}
            \fi
        \fi
    }
}

\begin{tikzpicture}
    \tkzKiviatDiagram[lattice style/.style={gray!50}, scale=0.2, gap=1,, lattice=15]{A,B,C,D}
\tkzKiviatLine[thick,
color=red,
mark=ball,
ball color=red,
mark size=4pt,opacity=.2,
fill=red!20](5,9,6,8,4)
\tkzKiviatLine[thick,
color=blue,
mark=ball,
mark size=4pt,
fill=blue!20,
opacity=.5](4,6,6,4,3)
    \tkzKiviatGrad[simplify label=10](7)  
     \LegendBox[shift={(-3cm,-1cm)}]{current bounding box.south}%
      {blue/blue decription,
       olive/blue description,
       green/green,
       red/red
       }{solid,dashed,dotted,dashed}
\end{tikzpicture}

\end{document}

答案1

可能还有其他更灵活的选项,但对于这种特定情况,您可以将宏修改\LegendBox为如下内容:

\newcommand{\LegendBox}[4][]{%
    \coordinate[#1] (LegendBox_anchor) at (#2) ;
    
    \foreach \col/\item/\linestyle [count=\hi from 0] in {#3} {
         \draw[
           line width=0.5mm,
           color=\col,
           \linestyle,
           style={#4}
           ] ([xshift={floor(\hi/2)*18cm}, % adjust 18cm depending on label widths
               yshift={mod(\hi,2)*15mm}]LegendBox_anchor) -- ++(1.5,0)
                                node[anchor=west] {\item}
          ;
    }
}

在原始代码中,图例条目只是根据计数器向下移动\hi。在此版本中,它们根据向下移动mod(\hi, 2),并根据向右移动floor(\hi/2)。这解决了网格布局问题。

对于线条样式,它被添加到颜色/图例条目列表中,因此在调用时需要例如blue/blue description/solid而不是。参见下面的示例blue/blue description\LegendBox

以下代码的输出

\documentclass[tikz,border=9]{standalone}
%%%%%Make sure to add "tkz-kiviat.sty file   !!!!!!!!!

\usepackage{tkz-kiviat} 
\newcommand{\LegendBox}[4][]{%
    \coordinate[#1] (LegendBox_anchor) at (#2) ;
    
    \foreach \col/\item/\linestyle [count=\hi from 0] in {#3} {
         \draw[
           line width=0.5mm,
           color=\col,
           \linestyle,
           style={#4}
           ] ([xshift={floor(\hi/2)*18cm}, % adjust 18cm depending on label widths
               yshift={mod(\hi,2)*15mm}]LegendBox_anchor) -- ++(1.5,0)
                                node[anchor=west] {\item}
          ;
    }
}
%%%%!!!!!!!!!!!!End of part  for Radar chart !!!!!!!%%%%%%%%
\usetikzlibrary{arrows}
\begin{document}

\pgfkeys{
    /kiviatgrad/simplify label/.code={
        \ifx\nv\undefined\else
            \pgfmathparse{Mod(\nv,5)}
            \ifdim\pgfmathresult pt>0pt
                \tikzset{opacity=0}
            \fi
        \fi
    }
}

\begin{tikzpicture}
\tkzKiviatDiagram[lattice style/.style={gray!50}, scale=0.2, gap=1,, lattice=15]{A,B,C,D}

\tkzKiviatLine[thick,
  color=red,
  mark=ball,
  ball color=red,
  mark size=4pt,opacity=.2,
  fill=red!20](5,9,6,8,4)
\tkzKiviatLine[thick,
  color=blue,
  mark=ball,
  mark size=4pt, 
  fill=blue!20,
  opacity=.5](4,6,6,4,3) 
\tkzKiviatGrad[simplify label=10](7)  
  
\LegendBox[shift={(-3cm,-1cm)}]{current bounding box.south}%
      % here you specify line styles as well
      {blue/blue description/solid,
       olive/olive description/dashed,
       green/green/dotted,
       red/red/dashed%
       }{} % the final argument is empty
\end{tikzpicture}

\end{document}

相关内容