3

I am new to Tikz and trying to draw a figure something like attached file. However, I am unable to get the desired result. My code is

\documentclass{article}
\usepackage{tikz}
\begin{document}
\def\resourceCapacity{(-4.5,-4) -- (-7,-4) -- (-7,-6) -- (-9,-6) node (v1) {} -- (-9,-8) -- (-11,-8) -- (-11,-10) -- (-14,-10) -- (-14,-12);}
\def\actualUse{plot[smooth, tension=.7] coordinates {(-4.5,-3.5) (-6.5,-4.5) (-8,-5.5) (-9,-7.5) (-10.5,-9) (-12,-10) (-14,-11.5)};}
\begin{tikzpicture}
\draw[color=cyan, very thick] \resourceCapacity {rc};
\draw [color=red, very thick] \actualUse {au};
\draw [yshift=1em, color=blue, ultra thick] \actualUse {fr};
\end{tikzpicture}
\end{document}

enter image description here

  • 1
    Welcome to TeX - LaTeX! May be tex.stackexchange.com/q/310987/15925 helps. – Andrew Swann Jan 31 '18 at 17:13
  • Is there some reason you are trying to do this with \def rather than putting the code in the picture? This is not at all a straightforward business and runs contrary to the design of TikZ. That is, you aren't supposed to do it this way and, partly as a result, it is much more difficult to do it this way. Also, \def should be avoided in LaTeX when possible as it much more dangerous than alternatives. – cfr Feb 1 '18 at 23:21
  • @cfr I am not bounded to use \def. I tried different sources and get what I have posted. I would be happy to achieve this either way. – Jitendra Feb 2 '18 at 7:38
3

I quickly gave it a try and I honestly have to admit that to reproduce such a picture in TikZ is not straightforward, especially for a beginner. Here your main friends are the pgf and the pgfplots packages manuals. It is too long to go in detail through each single feature used in my code and I think it is better for you to look for the commands you are not familiar with in the two manuals. Refer to pgf manual for all the commands I used, except that for \pgfplotsset and \tikzfillbetween, which are described in the pgfplots manual.

Note: I am aware that the code I propose may be improved (e.g. define the RCx coordinates in a \foreach loop) and it should rather consider as a starting point for a nicer result. Any constructive suggestion is, of course, welcome.

enter image description here

\documentclass[border=3mm]{standalone}
\usepackage[usenames, svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\pgfplotsset{
    compat=1.14,
    layers/my layer set/.define layer set={
        background,
        main,
    }{},
    set layers=my layer set,
}

\begin{document}
    \begin{tikzpicture}[>=stealth]
        %Axes
        \coordinate (O) at (0,0);
        \coordinate (x) at (9,0);
        \coordinate (y) at (0,6);
        \draw[->] (O) -- (x) node[below, pos=0.92] {Time};
        \draw[->] (O) -- (y) node[above, pos=0.80, rotate=90] {IT Resources};
        %IT resource capacity (only coordinates)
        \coordinate (RC0) at (0.2,0.5);
        \coordinate (RC1) at ([shift={(1.5,0)}]RC0);
        \coordinate (RC2) at ([shift={(0,1.5)}]RC1);
        \coordinate (RC3) at ([shift={(2.5,0)}]RC2);
        \coordinate (RC4) at ([shift={(0,1.5)}]RC3);
        \coordinate (RC5) at ([shift={(2.5,0)}]RC4);
        \coordinate (RC6) at ([shift={(0,1.5)}]RC5);
        \coordinate (RC7) at ([shift={(1.5,0)}]RC6);
        %Actual use of IT resources (only coordinates)
        \coordinate (AU0) at (0.2,0.2);
        \coordinate (AU1) at ([shift={(0,-0.1)}]RC1);
        \coordinate (AU2) at ({$(RC2)!0.45!(RC3)$} |- {$(RC1)!0.3!(RC2)$});
        \coordinate (AU3) at ($(RC2)!0.3!(RC3)$);
        \coordinate (AU4) at ({$(RC2)!0.4!(RC3)$} |- {$(RC3)!0.6!(RC4)$});
        \coordinate (AU5) at ($(RC3)!0.6!(RC4)$);
        \coordinate (AU6) at ({$(RC4)!0.8!(RC5)$} |- {$(RC3)!0.4!(RC4)$});
        \coordinate (AU7) at ($(RC4)!0.93!(RC5)$);
        \coordinate (AU8) at ($(RC5)!0.25!(RC6)$);
        \coordinate (AU9) at ({$(RC6)!0.3!(RC7)$} |- {$(RC5)!0.6!(RC6)$});
        \coordinate (AU10) at ({$(RC6)!0.6!(RC7)$} |- {$(RC5)!0.75!(RC6)$});
        \coordinate (AU11) at ([shift={(0,-0.2)}]RC7);
        %Drawing lines
        \draw[blue, very thick, name path=AU line] plot[smooth, tension=0.6] coordinates {(AU0) (AU1) (AU2) (AU3) (AU4) (AU5) (AU6) (AU7) (AU8) (AU9) (AU10) (AU11)};
        \draw[red, very thick, name path=RC line] (RC0) -- (RC1) -- (RC2) -- (RC3) -- (RC4) -- (RC5) -- (RC6) -- (RC7);
        %Filling
        \tikzfillbetween[on layer=background, of=RC line and AU line, split, every even segment/.style={Gold!50!Yellow}] {ForestGreen};
        %Labels
        \begin{scope}[font=\scriptsize]
            \node[above = 1.5 of RC2] (ov) {Overload};
            \draw[->] (ov.270) -- ({$(RC2)!0.65!(RC3)$} |- {$(RC3)!0.35!(RC4)$});
            \node[below = 1.5 of RC5, xshift=5mm] (rw) {\begin{tabular}{c} Resource \\ wastage\end{tabular}};
            \draw[->] (rw.135) -- ({$(RC4)!0.7!(RC5)$} |- {$(RC3)!0.6!(RC4)$});
            \node[above = of RC4] (rc) {\begin{tabular}{c} IT Resource \\ Capacity\end{tabular}};
            \draw[->, shorten >=1mm] (rc) -- ($(RC4)!0.45!(RC5)$);
            \node[below = 1.2 of RC3, xshift=3mm] (au) {\begin{tabular}{c} Actual Usage of \\ IT Resources\end{tabular}};
            \draw[->] (au.135) -- (AU2);
        \end{scope}
    \end{tikzpicture}
\end{document}

Make the code interact with overlays in beamer:

Imagining to desire to include the plot in a beamer frame and to make appear different parts on different overlays, the more direct way to achieve it is using the overlay-beamer-styles library of tikz. This basically provides the key visible on=<...> which can be used to hide or show parts of a tikzpicture.

Defining a new key

\tikzset{
    scope on/.style={
        every node/.append style={visible on=<#1>},
        every path/.append style={visible on=<#1>}
    }
}

one can basically show the content of scope on a given overlay. Clearly, the key visible on=<...> can be given here and there by hand (e.g. to a \draw command), but my approach can help sometimes to still have a kind of readable code.

enter image description here

\documentclass[usenames, svgnames]{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning,calc,overlay-beamer-styles}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\pgfplotsset{
    compat=1.14,
    layers/my layer set/.define layer set={
        background,
        main,
    }{},
    set layers=my layer set,
}

\tikzset{
    scope on/.style={every node/.append style={visible on=<#1>}, every path/.append style={visible on=<#1>}}
}

\begin{document}
    \begin{frame}
        \begin{tikzpicture}[>=stealth]
            %Axes
            \coordinate (O) at (0,0);
            \coordinate (x) at (9,0);
            \coordinate (y) at (0,6);
            \draw[->] (O) -- (x) node[below, pos=0.92] {Time};
            \draw[->] (O) -- (y) node[above, pos=0.80, rotate=90] {IT Resources};
            %IT resource capacity (only coordinates)
            \coordinate (RC0) at (0.2,0.5);
            \coordinate (RC1) at ([shift={(1.5,0)}]RC0);
            \coordinate (RC2) at ([shift={(0,1.5)}]RC1);
            \coordinate (RC3) at ([shift={(2.5,0)}]RC2);
            \coordinate (RC4) at ([shift={(0,1.5)}]RC3);
            \coordinate (RC5) at ([shift={(2.5,0)}]RC4);
            \coordinate (RC6) at ([shift={(0,1.5)}]RC5);
            \coordinate (RC7) at ([shift={(1.5,0)}]RC6);
            %Actual use of IT resources (only coordinates)
            \coordinate (AU0) at (0.2,0.2);
            \coordinate (AU1) at ([shift={(0,-0.1)}]RC1);
            \coordinate (AU2) at ({$(RC2)!0.45!(RC3)$} |- {$(RC1)!0.3!(RC2)$});
            \coordinate (AU3) at ($(RC2)!0.3!(RC3)$);
            \coordinate (AU4) at ({$(RC2)!0.4!(RC3)$} |- {$(RC3)!0.6!(RC4)$});
            \coordinate (AU5) at ($(RC3)!0.6!(RC4)$);
            \coordinate (AU6) at ({$(RC4)!0.8!(RC5)$} |- {$(RC3)!0.4!(RC4)$});
            \coordinate (AU7) at ($(RC4)!0.93!(RC5)$);
            \coordinate (AU8) at ($(RC5)!0.25!(RC6)$);
            \coordinate (AU9) at ({$(RC6)!0.3!(RC7)$} |- {$(RC5)!0.6!(RC6)$});
            \coordinate (AU10) at ({$(RC6)!0.6!(RC7)$} |- {$(RC5)!0.75!(RC6)$});
            \coordinate (AU11) at ([shift={(0,-0.2)}]RC7);
            %Drawing lines
            \draw[blue, very thick, name path=AU line, visible on=<2->] plot[smooth, tension=0.6] coordinates {(AU0) (AU1) (AU2) (AU3) (AU4) (AU5) (AU6) (AU7) (AU8) (AU9) (AU10) (AU11)};
            \draw[red, very thick, name path=RC line, visible on=<1->] (RC0) -- (RC1) -- (RC2) -- (RC3) -- (RC4) -- (RC5) -- (RC6) -- (RC7);
            %Filling
            \begin{scope}[scope on={3-}]
                \tikzfillbetween[on layer=background, of=RC line and AU line, split, every even segment/.style={Gold!50!Yellow}] {ForestGreen};
            \end{scope}
            %Labels
            \begin{scope}[font=\scriptsize]
                \begin{scope}[scope on={3-}]
                    \node[above = 1.5 of RC2] (ov) {Overload};
                    \draw[->] (ov.270) -- ({$(RC2)!0.65!(RC3)$} |- {$(RC3)!0.35!(RC4)$});
                    \node[below = 1.5 of RC5, xshift=5mm] (rw) {\begin{tabular}{c} Resource \\ wastage\end{tabular}};
                    \draw[->] (rw.135) -- ({$(RC4)!0.7!(RC5)$} |- {$(RC3)!0.6!(RC4)$});
                \end{scope}
                \begin{scope}[scope on={1-}]
                    \node[above = of RC4] (rc) {\begin{tabular}{c} IT Resource \\ Capacity\end{tabular}};
                    \draw[->, shorten >=1mm] (rc) -- ($(RC4)!0.45!(RC5)$);
                \end{scope}
                \begin{scope}[scope on={2-}]
                    \node[below = 1.2 of RC3, xshift=3mm] (au) {\begin{tabular}{c} Actual Usage of \\ IT Resources\end{tabular}};
                    \draw[->] (au.135) -- (AU2);
                \end{scope}
            \end{scope}
        \end{tikzpicture}
    \end{frame}
\end{document}
  • Hi @Axel, I tried the \pause for overlay effect but it does produce an unwanted effect. Order of overlay I want is (1) IT Resource Capacity, (2) Actual Usage, (3) Filling part (yellow and green). Can you help me to achieve this as well. – Jitendra May 22 '18 at 11:51
  • @Jitendra I added a possible approach to use my code with overlays. You did not say if you needed to use beamer as class, I just assumed it. – Axel Krypton May 24 '18 at 14:59

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.