\usepackage{tikz}
\usetikzlibrary{patterns, arrows}
\begin{document}
\begin{tikzpicture}
% Set the width and height of the rectangles
\pgfmathsetmacro{\rectHeight}{1} % Height of all rectangles
\pgfmathsetmacro{\totalWidth}{8} % Total width of the bottom rectangle
\pgfmathsetmacro{\toprecPercentage}{0.8} % Width of the top rectangle as a percentage of the bottom rectangle
% Define the number of rectangles
\def\numRectangles{9}
% Define an array of descriptions for the arrows
\def\descriptions{{"Desc 0", "Desc 1", "Desc 2", "Desc 3", "Desc 4", "Desc 5", "Desc 6", "Desc 7", "Desc 8"}}
% Define an array of segment counts
\def\segmentCounts{{3, 9, 1, 1, 1, 1, 1, 1, 1}} % Number of segments for each rectangle
% Define segmentFillPercentages for each rectangle
\def\segmentFillPercentages{
{70, 30, 50}, % Customize these percentages for rectangle 0
{20, 40, 60, 80, 10, 30, 50, 70, 90}, % Customize these percentages for rectangle 1
{10}, % Customize these percentages for rectangle 2
{40}, % Customize these percentages for rectangle 3
{20}, % Customize these percentages for rectangle 4
{10}, % Customize these percentages for rectangle 5
{40}, % Customize these percentages for rectangle 6
{20}, % Customize these percentages for rectangle 7
{100} % Customize these percentages for rectangle 8
}
% Calculate the width of the top rectangle
\pgfmathsetmacro{\toprecWidth}{\toprecPercentage*\totalWidth}
% Draw the rectangles and fill them with different patterns based on patterns based on fill percentages and segment counts
\foreach \i in {0,...,\numexpr\numRectangles-1} {
\pgfmathsetmacro{\currentWidth}{\totalWidth - (\i/\numRectangles)*\toprecWidth}
\pgfmathsetmacro{\startingPoint}{0.5*(\totalWidth-\currentWidth)} % Calculate the starting point
% Draw segments within each rectangle
\pgfmathsetmacro{\segmentCount}{\segmentCounts[\i]}
\pgfmathsetmacro{\segmentWidth}{\currentWidth/\segmentCount}
% Get the fill percentages for the current rectangle
\pgfmathparse{\segmentFillPercentages[\i]}
\let\currentSegmentFillPercentages\pgfmathresult
% Draw and fill each segment with different fill percentages
\foreach \j in {1,...,\segmentCount} {
\pgfmathparse{\currentSegmentFillPercentages[\j-1]}
\let\segmentFillPercentage\pgfmathresult
\pgfmathsetmacro{\segmentStartingPoint}{\startingPoint + (\j-1)*\segmentWidth}
\draw (\segmentStartingPoint,\rectHeight*\i) rectangle (\segmentStartingPoint + \segmentWidth,\rectHeight*\i+\rectHeight);
\fill[pattern=grid,pattern color=redpattern=grid,pattern color=red] (\segmentStartingPoint,\rectHeight*\i) rectangle (\segmentStartingPoint + \segmentWidth,\rectHeight*\i+(\segmentFillPercentage/100)*\rectHeight);
}
% Add arrows and descriptions within the loop
\draw[->] (\startingPoint + \currentWidth + 0.2, \rectHeight*\i + 0.5*\rectHeight) -- (\startingPoint + \currentWidth + 1.0, \rectHeight*\i + 0.5*\rectHeight) node[right,font=\footnotesize,text width=2cm] {\pgfmathparse{\descriptions[\i]}\pgfmathresult};
}
\end{tikzpicture}
\end{document}
Trying different methods to get the fillcolor for the segments but unable to get it correct. Any help on solving this issue is much appericiated.


pattern=grid,pattern color=redpattern=grid,pattern color=redis a typo/copy-paste error. You've got some troubles accessing the arrays because of the number of braces needed for the<array>[<index>]syntax to work. Are you set on using these arrays? I always find them cumbersome to use. – Qrrbrbirlbel Sep 28 '23 at 19:01