\documentclass{article}

% Packages necessary for common.tex
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{pgfplots.dateplot}

% Other packages
\usepackage[a4paper, total={12cm, 25cm}]{geometry}
\usepackage{float}
\usepackage{calc}
\usepackage{ifthen}
\usepackage{tikz}
\usepackage[l3]{csvsimple}
\usepackage{subcaption}
\usepackage{lmodern,textcomp}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Set common options %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\input{common.tex}
\pgfplotsset{colorscheme/rocket}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Define Custom Commands %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\makeatletter
\newcommand*\shortyear[1]{\expandafter\@gobbletwo\number\numexpr#1\relax}
\makeatother

\newcommand{\slice}[6]{
	\pgfmathparse{0.5*#1+0.5*#2}
	\let\midangle\pgfmathresult

	% slice
	\fill[thick,color=#5] (0,0) -- (#1:1) arc (#1:#2+1:1) -- (0,0);

	% outer label
	\node[label=\midangle:#4] at (\midangle:1) {};

	% inner label
	\pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
	\let\temp\pgfmathresult
	\pgfmathparse{max(\temp,-0.5) + 0.8}
	\let\innerpos\pgfmathresult
	\node[color=#6] at (\midangle:\innerpos) {#3};
}

\newcounter{pieElem}
\newcounter{pieSliceA}
\newcounter{pieSliceB}
\newcommand{\pie}[1]{
	% Count elements
	\setcounter{pieElem}{0}%
	\foreach\pieElem in {#1}{\stepcounter{pieElem}}%
	\edef\numElements{\arabic{pieElem}}

	% Draw pie chart
	\setcounter{pieSliceA}{0}%
	\setcounter{pieSliceB}{0}%
	\foreach \xi/\t [count=\xk from 0] in {#1} {
		% Get colors
		\pgfmathparse{1000 / (\numElements - 1) * \xk}
		\extractcolormapcolor{pieColor\xk}{\pgfmathresult}
		\pgfmathparse{(\xk / \numElements < 0.5)*1000}
		\extractcolormapcolor{pieTextColor\xk}{\pgfmathresult}

		% Draw slice
		\setcounter{pieSliceA}{\thepieSliceB}
		\addtocounter{pieSliceB}{\xi}
		\slice{\thepieSliceA/100*360}{\thepieSliceB/100*360}{\xi\%}{\t}{pieColor\xk}{pieTextColor\xk}
	}
}

\newcommand{\csvPie}[1]{
	% Count elements
	\setcounter{pieElem}{0}%
	\csvreader[head to column names]{#1}{}{%
		\stepcounter{pieElem}
	}
	\edef\numElements{\arabic{pieElem}}

	% Draw pie chart
	\setcounter{pieElem}{0}%
	\setcounter{pieSliceA}{0}%
	\setcounter{pieSliceB}{0}%
	\csvreader[head to column names]{#1}{}{%
		% Get colors
		\pgfmathparse{1000 / (\numElements - 1) * \thepieElem}
		\extractcolormapcolor{pieColor\thepieElem}{\pgfmathresult}
		\pgfmathparse{(\thepieElem / \numElements < 0.5)*1000}
		\extractcolormapcolor{pieTextColor\thepieElem}{\pgfmathresult}

		% Draw slice
		\setcounter{pieSliceA}{\thepieSliceB}
		\addtocounter{pieSliceB}{10*\real{\value}}
		\slice{\thepieSliceA/1000*360}{\thepieSliceB/1000*360}{\value\%}{\category}
			{pieColor\thepieElem}{pieTextColor\thepieElem}

		\stepcounter{pieElem}
	}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Actual Document %%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\title{Expense Report\vspace{-1cm}}
\author{}


\begin{document}

\maketitle
\vspace{1cm}

\begin{figure}[H]
	\centering

	\begin{tikzpicture}[scale=2.5]
		\csvPie{category_overview.csv}
	\end{tikzpicture}

	\caption{Total percenteage of expenses by category}
\end{figure}

\begin{figure}[H]
	\centering

	\begin{subfigure}[t]{\textwidth}
		\centering

		\begin{tikzpicture}
			\begin{axis}[
				date coordinates in=x,
				width=\textwidth,
				height=0.375\textwidth,
				ylabel={Net income in €},
				y label style={at={(-0.1,0.5)},anchor=south},
				xticklabel=\month.\shortyear{\year},
				xtick=data,
				xticklabel style={
					rotate=60,
					anchor=near xticklabel,
				},
				grid,
				enlarge x limits=0.03,
			]
				% Dummy plot to set x axis ticks
				\addplot[draw=none]
					table[col sep=comma, x=t, y=net]
						{net_income.csv};

				% Dummy plot to set x axis scale
				\addplot[draw=none]
					table[col sep=comma, x=t, y expr=0]
						{detailed_balance.csv};

				\addplot[ybar, bar width=0.4cm, draw=none, fill=scol2!30, line width=1pt]
					table[col sep=comma, x=t, y=income]
						{net_income.csv};
				\addplot[ybar, bar width=0.4cm, draw=none, fill=scol0!30, line width=1pt]
					table[col sep=comma, x=t, y=expenses]
						{net_income.csv};
                \addplot[ybar, bar width=0.3cm, draw=none, fill=scol0, line width=1pt]
					table[col sep=comma, x=t, y=net, discard if gt={net}{0}]
						{net_income.csv};
                \addplot[ybar, bar width=0.3cm, draw=none, fill=scol2, line width=1pt]
					table[col sep=comma, x=t, y=net, discard if lt={net}{0}]
						{net_income.csv};
			\end{axis}
		\end{tikzpicture}
	\end{subfigure}
	\begin{subfigure}[t]{\textwidth}
		\centering

		\begin{tikzpicture}
			\begin{axis}[
				date coordinates in=x,
				width=\textwidth,
				height=0.375\textwidth,
				ylabel={Total balance in €},
				y label style={at={(-0.1,0.5)},anchor=south},
				xticklabel=\month.\shortyear{\year},
				xtick=data,
				enlarge x limits=0.03,
				xticklabel style={
					rotate=60,
					anchor=near xticklabel,
				},
				grid,
			]
				% Dummy plot to set x axis ticks
				\addplot[draw=none]
					table[col sep=comma, x=t, y=value]
						{total_value.csv};

				\addplot[scol3, mark=none, line width=1pt]
					table[col sep=comma, x=t, y=value]
						{detailed_balance.csv};
				\addplot[scol1, mark=none, line width=1pt]
					table[col sep=comma, x=t, y=value]
						{total_value.csv};
			\end{axis}
		\end{tikzpicture}
	\end{subfigure}

	\caption{Development of account balance over time}
\end{figure}

\begin{figure}
	\centering

	
	\csvautotabular{net_income.csv}
\end{figure}


\begin{figure}[H]
	\centering

	% Read table
	\pgfplotstableread[col sep=comma]{expenses_by_category.csv}\expbycattable
	\pgfplotstablegetcolsof{\expbycattable}
	\pgfmathtruncatemacro\NumCols{\pgfplotsretval-1}

	\begin{subfigure}[c]{\textwidth}
		\centering

		\begin{tikzpicture}
			\begin{axis}[
				stack plots=y,
				area style,
				date coordinates in=x,
				width=\textwidth,
				height=0.375\textwidth,
				xticklabel=\month.\shortyear{\year},
				xtick=data,
				enlargelimits=false,
				xticklabel style={
					rotate=60,
					anchor=near xticklabel,
				},
				legend columns=5,
				legend style={at={(0.5,-0.6)},anchor=south},
				ylabel={Expenses in €},
				ymin=0,
			]
				% For each
				\pgfplotsinvokeforeach{0,...,\NumCols/2 -1}{
					% Define color
					\pgfmathparse{1000 / (\NumCols/2 -1) * #1}
					\extractcolormapcolor{tempcol#1}{\pgfmathresult}

					% Add plot
					\addplot+[tempcol#1]
						table[col sep=comma, x=t, y index=#1]
							{\expbycattable} \closedcycle;

					% Add legend entry (https://tex.stackexchange.com/a/405018)
					\pgfplotstablegetcolumnnamebyindex{#1}\of{\expbycattable}\to\pgfplotsretval
					\expandafter\addlegendentry\expandafter{\pgfplotsretval}
				}
			\end{axis}
		\end{tikzpicture}
	\end{subfigure}\\[1em]
	\begin{subfigure}[c]{\textwidth}
		\centering

		\begin{tikzpicture}
			\begin{axis}[
				stack plots=y,
				area style,
				date coordinates in=x,
				width=\textwidth,
				height=0.375\textwidth,
				xticklabel=\month.\shortyear{\year},
				xtick=data,
				enlargelimits=false,
				xticklabel style={
					rotate=60,
					anchor=near xticklabel,
				},
				legend columns=5,
				legend style={at={(0.5,-0.6)},anchor=south},
				ylabel={Expenses in €},
				ymin=0,
			]
				% For each
				\pgfplotsinvokeforeach{\NumCols/2,...,\NumCols-1}{
					% Define color
					\pgfmathparse{1000 * (#1 - \NumCols/2) / (\NumCols-1 - \NumCols/2)}
					\extractcolormapcolor{tempcol#1}{\pgfmathresult}

					% Add plot
					\addplot+[tempcol#1]
						table[col sep=comma, x=t, y index=#1]
							{\expbycattable} \closedcycle;

					% Add legend entry (https://tex.stackexchange.com/a/405018)
					\pgfplotstablegetcolumnnamebyindex{#1}\of{\expbycattable}\to\pgfplotsretval
					\expandafter\addlegendentry\expandafter{\pgfplotsretval}
				}
			\end{axis}
		\end{tikzpicture}
	\end{subfigure}

	\caption{Expenses by category}
\end{figure}

\end{document}

