一点点 TikZ

一点点 TikZ

我应该创建一个像这里这样的表格作为示例。我认为制作这样的表格的最佳方法是生成一些标签并使用它们为节点着色以使其更加自动化。第一列是分类颜色,右列是色谱。我的问题是,如果您必须面对这样的问题,您将使用 TikZ 中的哪种工具来生成这样的表格。我开始在 TikZ 中编写表格,但我定义了所有内容:节点的位置,所有线相交点,以便随后创建线条。代码变得庞大、讨厌,并且很难检查我是否正确放置了颜色。

那么,您对如何完成这项工作有什么建议吗?

在此处输入图片描述

答案1

这可以给你一个起点:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}

\definecolor{redi}{RGB}{255,38,0}
\definecolor{redii}{RGB}{200,50,30}
\definecolor{yellowi}{RGB}{255,251,0}
\definecolor{bluei}{RGB}{0,150,255}
\definecolor{blueii}{RGB}{135,247,210}
\definecolor{blueiii}{RGB}{91,205,250}
\definecolor{blueiv}{RGB}{115,244,253}
\definecolor{bluev}{RGB}{1,58,215}
\definecolor{orangei}{RGB}{240,143,50}
\definecolor{yellowii}{RGB}{222,247,100}
\definecolor{greeni}{RGB}{166,247,166}

\tikzset{ 
table/.style={
  matrix of nodes,
  row sep=-\pgflinewidth,
  column sep=-\pgflinewidth,
  nodes={rectangle,draw=black,text width=1.25ex,align=center},
  text depth=0.25ex,
  text height=1ex,
  nodes in empty cells
  },
texto/.style={font=\footnotesize\sffamily},
title/.style={font=\small\sffamily}
}


\newcommand\CellText[2]{%
  \node[texto,left=of mat#1,anchor=east]
  at (mat#1.west)
  {#2};
}

\newcommand\SlText[2]{%
  \node[texto,left=of mat#1,anchor=west,rotate=75]
  at ([xshift=3ex]mat#1.north)
  {#2};
}

\newcommand\RowTitle[2]{%
\node[title,left=6.3cm of mat#1,anchor=west]
  at (mat#1.north west)
  {#2};
}


\begin{document}


\begin{tikzpicture}[node distance =0pt and 0.5cm]

\matrix[table] (mat11) 
{
|[fill=redi]| & & & \\
|[fill=redi]| & & & \\
|[fill=redi]| & & & \\
};
\matrix[table,right=of mat11] (mat12) 
{
|[fill=orangei]| & |[fill=yellowii]| & |[fill=blueii]| & |[fill=blueiii]| \\
& |[fill=blueii]| & |[fill=greeni]| & |[fill=blueiv]| \\
& & |[fill=bluev]| & |[fill=blueiii]| \\
};
\matrix[table,below=of mat11] (mat21) 
{
|[fill=redi]| & & & \\
|[fill=redi]| & & & \\
|[fill=yellowi]| & & & \\
|[fill=yellowi]| & & & \\
};
\matrix[table,below=of mat12] (mat22) 
{
|[fill=bluev!95]| & & |[fill=yellowii]| & |[fill=orangei!80]| \\
& |[fill=yellowii]| & |[fill=bluev!80]| & |[fill=blueiii]| \\
|[fill=redii]| & |[fill=blueiii!80]| & & |[fill=bluev]| \\
|[fill=bluev]| & & |[fill=blueiii!80]| & |[fill=bluev]| \\
};
\matrix[table,below=of mat21] (mat31) 
{
|[fill=redi]| & & & \\
|[fill=bluei]| & & & \\
};
\matrix[table,below=of mat22] (mat32) 
{
|[fill=bluev]| & & |[fill=redii]| & |[fill=redii!90!black]| \\
|[fill=redii!90]| & |[fill=redii!75!black]| & |[fill=bluev]| & |[fill=bluev!80!black]| \\
};

\SlText{11-1-1}{Fibroadenoma [xx]}
\SlText{11-1-2}{Simple Cyst}
\SlText{11-1-3}{Complex Cyst}
\SlText{11-1-4}{Papilloma}

\SlText{12-1-1}{CDI}
\SlText{12-1-2}{CLI}
\SlText{12-1-3}{FA}
\SlText{12-1-4}{Cyst}

\RowTitle{11}{Background echotexture};
\CellText{11-1-1}{Homogeneous adipose-echotexture};
\CellText{11-2-1}{Homogeneous fibroglandular-echotexture};
\CellText{11-3-1}{Hoterogeneous};

\RowTitle{21}{Mass shape};
\CellText{21-1-1}{Oval};
\CellText{21-2-1}{Round};
\CellText{21-3-1}{Irregular};

\RowTitle{31}{Mass orientation};
\CellText{21-4-1}{Lobular};
\CellText{31-1-1}{Parallel to skin};
\CellText{31-2-1}{Non-parallel to skin};

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

你不需要使用 Tikz;-)

在此处输入图片描述

\documentclass{article}

\usepackage{xcolor,array,graphicx}

\makeatletter
\newcolumntype{Z}{>{\zz}p{1em}|}
\def\zzz{\textcolor{\thiscol}{\leaders\vrule height1em\hskip1em}\kern\z@\ignorespaces}

\def\zz\ignorespaces#1{%
\def\thiscol{white}%
\if S#1\expandafter\szz
\else\if R#1\def\thiscol{red}%
\else\if Y#1\def\thiscol{yellow}%
\else\if B#1\def\thiscol{blue}%
\else\if X#1\def\thiscol{black}%
\else#1\fi\fi\fi\fi
\expandafter\zzz
\fi}
\def\szz{\afterassignment\@szz\count@0}
\def\@szz{\edef\thiscol{mycolor-\the\count@}\zzz}

\definecolor{mycolor-1}{rgb}{.8,.4,.6}
\definecolor{mycolor-20}{rgb}{.7,.9,.6}
\definecolor{mycolor-5}{rgb}{.2,.4,.6}
\definecolor{mycolor-4}{rgb}{.2,.7,.3}
\begin{document}
\renewcommand\arraystretch{0}
\setlength\tabcolsep{0pt}
\begin{tabular}{l@{\hspace{2em}}r@{\hspace{2pt}}|ZZZZ@{\hspace{1cm}}ZZZZ}
\multicolumn{1}{l}{\rlap{\rotatebox{60}{something}}}&
\multicolumn{1}{l}{\rlap{\rotatebox{60}{something else}}}&
\multicolumn{1}{l}{\rlap{\rotatebox{60}{ho hum}}}&
\multicolumn{1}{l}{\rlap{\rotatebox{60}{more stuff}}}&
\multicolumn{1}{l}{\rlap{\rotatebox{60}{and more}}}&
\multicolumn{1}{l@{\hspace{1cm}}}{\rlap{\rotatebox{60}{interesting column}}}&
\multicolumn{1}{l}{\rlap{\rotatebox{60}{ABC}}}&
\multicolumn{1}{l}{\rlap{\rotatebox{60}{XYZ}}}&
\multicolumn{1}{l}{\rlap{\rotatebox{60}{EED}}}&
\multicolumn{1}{l}{\rlap{\rotatebox{60}{JHY}}}\\
\hline
stuff& foo &R&&&&  S1 &&&\\\hline
stuff& foo &Y&&&&  S20 &&S5&\\\hline
stuff& foo &B&&&&  S5 &&S1&\\\hline
stuff& foo &X&&&&  S4 &&&S20\\\hline
\end{tabular}

\end{document}

答案3

您不必使用 TikZ(但您可以)。

我会使用pgfplotstable(但这将是艰难的开始)或者至少collcell包使得创作更加容易。

\colorlet如果您想要的只是彩色单元格,请使用或用独特的名称定义所需的颜色,\definecolor然后将其用作单元格内容,然后将其抓取并作为参数\collectcell提供。\myBox

在这个例子中,这只是用来使用,\cellcolor但可以提供给 TikZ 绘制一个矩形,或者可以是一个键的名称(想想pgfkeys) 可以做任何事情(\cellcolor,简单的文本,TikZ)。

代码

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{array,collcell}
\newcommand*{\myBox}[1]{\cellcolor{col@#1}}
\colorlet{col@a}{red}  \colorlet{col@b}{blue}
\colorlet{col@c}{cyan} \colorlet{col@d}{black}
\colorlet{col@e}{green}\colorlet{col@f}{red!50!yellow}
\newcolumntype{C}[1]{>{\collectcell\myBox}#1<{\endcollectcell}}
\begin{document}
\begin{tabular}{l C{c} C{c} p{.5cm} C{c} C{c}}
    Background Echotexture & \multicolumn{1}{c}{Head1} & \multicolumn{1}{c}{Head2} & & \multicolumn{1}{c}{Head3} & \multicolumn{1}{c}{Head4} \\
    Foo                    & a                         & b                         & & c                         & d                         \\
    Bar                    & e                         & a                         & & d                         & f
\end{tabular}
\end{document}

输出

在此处输入图片描述

一点点 TikZ

这是一个更加复杂的方法。

TikZ 仅用于绘制框,没有其他用途。

pgfkeys用于定义一些简单的样式,如commonprobable.样式是一种极简风格,只绘制边框,不使用任何填充。.unknown设置键以便您可以使用任何颜色,然后将其用作样式的参数box;但我建议至少为常见单元格定义样式(右侧框中的颜色肯定有某种含义)。

代码

\documentclass{article}
\usepackage{tikz,collcell}
\newcommand*{\cellset}{\pgfqkeys{/cell}}
\newcommand*{\myCell}{\cellset}
\newcolumntype{C}[1]{>{\collectcell\myCell}#1<{\endcollectcell}}
\makeatletter
\tikzset{overlay linewidth/.code=\tikz@addmode{\tikzset{overlay}}}
\cellset{.unknown/.code={\edef\pgfkeys@temp{\noexpand\cellset{box=\pgfkeyscurrentname}}\pgfkeys@temp}}
\makeatother
\cellset{
  head/.code=\rlap{\rotatebox{60}{#1}}\hspace*{1.2em},
  myCell/.style={
    draw=black,
    overlay linewidth,
    inner sep=+0pt,
    outer sep=+0pt,
    anchor=center,
    fill={#1},
    minimum size=+1.7em},
  box/.code={%
    \tikz[baseline=-1ex]
      \node[/cell/myCell={#1}]{};},
  box/.default=none,
  ./.style={box},
  @define/.style args={#1:#2}{#1/.style={box=#2}},
  @define/.list={%
    not present:black,  possible:cyan,
    probable:   yellow, common:  red,
    o:          orange, y:       yellow}
  }
\begin{document}
\begin{tabular}{>{\footnotesize}r *4{C{c} @{}} @{\hspace*{.5cm}} *4{C{c} @{}}}
                                               & head=Fibroadenoma [xx] & head=Simply Cyst & head=Complex Cist & head=Pailloma & head=CDI & head=CLI     & head=FA & head=Cyst      \\[1em]
    \multicolumn{1}{l}{Background Echotexture} &                        &                  &                   &               &          &              &         &  \\
               Homogeneous adipose-echotexture & common                 & .                & .                 & .             & o        & y            & .       & .              \\
        Homogeneous fibroglandular-echotexture & common                 & .                & .                 & .             & blue     & .            & .       & .              \\
               Homogeneous adipose-echotexture & common                 & .                & .                 & .             & .        & .            & .       & .              \\[1em]
                \multicolumn{1}{l}{Mass Shape} &                        &                  &                   &               &          &              &         &  \\
                                          Oval & common                 & .                & .                 & .             & .        & .            & .       & o              \\
                                         Round & common                 & .                & .                 & .             & .        & .            & .       & .              \\
                                     Irregular & probable               & .                & .                 & .             & .        & .            & .       & .              \\
                                       Iobular & probable               & .                & .                 & .             & .        & .            & .       & .              \\[1em]
          \multicolumn{1}{l}{Mass Orientation} &                        &                  &                   &               &          &              &         &  \\
                              Parallel to skin & common                 & .                & .                 & .             & blue     & .            & red     & red!50!black   \\
                          Non-parallel to skin & possible               & .                & .                 & .             & red      & red!30!black & blue    & blue!50!black 
\end{tabular}
\end{document}

输出

在此处输入图片描述

相关内容