2

I want to create something like this, where the percentage of the colored rectangle is an input variable.

enter image description here

and color of the rectangle is changing, for <33% is red, <66% is yellow and green for >67%.

This is what I've got so far.

\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}

I'm sure there is much more elegant way to solve this. This doesn't work for percentages below 30.

Also my color rectangle is centered and not aligned.

3

I hope I understood the question correctly.

\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}

enter image description here

or

\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}

enter image description here

Or just changing two lines:

\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}

enter image description here

  • I just need one color, which is based on the input percentage.. so if you have 30%, than rectangle will be red color, if it is 70% rectangle will be green. I managed to do that with ifthenelse command. However, my problem is the size of the rectangle. It needs to be dynamically adjusted based on that percentage. So I'm trying to figure out is it possible to have if percentage is 70%, to have a rectangle filled with green color that is 0.7 of the length of whole rectangle and aligned.. – Almic7 Jun 26 at 15:11
  • @Almic7 OK, I updated it. – user121799 Jun 26 at 15:17
  • thank you.. sorry for asking again, but the length of green or red part needs to change too.. not just the color.. this means that white part is changing based on the percentage.. – Almic7 Jun 26 at 15:22
2

something silimar but with a 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}

enter image description here

1

It is not very clear how you like to present "Satisfactory Match". IN aproach, which I as it can be understand, it works only between range of 25% to 90%, outside of this the result is strange:

enter image description here

(red lines indicate text borders)

MWE, for above results is:

\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}

To my opinion (and taste) the following result is more meaningful:

enter image description here

which is generated by

\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}

Addendum: For shorter document code you can define new command as is the next 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}

Result is as in the second example.

  • 1
    This is to a large extent copied (at the conceptual level) from the already existing answer and has the disadvantage that it only works for integers. – user121799 Jun 28 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.