我想创建类似的东西,其中彩色矩形的百分比是一个输入变量。
并且矩形的颜色正在变化,<33%为红色,<66%为黄色,>67%为绿色。
这是我目前所得到的。
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{fit,calc,positioning}
\usetikzlibrary{shapes.geometric}
\usepackage{ifthen}
\definecolor{textgray}{HTML}{525252}
\edef\mycolorr{red} \edef\mycolory{yellow} \edef\mycolorg{green}
\begin{document}
%% Set satisfactory value
\newcounter{satisfactoryvalue}
\setcounter{satisfactoryvalue}{85} %% here place percentage
\newcommand{\myfactor}{0.85} %% here place scaling factor
\newcommand{\scaletomyfactor}[1]{%
\csname rem@pt\expandafter\endcsname\the\dimexpr #1\dimexpr\myfactor pt\relax\relax }
\newlength{\duz} \setlength{\duz}{16cm}
\ifthenelse{\value{satisfactoryvalue}<33}{\edef\mycolorf{\mycolorr}\setcounter}{\ifthenelse{\value{satisfactoryvalue}<66}{\edef\mycolorf{\mycolory}}{\edef\mycolorf{\mycolorg}}}
\begin{center}
\begin{tikzpicture}
\node at (0,0) [draw, rectangle,
minimum width=\duz, minimum height=1.5cm, textgray] (r1) {};
\node at (r1) [fill, rectangle, \mycolorf, minimum
width=\scaletomyfactor{\myfactor}\duz, minimum height=1.5cm] (r2) {};
\end{tikzpicture}
\end{center}
\end{document}
我确信有更优雅的方法来解决这个问题。对于低于 30 的百分比,这不起作用。
此外,我的颜色矩形居中且未对齐。
答案1
我希望我正确理解了这个问题。
\documentclass[a4paper]{article}
\usepackage{tikz}
\definecolor{textgray}{HTML}{525252}
\newlength{\duz}
\begin{document}
\setlength{\duz}{11cm}
\edef\mycolorr{red} \edef\mycolory{yellow} \edef\mycolorg{green}
\edef\LstColors{"red","yellow","green"}
\begin{center}
\begin{tikzpicture}[pics/dynamic rectangle/.style n args={2}{code={
\pgfmathsetmacro{\pft}{#2/100}
\pgfmathsetmacro{\pfft}{1-\pft}
\node[anchor=west,draw, rectangle, minimum width=\pfft\duz, minimum height=1.5cm,
text=textgray,font=\large\sffamily] at (0,0) (l1) {#1};
\pgfmathsetmacro{\mycolor}{{\LstColors}[int(3*#2/100)]}
\node[anchor=west,fill=\mycolor,draw, rectangle, minimum width=\pft\duz, minimum height=1.5cm,
font=\Large\sffamily\bfseries,text depth=1.25em] (r1) at (l1.east){#2\%};
\node[anchor=south,font=\small\sffamily] at (r1.south) {Satisfactory Match};
}}]
\pic{dynamic rectangle={Sales role}{54}};
\pic at (0,-2) {dynamic rectangle={Sales role}{32}};
\pic at (0,-4) {dynamic rectangle={Sales role}{34}};
\pic at (0,-6) {dynamic rectangle={Sales role}{66}};
\pic at (0,-8) {dynamic rectangle={Sales role}{67}};
\end{tikzpicture}
\end{center}
\end{document}
或者
\documentclass[a4paper]{article}
\usepackage{tikz}
\definecolor{textgray}{HTML}{525252}
\newlength{\duz}
\begin{document}
\setlength{\duz}{11cm}
\edef\mycolorr{red} \edef\mycolory{yellow} \edef\mycolorg{green}
\edef\LstColors{"red","yellow","green"}
\begin{center}
\begin{tikzpicture}[pics/dynamic rectangle/.style n args={2}{code={
\pgfmathsetmacro{\pft}{1/3+#2/300}
\pgfmathsetmacro{\pfft}{1-\pft}
\node[anchor=west,draw, rectangle, minimum width=\pfft\duz, minimum height=1.5cm,
text=textgray,font=\large\sffamily] at (0,0) (l1) {#1};
\pgfmathsetmacro{\mycolor}{{\LstColors}[int(3*#2/100)]}
\node[anchor=west,fill=\mycolor,draw, rectangle, minimum width=\pft\duz, minimum height=1.5cm,
font=\Large\sffamily\bfseries,text depth=1.25em] (r1) at (l1.east){#2\%};
\node[anchor=south,font=\small\sffamily] at (r1.south) {Satisfactory Match};
}}]
\pic{dynamic rectangle={Sales role}{54}};
\pic at (0,-2) {dynamic rectangle={Sales role}{32}};
\pic at (0,-4) {dynamic rectangle={Sales role}{34}};
\pic at (0,-6) {dynamic rectangle={Sales role}{66}};
\pic at (0,-8) {dynamic rectangle={Sales role}{67}};
\end{tikzpicture}
\end{center}
\end{document}
或者只改变两行:
\documentclass[a4paper]{article}
\usepackage{tikz}
\definecolor{textgray}{HTML}{525252}
\newlength{\duz}
\begin{document}
\setlength{\duz}{11cm}
\edef\mycolorr{red} \edef\mycolory{yellow} \edef\mycolorg{green}
\edef\LstColors{"red","yellow","green"}
\begin{center}
\begin{tikzpicture}[pics/dynamic rectangle/.style n args={2}{code={
\pgfmathsetmacro{\pft}{1/3+#2/300}
\pgfmathsetmacro{\pfft}{1-\pft}
\node[anchor=west,draw, rectangle, minimum width=0.333\duz, minimum height=1.5cm,outer sep=0pt,
text=textgray,font=\large\sffamily] at (0,0) (l1) {#1};
\draw (l1.north east) rectangle (\duz,0|-l1.south);
\pgfmathsetmacro{\mycolor}{{\LstColors}[int(3*#2/100)]}
\node[anchor=west,fill=\mycolor,draw, rectangle, minimum width=\pft\duz, minimum height=1.5cm,
font=\Large\sffamily\bfseries,text depth=1.25em] (r1) at (l1.east){#2\%};
\node[anchor=south,font=\small\sffamily] at (r1.south) {Satisfactory Match};
}}]
\pic{dynamic rectangle={Sales role}{54}};
\pic at (0,-2) {dynamic rectangle={Sales role}{32}};
\pic at (0,-4) {dynamic rectangle={Sales role}{34}};
\pic at (0,-6) {dynamic rectangle={Sales role}{66}};
\pic at (0,-8) {dynamic rectangle={Sales role}{67}};
\end{tikzpicture}
\end{center}
\end{document}
答案2
类似的东西,但是带有sidebyside
tcolorbox:
\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lmodern}
\newcommand{\salesone}[3][]{
\tcbsidebyside[%
notitle, sidebyside, bicolor, sharp corners,
boxrule=1pt, colback=white, colbacklower=#2,
fontupper=\sffamily\Large,
fontlower=\sffamily,
righthand ratio=#3,
halign upper=center, halign lower=center,
code={\pgfmathsetmacro{\valor}{int(#3*100)}},
#1
]{Sales role}{{\bfseries \valor\%}\\ Satisfactory Match}
}
\newcommand{\salestwo}[3][]{
\tcbsidebyside[%
title = Sales role,
fonttitle=\sffamily,
halign title=center,
sidebyside, bicolor, sharp corners,
boxrule=1pt, colback=white, colbacklower=#2,
fontupper=\sffamily\Large,
fontlower=\sffamily,
righthand ratio=#3,
halign upper=center, halign lower=center,
code={\pgfmathsetmacro{\valor}{int(#3*100)}},
#1
]{}{{\bfseries \valor\%}\\ Satisfactory Match}
}
\begin{document}
\salesone{red}{.66}
\salesone{blue}{.30}
\salesone{green!10}{.45}
\salesone{orange}{.75}
\salestwo{red}{.66}
\salestwo{blue}{.30}
\salestwo{green!10}{.45}
\salestwo{orange}{.75}
\end{document}
答案3
不太清楚您喜欢如何呈现“满意匹配”。在我看来,它只在 25% 到 90% 的范围内有效,除此之外,结果很奇怪:
(红线表示文本边框)
上述结果的 MWE 为:
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
\newcommand\satisfactory{\ifnum\myfactor<33 red\else\ifnum\myfactor<66 yellow\else green\fi\fi}
\tikzset{
node distance = 0pt,
box/.style 2 args = {draw,
fill=\satisfactory,
text width=#1,
minimum height=15mm, inner sep=0pt, outer sep=0pt,
align=center,
font=\Large\sffamily\linespread{0.7}\selectfont, text=#2},
}
\begin{document}
\begin{center}
\def\myfactor{15}
\begin{tikzpicture}
\node (r1) [box={\linewidth-(\myfactor/100)*\linewidth}{gray}, fill=none]
{\textbf{Sales role}};
\node (r2) [box={(\myfactor/100)*\linewidth}{black}, right=of r1]
{\textbf{\myfactor\,\%}\\
\normalsize{Satisfactory Match}};
\end{tikzpicture}
\bigskip
\def\myfactor{50}
\begin{tikzpicture}
\node (r1) [box={\linewidth-(\myfactor/100)*\linewidth}{gray}, fill=none]
{\textbf{Sales role}};
\node (r2) [box={(\myfactor/100)*\linewidth}{black}, right=of r1]
{\textbf{\myfactor\,\%}\\
\normalsize{Satisfactory Match}};
\end{tikzpicture}
\bigskip
\def\myfactor{85}
\begin{tikzpicture}
\node (r1) [box={\linewidth-(\myfactor/100)*\linewidth}{gray}, fill=none]
{\textbf{Sales role}};
\node (r2) [box={(\myfactor/100)*\linewidth}{black}, right=of r1]
{\textbf{\myfactor\,\%}\\
\normalsize{Satisfactory Match}};
\end{tikzpicture}
\bigskip
\def\myfactor{90}
\begin{tikzpicture}
\node (r1) [box={\linewidth-(\myfactor/100)*\linewidth}{gray}, fill=none]
{\textbf{Sales role}};
\node (r2) [box={(\myfactor/100)*\linewidth}{black}, right=of r1]
{\textbf{\myfactor\,\%}\\
\normalsize{Satisfactory Match}};
\end{tikzpicture}
\bigskip
\def\myfactor{100}
\begin{tikzpicture}
\node (r1) [box={\linewidth-(\myfactor/100)*\linewidth}{gray}, fill=none]
{\textbf{Sales role}};
\node (r2) [box={(\myfactor/100)*\linewidth}{black}, right=of r1]
{\textbf{\myfactor\,\%}\\
\normalsize{Satisfactory Match}};
\end{tikzpicture}
\end{center}
\end{document}
在我看来(和品味),以下结果更有意义:
由
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
\newcommand\satisfactory{\ifnum\myfactor<33 red\else\ifnum\myfactor<66 yellow\else green\fi\fi}
\tikzset{
node distance = 0pt,
box/.style args = {#1/#2}{draw,
text width=#1\linewidth,
minimum height=15mm, inner sep=0pt, outer sep=0pt,
align=#2,
font=\Large\sffamily\bfseries},
smb/.style = {fill=\satisfactory,
text width=(\myfactor/100)*0.75\linewidth,
minimum height=15mm, inner sep=0pt, outer sep=0pt,
node contents={}},
}
\begin{document}
\begin{center}
\def\myfactor{15}
\begin{tikzpicture}
\node (r1) [box=0.25/center, text=gray] {Sales role};
\node [smb, right=of r1];
\node [box=0.75/left, text=black,
right=of r1] {~\textbf{\myfactor\,\%}\\
~\normalsize{Satisfactory Match}};
\end{tikzpicture}
\bigskip
\def\myfactor{50}
\begin{tikzpicture}
\node (r1) [box=0.25/center, text=gray] {Sales role};
\node [smb, right=of r1];
\node [box=0.75/left, text=black,
right=of r1] {~\textbf{\myfactor\,\%}\\
~\normalsize{Satisfactory Match}};
\end{tikzpicture}
\bigskip
\def\myfactor{85}
\begin{tikzpicture}
\node (r1) [box=0.25/center, text=gray] {Sales role};
\node [smb, right=of r1];
\node [box=0.75/left, text=black,
right=of r1] {~\textbf{\myfactor\,\%}\\
~\normalsize{Satisfactory Match}};
\end{tikzpicture}
\bigskip
\def\myfactor{90}
\begin{tikzpicture}
\node (r1) [box=0.25/center, text=gray] {Sales role};
\node [smb, right=of r1];
\node [box=0.75/left, text=black,
right=of r1] {~\textbf{\myfactor\,\%}\\
~\normalsize{Satisfactory Match}};
\end{tikzpicture}
\bigskip
\def\myfactor{100}
\begin{tikzpicture}
\node (r1) [box=0.25/center, text=gray] {Sales role};
\node [smb, right=of r1];
\node [box=0.75/left, text=black,
right=of r1] {~\textbf{\myfactor\,\%}\\
~\normalsize{Satisfactory Match}};
\end{tikzpicture}
\end{center}
\end{document}
附录: 对于较短的文档代码,您可以像下一个 MWE 一样定义新命令
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
\newcommand\satisfactory{\ifnum\myfactor<33 red\else\ifnum\myfactor<66 yellow\else green\fi\fi}
\tikzset{
node distance = 0pt,
box/.style args = {#1/#2}{draw,
text width=#1\linewidth,
minimum height=15mm, inner sep=0pt, outer sep=0pt,
align=#2,
font=\Large\sffamily\bfseries},
smb/.style = {fill=\satisfactory,
text width=(\myfactor/100)*0.75\linewidth,
minimum height=15mm, inner sep=0pt, outer sep=0pt,
node contents={}},
}
\newcommand\SalesRole[1]%
{
\def\myfactor{#1}
\begin{tikzpicture}
\node (r1) [box=0.25/center, text=gray] {Sales role};
\node [smb, right=of r1];
\node [box=0.75/left, text=black,
right=of r1] {~\textbf{\myfactor\,\%}\\
~\normalsize{Satisfactory Match}};
\end{tikzpicture}
}
\begin{document}
\begin{center}
\SalesRole{15}
\bigskip
\SalesRole{50}
\bigskip
\SalesRole{85}
\bigskip
\SalesRole{90}
\bigskip
\SalesRole{100}
\end{center}
\end{document}
结果与第二个示例相同。