根据 KPI 报告的值选择颜色

根据 KPI 报告的值选择颜色

我试图制作一份包含彩色球体的报表,该报表应包含在命令中并动态更新。我定义了一个彩色球体,它接收颜色、值和文本。颜色由值确定,我使用一系列 ifthen 进行选择。问题是我无法让球体接受带有颜色参考的字符串。

\documentclass[10pt]{article}
\usepackage[a4paper,margin=2.54cm,bmargin=2.04cm,headheight=2.54cm,headsep=0.5cm]{geometry}

\usepackage{tikz}
\usetikzlibrary{calc,shapes,positioning}
\usepackage{eso-pic}
\usepackage{xcolor}
\usepackage{calc}
\usepackage{ifthen}
\usepackage{xparse}
\usepackage{fp}
\usepackage{pgfplots}
\pgfplotsset{compat=newest,width=4cm}
\usepgfplotslibrary{patchplots,polar}
\usepackage[many]{tcolorbox}
\usepackage[absolute]{textpos}

\definecolor{newgreen}{RGB}{173,194,0}%
\definecolor{neworange}{RGB}{217,109,0}% 
\definecolor{newyellow}{RGB}{239,186,0}% 
\definecolor{newred}{RGB}{180,30,10}% 
\definecolor{newgrey}{RGB}{77,77,77}% 


\begin{document}

%%% Here we make colored sphere %%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\gradientcircle}[3]{%
\pgfdeclareradialshading{ballshading}%
{\pgfpoint{-10bp}{10bp}}{%
    color(0bp)=(#1!45!white);% 
    color(9bp)=(#1!75!white);%
    color(18bp)=(#1!70!black);% 
    color(25bp)=(#1!50!black);%
    color(50bp)=(#1!15!black)}%
\begin{tikzpicture}%
\pgfpathcircle{\pgfpoint{0cm}{0cm}}{1.25cm}%
\pgfshadepath{ballshading}{280}%
\pgfusepath{}%
\node{\textbf{\textcolor{newgrey!5!white}{\Huge#2}}};%
\end{tikzpicture}\par\smallskip%
{\scriptsize #3}%
}%


%%% Here we make the color selection %%%%%%%%%%%%%%%%%%%%%%%

\def\thresholdi{50}
\def\thresholdii{75}
\def\thresholdiii{90}

\newcommand{\TestColor}[1]{%
\ifthenelse{\NOT#1=0}{%
    \ifthenelse{#1>\thresholdi}{%
        \ifthenelse{#1>\thresholdii}{%
            \ifthenelse{#1>\thresholdiii}{newgreen%
            }{newyellow}%
        }{neworange}%
    }{newred}%
}{newgrey}%
}   


%%% Here we define headers and more %%%%%%%%%%%%%%%%%%%%%%%%

\def\statusitext  {Text 1}
\def\statusiitext {Text 2}
\def\statusiiitext{Text 3}
\def\statusivtext {Text 4}
\def\statusvtext  {Text 5}


%%% Here we get the current KPI value %%%%%%%%%%%%%%%%%%%%%%

\def\testi{49}
\def\testii{74}
\def\testiii{85}
\def\testiv{96}
\def\testv{0}


%%% Here we make the reporting balls dynamics %%%%%%%%%%%%%%

\newcommand{\statusi}{\gradientcircle{blue}{\testi}{\statusitext}}
\newcommand{\statusii}{\gradientcircle{\TestColor{\testii}}{\testi}{\statusiitext}}

%\newcommand{\statusii} {\gradientcircle{\TestColor{\testii}}{\testii}{\statusiitext}}
%\newcommand{\statusiii}{\gradientcircle{\TestColor{\testiii}}{\testiii}{\statusiiitext}}
%\newcommand{\statusiv} {\gradientcircle{\TestColor{\testiv}}{\testiv}{\statusivtext}}
%\newcommand{\statusv}  {\gradientcircle{\TestColor{\testi}}{\testv}{\statusvtext}}


% Here we test the color selection %%%%%%%%%%%%%%%%%%%%%%%%%

\testi{} yields \TestColor{\testi} \par
\testii{} yields \TestColor{\testii} \par
\testiii{} yields \TestColor{\testiii} \par
\testiv{} yields \TestColor{\testiv} \par
\testv{} yields \TestColor{\testv} \par


% Here we test the colored spheres %%%%%%%%%%%%%%%%%%%%%%%%%

\vskip 24pt
Hard code color, value and text

\gradientcircle{black}{123}{Test}


\vskip 24pt
Hard code color, dynamic value and text

\statusi


% Here is the problem %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\vskip 24pt
Dynamic color, value and text

\statusii

\end{document}

答案1

我认为最简单的方法可能是定义一个颜色newcol,然后对其进行适当的设置。这可以避免在实际测试时尝试处理颜色设置所导致的问题。

稍微简化一下这个例子:

\documentclass{article}
\usepackage{tikz}
\usepackage{ifthen}
\definecolor{newgreen}{RGB}{173,194,0}
\definecolor{neworange}{RGB}{217,109,0}
\definecolor{newyellow}{RGB}{239,186,0}
\definecolor{newred}{RGB}{180,30,10}
\definecolor{newgrey}{RGB}{77,77,77}
\colorlet{newcol}{black}
\begin{document}
%%% Here we make colored sphere %%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\gradientcircle}[3]{%
  \pgfdeclareradialshading{ballshading}%
  {\pgfpoint{-10bp}{10bp}}{%
    color(0bp)=(#1!45!white);%
    color(9bp)=(#1!75!white);%
    color(18bp)=(#1!70!black);%
    color(25bp)=(#1!50!black);%
    color(50bp)=(#1!15!black)}%
  \begin{tikzpicture}%
    \pgfpathcircle{\pgfpoint{0cm}{0cm}}{1.25cm}%
    \pgfshadepath{ballshading}{280}%
    \pgfusepath{}%
    \node{\textbf{\textcolor{newgrey!5!white}{\Huge#2}}};%
  \end{tikzpicture}\par\smallskip%
  {\scriptsize #3}%
}
\def\thresholdi{50}
\def\thresholdii{75}
\def\thresholdiii{90}
\newcommand{\TestColor}[1]{%
  \ifthenelse{\NOT#1=0}{%
    \ifthenelse{#1>\thresholdi}{%
      \ifthenelse{#1>\thresholdii}{%
        \ifthenelse{#1>\thresholdiii}{%
          \colorlet{newcol}{newgreen}%
        }{%
          \colorlet{newcol}{newyellow}%
        }%
      }{%
        \colorlet{newcol}{neworange}%
      }%
    }{%
      \colorlet{newcol}{newred}%
    }%
  }{%
    \colorlet{newcol}{newgrey}%
  }%
}
\def\statusitext  {Text 1}
\def\statusiitext {Text 2}
\def\testi{49}
\def\testii{74}
\def\testiii{85}
\def\testiv{96}
\def\testv{0}
\newcommand{\statusi}{\gradientcircle{blue}{\testi}{\statusitext}}
\newcommand{\statusii}{\TestColor{\testii}\gradientcircle{newcol}{\testi}{\statusiitext}}
\testi{} yields \TestColor{\testi} \par
\testii{} yields \TestColor{\testii} \par
\testiii{} yields \TestColor{\testiii} \par
\testiv{} yields \TestColor{\testiv} \par
\testv{} yields \TestColor{\testv} \par

\vskip 24pt
Hard code color, value and text

\gradientcircle{black}{123}{Test}

\vskip 24pt
Hard code color, dynamic value and text

\statusi

\vskip 24pt
Dynamic color, value and text

\statusii

\end{document}

颜色选择

但是,有比使用更简单的测试方法ifthen。例如,您可以使用 PGF/TikZ 本身来处理这些内容,因为无论如何您都会加载它。(但我想我更可能使用 TeX 的东西。我从来都不喜欢 LaTeX 等价物的笨拙,尽管我猜它们更安全。)

相关内容