\ProvidesPackage{sparkln}
\RequirePackage{tikz}

% A LaTeX implementation of Edward Tufte's Sparklines to
% compete with sparklines.sty and spark.sty.
%
% ©2010 Jorden Mauro, released under the LPPL
% 
% There are many flavors of the sparkln command to facilitate
% users who are as obsessive about design as I am.
%
% FILE FORMAT:
% Files whose paths are given for arguments labeled `Datafile' should
% be plain text and contain 2 numbers per line. The first number should
% be a monotonically increasing integer starting at 1, the second should
% be the corresponding observed value. Lines may be commented out using
% a percent sign. Ex:
%
% % This is a comment
% 1 32
% 2 15.7
% 3 12.9
%
% STATUS
% So far, some basic sparkline macros have been created. They are picky
% about maximum values, but anything up to 300 should be ok. If I
% eventually figure out how to get automagic scaling to work (or better
% yet, someone else does), that would be cool. As much as I don't like
% this problem, it seems easy enough to scale the data files with awk.
% The sparklines also do not display the gray `normal range' bar that
% Tufte describes in `Beautiful Evidence'.
%
% BUT WHY, JORDEN?
% Because the other packages incur tradeoffs of some kind that I
% don't want to trade off. Also, my package has the `binary'
% sparklines described in Tufte's book `Beautiful Evidence', and
% the competitors do not.
%

% These are the default max and min dot sizes and colors. Run this
% command yourself with different arguments if you don't like these.
\tikzstyle{maxmark} = [mark=*,mark options={color=red,scale=45}]
\tikzstyle{minmark} = [mark=*,mark options={color=blue,scale=45}]

% Make a sparkline the lazy way. This will calculate the max
% and min values. This could be slow if the Datafile is HUGE.
%
% Arguments:
% 1) Datafile
%\newcommand{\sparkln}[1]{%
%\begin{tikzpicture[xscale=0.01, yscale=0.01]
%\draw[ultra thin] plot[smooth,mark=*, maxmark, mark indices={#2}]
%	file {#1};
%\draw[ultra thin] plot[smooth,only marks, minmark, mark indices={#3}] 
%	file {#1};
%\end{tikzpicture}%
%}

% Make a sparkline by giving max and min values
%
% Arguments:
% 1) File
% 2) Index of min value
% 3) Index of max value
\newcommand{\sparklnMm}[3]{%
\begin{tikzpicture}[xscale=0.01, yscale=0.01]
\draw[ultra thin] plot[smooth,mark=*, maxmark, mark indices={#2}]
	file {#1};
\draw[ultra thin] plot[smooth,only marks, minmark, mark indices={#3}] 
	file {#1};
\end{tikzpicture}%
}

% Make a sparkline by giving the mean, max, and min values
%
% Arguments:
% 1) Datafile
% 2) Index of min value
% 3) Index of max value
% 4) Mean value
\newcommand{\sparklnMmA}[4]{%
\begin{tikzpicture}[xscale=0.01, yscale=0.01]
\draw[ultra thin, black!50] (1,#4)--(365,#4);
\draw[ultra thin] plot[smooth,mark=*, maxmark, mark indices={#2}]
	file {#1};
\draw[ultra thin] plot[smooth,only marks, minmark, mark indices={#3}] 
	file {#1};
\end{tikzpicture}%
}

%%%%%%%%%%%%%%%%%%%%
% Sparklines that are really just histograms
%%%%%%%%%%%%%%%%%%%%

% Make a histogram
%
% Arguments:
% 1) Datafile
\newcommand{\sparkbar}[1]{
\begin{tikzpicture}[xscale=0.01, yscale=0.01]
\begin{scope}[ycomb, yscale=0.6]
\draw[black, ultra thick] plot[] file {#1};
\end{scope}
\end{tikzpicture}
}

% Make a colored histogram
%
% Arguments:
% 1) Datafile
\newcommand{\sparkbarC}[2]{
\begin{tikzpicture}[xscale=0.01, yscale=0.01]
\begin{scope}[ycomb, yscale=0.6]
\draw[{#2}, thin] plot[] file {#1};
\end{scope}
\end{tikzpicture}
}

%%%%%%%%%%%%%%%%%%
% Fancy-schmantzy sparklines
%%%%%%%%%%%%%%%%%%

% Make a binary sparkline
%
% Arguments:
% 1) Datafile -- unlike all the rest, the values in the second column
%	should be either 1 or -1 to indicate `on' or `off', etc.
\newcommand{\sparkbin}[1]{
\begin{tikzpicture}[xscale=0.07, yscale=.20]
\begin{scope}[ycomb, yscale=0.6]
\draw[black, ultra thick] plot[] file {#1};
\end{scope}
\end{tikzpicture}
}
