%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -*- Mode: Latex -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% aahistogram.sty -- a simple histogram package
%% 
%% Author          : Anders Andersen
%% Created On      : Wed Nov 24 10:03:30 1999
%% Last Modified By: Anders Andersen
%% Last Modified On: Wed Nov 24 21:28:24 1999
%% Status          : Unknown, Use with caution!
%% 
%% Unless other notices are present in any part of this file explicitly
%% claiming copyrights for other people and/or organisations, the contents
%% of this file is fully copyright (C) 1999 Anders Andersen, all rights
%% reserved.
%% 
%% Permission is granted to make and distribute verbatim copies of this
%% document provided that the copyright notice and this permission notice
%% are preserved on all copies.
%% 
%% Permission is granted to copy and distribute modified versions of this
%% document under the conditions for verbatim copying, provided that the
%% entire resulting derived work is distributed under the terms of a
%% permission notice identical to this one.
%% 
%% If any other present copyright notices interfere with the copyright
%% claims above, these claims may be partially overruled by those notices.
%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% --------------------------------------%--------------------------------------
% ---------------------------- Identification part ----------------------------
% --------------------------------------%--------------------------------------

\RequirePackage{rcs}

\RCS$Date: 1999/11/24 20:29:14 $
\RCS$RCSfile: aahistogram.sty,v $
\RCS$Revision: 1.3 $
\RCS$State: Exp $
\RCS$Author: aa $

\input{aausercs}


% --------------------------------------%--------------------------------------
% ----------------------------- Example of usage ------------------------------
% --------------------------------------%--------------------------------------

% \usepackage{aahistogram}
% ...
% \begin{aahistogram}[Kirkenes]{\linewidth}{1000}{100}{2}
%   \aanumlabels{0}{300}
%   \aahbar{Kirkenes}{534}                         % First bar
%   \aahbar[red]{Kiby}{789}                        % Second bar
% \end{aahistogram}


% --------------------------------------%--------------------------------------
% ------------------------------ Package loading ------------------------------
% --------------------------------------%--------------------------------------

\RequirePackage{color}
\RequirePackage{calc}
\RequirePackage{ifthen}


% --------------------------------------%--------------------------------------
% --------------------------------- Main code ---------------------------------
% --------------------------------------%--------------------------------------

% The width used for each unit
\newlength{\uw}

% The distance (height) between each bar (in \unitlength)
\newcounter{bhu}\setcounter{bhu}{15}

% The width of the histogram
\newcounter{hwidthu}

% The height of the histogram
\newcounter{hheightu}

% The total width (including borders and description of task)
\newcounter{htwidthu}

% The total height (including borders and description of unit)
\newcounter{htheightu}
\newcounter{vxheightu}

% The bar number (stepped for each new bar in the diagram)
\newcounter{hbar}

% The start position (y) of this task
\newcounter{hypos}

% Space used for description of tasks
\newcounter{htextwu}
\newlength{\htextw}

% Space used for description of units and for names
\newcounter{utextu}\setcounter{utextu}{15}

% Length of time axis
\newcounter{hveclenu}

% Number of marks
\newcounter{mnum}

% Label value
\newcounter{lnum}

% Maximum value
\newcounter{maxval}

% Width per value-unit
\newlength{\bwu}

% Width per marks in \lengthunit
\newcounter{bwm}

% The start x position of description text
\newcounter{hdescxpos}

% The start y position of description text
\newcounter{hdescypos}\setcounter{hdescypos}{\value{bhu}/2}


\newcommand{\aahbar}[3][black]{%
  % #1: Color of bar
  % #2: Name of bar
  % #3: Value of bar
  \stepcounter{hbar}
  \setcounter{hypos}{-\value{hbar}*\value{bhu}}
  \put(0,\value{hypos}){%
    \textcolor{#1}{\rule{#3\bwu}{2\unitlength}}}
  \put(-\value{htextwu},\value{hypos}){%
    \makebox(\value{htextwu},0)[l]{#2\phantom{jl}}}}


\newcommand{\aanumlabels}[2]{%
  % #1: First label value
  % #2: Step between labels
  \setcounter{lnum}{#1}
  \setcounter{hdescxpos}{\value{lnum}*\bwu/\unitlength}
  \whiledo{\value{lnum}<\value{maxval}}{%
    \put(\value{hdescxpos},\value{hdescypos}){\makebox(0,0){\thelnum}}
    \addtocounter{lnum}{#2}
    \setcounter{hdescxpos}{\value{lnum}*\bwu/\unitlength}}}


\newenvironment{aahistogram}[5][]{%
  % #1: Longest name of bar
  % #2: Total width (in length)
  % #3: Max value
  % #4: Value between each mark
  % #5: Number of bars
  \setcounter{hbar}{0}%                         % A bar counter
  \settowidth{\htextw}{#1\phantom{jl}}%         % Width of text
  \setcounter{htextwu}{\htextw/\unitlength}%    % Width of text in units
  \setcounter{hheightu}{#5*\value{bhu}}%        % Height of all bars in units
  \setcounter{htwidthu}{#2/\unitlength}%        % Total width in units
  \setcounter{htheightu}{\value{hheightu}+\value{utextu}+5}%
                                                % Total height in units
  \setcounter{vxheightu}{\value{htheightu}-\value{utextu}}%
                                                % Horizontal position of origo
  \setcounter{hveclenu}{\value{htwidthu}-\value{htextwu}}%
                                                % Length of unit-vector
  \setcounter{hwidthu}{\value{hveclenu}-5}%     % Max length of bar
  \setcounter{maxval}{#3}%                      % Maximum value
  \setcounter{mnum}{#3/#4+1}%                   % Number of marks
  \setcounter{bwm}{#4*\value{hwidthu}/#3}%      % Length between marks in units
  \setlength{\bwu}{\value{bwm}\unitlength/#4}%      % Length between marks
  \begin{picture}(\value{htwidthu},\value{htheightu})%
    (-\value{htextwu},-\value{vxheightu})
    \put(0,0){\vector(1,0){\value{hveclenu}}}
    \multiput(0,-1)(\value{bwm},0){\value{mnum}}{\line(0,1){2}}%
  }{\end{picture}}
