% this is my first package
% 
% (c) Frederik Vanggaard
%
% Inspired by bchart version 0.1.2 by Tobias Kuhn
%
%% This program can be redistributed and/or modified under the terms
%% of the LaTeX Project Public License Distributed from CTAN archives
%% in directory macros/latex/base/lppl.txt.
% 
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{barchart}
  [2014/05/19 v0.01 LaTeX package for bar charts]

\usepackage{tikz}
\usepackage{ifthen}
\usetikzlibrary{calc}

\newlength{\barcpos}
\newcommand{\tempWidth}{}
% barchart args:
% #1: max
% #2: min
% #3: steps
% #4: step
% #5: width
% #7: height
% #8: scale
% #9: axis label
\newcommand{\barcmin}{}
\newcommand{\barcmax}{}
\newcommand{\barcsteps}{}
\newcommand{\barcstep}{}
\newlength{\barctotalWidth}
\newcommand{\ypos}{}
\newcommand{\scale}{}
\newcommand{\barcy}{}

\makeatletter
\define@key{barchart}{min}{\renewcommand{\barcmin}{#1}}
\define@key{barchart}{max}{\renewcommand{\barcmax}{#1}}
\define@key{barchart}{steps}{\renewcommand{\barcsteps}{#1}}
\define@key{barchart}{step}{\renewcommand{\barcstep}{#1}}
\define@key{barchart}{width}{\setlength{\barctotalWidth}{#1}}
\define@key{barchart}{height}{\renewcommand{\ypos}{#1}}
\define@key{barchart}{scale}{\renewcommand{\scale}{#1}}
\define@key{barchart}{label}{\renewcommand{\barcy}{#1}}

\makeatother

%barc args:
% #1: color
% #2: width
% #3: plain (true/false)
% #4: label
% #5: rotation (true/false)
\newcommand{\barccolor}{}
\newcommand{\barcwidth}{}
\newcommand{\barcplain}{}
\newcommand{\barclabel}{}
\newcommand{\barcrotation}{}

\makeatletter
\define@key{barc}{color}{\renewcommand{\barccolor}{#1}}
\define@key{barc}{width}{\renewcommand{\barcwidth}{#1}}
\define@key{barc}{plain}[false]{\renewcommand{\barcplain}{true}}
\define@key{barc}{label}{\renewcommand{\barclabel}{#1}}
\define@key{barc}{rotation}[false]{\renewcommand{\barcrotation}{true}}

\makeatother

%skip args:
% #1: size
\newcommand{\barcskiplabel}{}

\makeatletter
\define@key{barcskip}{label}{\renewcommand{\barcskiplabel}{#1}}
\makeatother

\newenvironment{barchart}[1][]{%
	\newcommand{\barc}[2][] {%

		\renewcommand{\barccolor}{blue!20}
		\renewcommand{\barcwidth}{30pt}
		\renewcommand{\barcplain}{false}
		\renewcommand{\barclabel}{}
		\renewcommand{\barcrotation}{false}

		\addtolength{\barcpos}{5mm}
		\setkeys{barc}{##1}
		%\fill[color=\barccolor,fill,draw](\barcpos,0) rectangle (\barcwidth+\barcpos,##2);
		\fill[color=\barccolor, fill,draw](\barcpos,0) rectangle ($##2-\barcmin*(0,\barcrange/\barcrange) + (\barcwidth+\barcpos-5mm,0)$);
		\draw (\barcpos,0) rectangle ($##2-\barcmin*(0,\barcrange/\barcrange) + (\barcwidth+\barcpos-5mm,0)$);

		%\node[draw, shape=circle,fill,\barccolor] at (\barcpos+\barcwidth-5mm,0){};
		%\node[draw, shape=circle,fill,\barccolor] at ($##2-\barcmin*(0,\barcrange/\barcrange) + (\barcwidth+\barcpos-5mm,0)$){##2};

		\ifthenelse{\equal{\barcplain}{true}}{}{
	    	% Write value:
			%\node[anchor=east] at ($(\barcpos+(\barcwidth/3,0) + (0,##2+0.5)$){##2};
	    	% $##2-\bcmin*(\bcwidth/\bcrange,0) + (0,\bcpos-2.5mm)$
	    	\node[] at ($($##2-\barcmin*(0,\barcrange/\barcrange) + (\barcwidth+\barcpos-5mm,0)$)!0.5!(\barcpos,##2+0.25+\barcmax/10) $){##2};
	    }
	    \ifthenelse{\equal{\barcrotation}{true}}{false}{
	    	\node[label={[label distance=0.5cm,text depth=-1ex,rotate=-90]right:\barclabel}] at ($($\barcmin*(0,\barcrange/\barcrange) + (\barcwidth+\barcpos-5mm,0)$)!0.5!(\barcpos,1.5) $) {};
	    }
	    \ifthenelse{\equal{\barcrotation}{false}}{true}{
	    	\node[label={[label distance=0.5cm,text depth=-1ex,rotate=-45]right:\barclabel}] at ($($\barcmin*(0,\barcrange/\barcrange) + (\barcwidth+\barcpos-10mm,0)$)!0.5!(\barcpos,1) $) {};
	    }

		\addtolength{\barcpos}{\dimexpr(\barcwidth-20pt) \relax}
		
	}

	\newcommand{\barcskip}[2][]{
		\renewcommand{\barcskiplabel}{}
		\setkeys{barcskip}{##1}
		\addtolength{\barcpos}{##2}
	}

	\renewcommand{\barcmin}{0}
  	\renewcommand{\barcmax}{10}
  	\renewcommand{\barcstep}{1}
  	\renewcommand{\ypos}{1em}
  	\renewcommand{\scale}{1}

	\renewcommand{\smallskip}[1][]{\barcskip[##1]{2.5mm}}
	\renewcommand{\medskip}[1][]{\barcskip[##1]{5.0mm}}
	\renewcommand{\bigskip}[1][]{\barcskip[##1]{7.5mm}}

	\newcommand{\barcrange}{\bcstripunit{\dimexpr\barcmax pt-\barcmin pt\relax}}
	\renewcommand{\barcstep}{\barcrange}
	\renewcommand{\barcsteps}{0,\barcstep,...,\barcrange}%

	
	\setkeys{barchart}{#1}
	\begin{tikzpicture}[y=\ypos, scale = \scale]
} {
	

	\draw (0,0) -- (\barcpos+\barcwidth,0);
	\draw (0,0) -- ($(0,(\barcmax-\barcmin)$);
	\draw (0,0) -- (0-1mm,0);
    \node[anchor=east] (n) at (0-1mm,0) {\bcstripunit{\dimexpr\barcmin pt\relax}};
    \coordinate (labelpos) at (n.east);


	\foreach \x in \barcsteps {
		\ifthenelse{\equal{\x}{0}}{}{
			\draw ($\x*(0,\barcmax/\barcrange) + (0,0)$) -- ($\x*(0,\barcmax/\barcrange) + (0-1mm,0)$);
			\node[anchor=east] at (-1mm,\x){\bcstripunit{\dimexpr\barcmin pt+\x pt\relax}};
		}
	}


	\ifthenelse{\equal{\barcy}{}}{}{
		%\node[label={[label distance=0.5cm,align=center,text depth=-1ex,rotate=-90]left:\barcy}] at ($(-0.75,\barcmax) !0.5! (0,\barcmin)$){};
		\node[rotate=-90] at ($(-0.75-\barcmax/100,\barcmax) !0.5! (0,\barcmin)$){\barcy};
	}
	\end{tikzpicture}%


}

\makeatletter
\newcommand*{\bcstripunit}[1]{\strip@pt#1}
\makeatother


%%
%% End of file `barchart.sty'.