| 1 |
\documentclass{article} |
| 2 |
|
| 3 |
\usepackage{sparklines} |
| 4 |
|
| 5 |
\renewcommand{\baselinestretch}{1.25} |
| 6 |
\let\env=\texttt |
| 7 |
\let\pkg=\textsf |
| 8 |
|
| 9 |
\definecolor{sparkrectanglecolor}{rgb}{0.8,0.95,0.8} |
| 10 |
\definecolor{sparkspikecolor}{rgb}{1,0,0} |
| 11 |
\setlength{\sparkdotwidth}{1.3pt} |
| 12 |
\setlength{\sparklinethickness}{.3pt} |
| 13 |
|
| 14 |
\begin{document} |
| 15 |
|
| 16 |
\title{Sparklines} |
| 17 |
\author{Andreas Loeffler \and Dan Luecking} |
| 18 |
\date{Updated 27~December 2016 (version 1.7)}\maketitle |
| 19 |
|
| 20 |
\section{What are sparklines?} |
| 21 |
|
| 22 |
Sparklines are intense, simple, wordlike graphics, so named by Edward |
| 23 |
Tufte. This is an example of sparkline: |
| 24 |
\begin{sparkline}{10} |
| 25 |
% This environment has now an own coordinate system. x and y run from 0 to 1. |
| 26 |
% |
| 27 |
% This draws a sparkling-rectangle with lower level y=0.3 and upper |
| 28 |
% level y=0.8 |
| 29 |
\sparkrectangle 0.3 0.8 |
| 30 |
% |
| 31 |
% This draws a blue colored sparkling-dot at x=0.5, y=0.62 |
| 32 |
\sparkdot 0.5 0.62 blue |
| 33 |
% |
| 34 |
% This draws a red colored sparkling-dot at x=1, y=0.2 |
| 35 |
\sparkdot 1 0.2 red |
| 36 |
% |
| 37 |
% This draws a sparkline with ten values starting at x=0.1 and |
| 38 |
% y=0.95, then x=0.2 and y=0.8 etc. Drawing finishes if / appears |
| 39 |
\spark 0.1 0.95 0.2 0.8 0.3 0.3 0.4 0.52 0.5 0.62 |
| 40 |
0.6 0.7 0.7 0.5 0.8 0.4 0.9 0.25 1 0.2 / |
| 41 |
\end{sparkline} |
| 42 |
(the stock price of Daimler Chrysler, for example). |
| 43 |
% |
| 44 |
In lieu of a more detailed introduction, Professor Tufte's site |
| 45 |
has an early release of a chapter on sparklines, see |
| 46 |
www.edwardtufte.com. A PHP implementation can be found at |
| 47 |
http://sparkline.sourceforge.net/. |
| 48 |
|
| 49 |
A sparkline can be added using the \env{sparkline}--environment. |
| 50 |
Also, you can add sparkling rectangles for the median and special |
| 51 |
sparkling dots in red or blue. If we want to add a sparkline, be careful not to leave an empty line |
| 52 |
between the text left of the sparkline and the environment itself, |
| 53 |
since otherwise the sparkline starts a new paragraph. |
| 54 |
|
| 55 |
Sparklines do not appear within a |
| 56 |
dvi-file, they require either pdflatex or conversion to postscript with |
| 57 |
dvips. The \textsf{sparklines} package requires the \pkg{pgf} package. |
| 58 |
This makes it incompatible with \pkg{pictex}: the combination both |
| 59 |
require too many dimension parameters. It is possible that the package \pkg{sparklines} |
| 60 |
can be used with \pkg{pictexwd}; they can at least be loaded together |
| 61 |
without using too many dimensions, but no other test was conducted. |
| 62 |
|
| 63 |
|
| 64 |
\section{Usage} |
| 65 |
\paragraph{Sparkline environment} |
| 66 |
The sparkline at the beginning of the previous section was created with the |
| 67 |
following: |
| 68 |
\begin{verbatim} |
| 69 |
\begin{sparkline}{10} |
| 70 |
\sparkrectangle 0.3 0.8 |
| 71 |
\sparkdot 0.5 0.62 blue |
| 72 |
\sparkdot 1 0.2 red |
| 73 |
\spark 0.1 0.95 0.2 0.8 0.3 0.3 0.4 0.52 0.5 0.62 |
| 74 |
0.6 0.7 0.7 0.5 0.8 0.4 0.9 0.25 1 0.2 / |
| 75 |
\end{sparkline} |
| 76 |
\end{verbatim} |
| 77 |
|
| 78 |
The argument of the environment gives the width of the graphic as a |
| 79 |
multiple of the dimension unit \texttt{ex} (approximately the height of |
| 80 |
the lowercase `x' in the current font). Within the environment, |
| 81 |
$x$-coordinates run from 0 (extreme left of the graphic) to 1 (extreme |
| 82 |
right). |
| 83 |
|
| 84 |
The height of the graphic is given by the \emph{macro} |
| 85 |
\verb$\sparklineheight$, defined to produce \texttt{1.75} by default. |
| 86 |
The units are the same as for the width: the \texttt{ex} of the current |
| 87 |
font. You can redefine that macro (with \verb$\renewcommand$) to force |
| 88 |
another height. Within the graphic, the $y$-coordinate runs from 0 |
| 89 |
(lowermost point) to 1 (uppermost). |
| 90 |
|
| 91 |
Each pair of numbers after the macro \verb$\spark$ represents a |
| 92 |
coordinate pair, giving the location of a point in the above described |
| 93 |
coordinate system. The macro draws a line from each point to the next. |
| 94 |
Each number must be followed by a space, and the list is terminated by a |
| 95 |
\texttt{/}. Be careful that there are an even number of coordinates. The |
| 96 |
thickness of the line that is created is the value of the length |
| 97 |
\verb$\sparklinethickness$, which the user may change (with |
| 98 |
\verb$\setlength$). The default value is \texttt{0.2pt}; the above |
| 99 |
example was created with the value \texttt{0.3pt}. |
| 100 |
|
| 101 |
\paragraph{Sparkrectangle} |
| 102 |
The \verb$\sparkrectangle$ command produces a background rectangle. It |
| 103 |
must be followed by two numbers, each followed by a space or the end of |
| 104 |
the line. They are the $y$-coordinates of the bottom and top of the |
| 105 |
rectangle. This is supposed to show the `normal range' of the $y$-data, |
| 106 |
so that a point outside that rectangle represents a departure from |
| 107 |
normal. The color of the rectangle is `\texttt{sparkrectanglecolor}', |
| 108 |
which the user may redefine (with |
| 109 |
\verb$\definecolor$). The initial definition is given by \\ |
| 110 |
\indent\verb$\definecolor{sparkrectanglecolor}{gray}{0.9}$\\ |
| 111 |
In the above example, it was first changed to a light green. |
| 112 |
|
| 113 |
\paragraph{Sparkdots} |
| 114 |
The colored dots are produced by \verb$\sparkdot$, the diameter of the |
| 115 |
dot is the value of the length \verb$\sparkdotwidth$, which the user may |
| 116 |
change (with \verb$\setlength$). The default value is \texttt{1.2pt}; |
| 117 |
the above example was created a value of \texttt{1.3pt}. The command |
| 118 |
takes three parameters, each of which must be followed by a space or the |
| 119 |
end of the line. The first two are the coordinates of the center of the |
| 120 |
dot, the third is the color. |
| 121 |
|
| 122 |
\paragraph{Bar graphs} |
| 123 |
Bar graphs can be drawn easily: |
| 124 |
\begin{sparkline}{5} |
| 125 |
\sparkspike .083 .18 |
| 126 |
\sparkspike .25 .55 |
| 127 |
\sparkspike .417 1 |
| 128 |
\sparkspike .583 .62 |
| 129 |
\sparkspike .75 .42 |
| 130 |
\sparkspike .917 .5 |
| 131 |
\end{sparkline}. |
| 132 |
This was created by the code: |
| 133 |
\begin{verbatim} |
| 134 |
\begin{sparkline}{5} |
| 135 |
\sparkspike .083 .18 |
| 136 |
\sparkspike .25 .55 |
| 137 |
\sparkspike .417 1 |
| 138 |
\sparkspike .583 .62 |
| 139 |
\sparkspike .75 .42 |
| 140 |
\sparkspike .917 .5 |
| 141 |
\end{sparkline}. |
| 142 |
\end{verbatim} |
| 143 |
The macro \verb$\sparkspike$ must be followed by a pair of numbers, each |
| 144 |
followed by a space or the end of the line. The first of the pair is the |
| 145 |
horizontal location of the bar and the second is the height. The bars |
| 146 |
are drawn in color `\texttt{sparkspikecolor}' which the user may |
| 147 |
redefine (with \verb$\definecolor$). The default is \texttt{black}; the |
| 148 |
above example was drawn with it changed to \texttt{red}. The width of |
| 149 |
each bar is the value of the length \verb$\sparkspikewidth$, which the |
| 150 |
user may change (with \verb$\setlength$). The default is \texttt{2pt}. |
| 151 |
|
| 152 |
You can combine bars and lines: |
| 153 |
\begin{sparkline}{5} |
| 154 |
\sparkspike .083 .18 |
| 155 |
\sparkspike .25 .55 |
| 156 |
\sparkspike .417 1 |
| 157 |
\sparkspike .583 .62 |
| 158 |
\sparkspike .75 .42 |
| 159 |
\sparkspike .917 .5 |
| 160 |
\spark 0.1 0.95 0.2 0.8 0.3 0.3 0.4 0.52 0.5 0.62 |
| 161 |
0.6 0.7 0.7 0.5 0.8 0.4 0.9 0.25 1 0.2 / |
| 162 |
\sparkdot 1 0.2 blue |
| 163 |
\end{sparkline}\space. This was created with |
| 164 |
\begin{verbatim} |
| 165 |
\begin{sparkline}{5} |
| 166 |
\sparkspike .083 .18 |
| 167 |
\sparkspike .25 .55 |
| 168 |
\sparkspike .417 1 |
| 169 |
\sparkspike .583 .62 |
| 170 |
\sparkspike .75 .42 |
| 171 |
\sparkspike .917 .5 |
| 172 |
\spark 0.1 0.95 0.2 0.8 0.3 0.3 0.4 0.52 0.5 0.62 |
| 173 |
0.6 0.7 0.7 0.5 0.8 0.4 0.9 0.25 1 0.2 / |
| 174 |
\sparkdot 1 0.2 blue |
| 175 |
\end{sparkline}\space. |
| 176 |
\end{verbatim} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
\paragraph{Colors} In case you want to change colors use |
| 181 |
\begin{verbatim} |
| 182 |
\definecolor{sparkrectanglecolor}{gray}{0.9} |
| 183 |
\definecolor{sparkspikecolor}{named}{red} |
| 184 |
\definecolor{sparklinecolor}{named}{red} |
| 185 |
\end{verbatim} |
| 186 |
before the sparkline environment (see a manual about defining colors in \LaTeX{} if you do not understand the definition of \emph{named} etc.). |
| 187 |
|
| 188 |
\paragraph{Bottom line} This adds a bottom line (the x-axis) which can be useful to visually separate different bar charts that are next to each other: |
| 189 |
\begin{sparkline}{5} |
| 190 |
\definecolor{sparkbottomlinecolor}{gray}{0.9} |
| 191 |
\sparkspike .15 .55 |
| 192 |
\sparkspike .317 1 |
| 193 |
\sparkspike .483 .62 |
| 194 |
\sparkspike .65 .42 |
| 195 |
\sparkspike .817 .5 |
| 196 |
\sparkbottomline[0.9] |
| 197 |
\end{sparkline}. |
| 198 |
The code used was |
| 199 |
\begin{verbatim} |
| 200 |
\begin{sparkline}{5} |
| 201 |
\definecolor{sparkbottomlinecolor}{gray}{0.9} |
| 202 |
\sparkspike .15 .55 |
| 203 |
\sparkspike .317 1 |
| 204 |
\sparkspike .483 .62 |
| 205 |
\sparkspike .65 .42 |
| 206 |
\sparkspike .817 .5 |
| 207 |
\sparkbottomline[0.9] |
| 208 |
\end{sparkline}. |
| 209 |
\end{verbatim} |
| 210 |
Changing the color of the bottom line is quite easy using the command |
| 211 |
like |
| 212 |
\begin{verbatim} |
| 213 |
\definecolor{sparkbottomlinecolor}{named}{red} |
| 214 |
\end{verbatim}. You can change the thickness of the bottom line using |
| 215 |
command like |
| 216 |
\begin{verbatim} |
| 217 |
\setlength\sparkbottomlinethickness{1pt} |
| 218 |
\end{verbatim} |
| 219 |
|
| 220 |
The optional argument of \verb|\sparkbottomline| is the length of the |
| 221 |
bottom line (by default~1). |
| 222 |
|
| 223 |
If you need to set both start and end of the bottomline, use |
| 224 |
\verb|\sparkbottomlinex| command, which has two obligatory arguments: |
| 225 |
the start and the end, for example, |
| 226 |
\definecolor{sparkbottomlinecolor}{named}{blue}% |
| 227 |
\begin{sparkline}{5} |
| 228 |
\sparkspike .15 .55 |
| 229 |
\sparkspike .317 1 |
| 230 |
\sparkspike .483 .62 |
| 231 |
\sparkspike .65 .42 |
| 232 |
\sparkspike .817 .5 |
| 233 |
\sparkbottomlinex 0.3 0.8 |
| 234 |
\end{sparkline}. This was created with the code |
| 235 |
\begin{verbatim} |
| 236 |
\begin{sparkline}{5} |
| 237 |
\sparkspike .15 .55 |
| 238 |
\sparkspike .317 1 |
| 239 |
\sparkspike .483 .62 |
| 240 |
\sparkspike .65 .42 |
| 241 |
\sparkspike .817 .5 |
| 242 |
\sparkbottomlinex 0.3 0.8 |
| 243 |
\end{sparkline} |
| 244 |
\end{verbatim} |
| 245 |
|
| 246 |
\paragraph{Clipping} |
| 247 |
|
| 248 |
Sometimes you may have sparklines outside the drawing region, for |
| 249 |
example, |
| 250 |
\begin{sparkline}{10} |
| 251 |
\sparkrectangle 0.3 0.8 |
| 252 |
\sparkdot 0.5 0.62 blue |
| 253 |
\sparkdot 1 0.2 red |
| 254 |
\spark 0.1 0.95 0.2 0.8 0.3 0.3 0.4 2.52 0.5 0.62 |
| 255 |
0.6 0.7 0.7 0.5 0.8 0.4 0.9 0.25 1 0.2 / |
| 256 |
\end{sparkline}. You may want to clip them. The starred version |
| 257 |
\verb|sparkline*| creates \emph{clipped} sparklines: the graphics |
| 258 |
outside the rectangle (0,0) -- (1,1) plus a thin border of the width |
| 259 |
\verb|\sparklineclipsep| is clipped: |
| 260 |
\begin{sparkline*}{10} |
| 261 |
\sparkrectangle 0.3 0.8 |
| 262 |
\sparkdot 0.5 0.62 blue |
| 263 |
\sparkdot 1 0.2 red |
| 264 |
\spark 0.1 0.95 0.2 0.8 0.3 0.3 0.4 2.52 0.5 0.62 |
| 265 |
0.6 0.7 0.7 0.5 0.8 0.4 0.9 0.25 1 0.2 / |
| 266 |
\end{sparkline*}. The first sparkline was created with |
| 267 |
\begin{verbatim} |
| 268 |
\begin{sparkline}{10} |
| 269 |
\sparkrectangle 0.3 0.8 |
| 270 |
\sparkdot 0.5 0.62 blue |
| 271 |
\sparkdot 1 0.2 red |
| 272 |
\spark 0.1 0.95 0.2 0.8 0.3 0.3 0.4 2.52 0.5 0.62 |
| 273 |
0.6 0.7 0.7 0.5 0.8 0.4 0.9 0.25 1 0.2 / |
| 274 |
\end{sparkline}. |
| 275 |
\end{verbatim} |
| 276 |
The second sparkline was created with |
| 277 |
\begin{verbatim} |
| 278 |
\begin{sparkline*}{10} |
| 279 |
\sparkrectangle 0.3 0.8 |
| 280 |
\sparkdot 0.5 0.62 blue |
| 281 |
\sparkdot 1 0.2 red |
| 282 |
\spark 0.1 0.95 0.2 0.8 0.3 0.3 0.4 2.52 0.5 0.62 |
| 283 |
0.6 0.7 0.7 0.5 0.8 0.4 0.9 0.25 1 0.2 / |
| 284 |
\end{sparkline*}. |
| 285 |
\end{verbatim} |
| 286 |
|
| 287 |
The clipping separation is set to 2~pt. You can change it with |
| 288 |
\begin{verbatim} |
| 289 |
\setlength\sparklineclipsep{...}. |
| 290 |
\end{verbatim} |
| 291 |
|
| 292 |
|
| 293 |
\section*{Version history} |
| 294 |
\begin{description} |
| 295 |
|
| 296 |
\item[] Dev 26, 2016 |
| 297 |
|
| 298 |
version 1.7: Boris Veytsman ([email protected]). Added clipping, |
| 299 |
extensible bottom lines, some bug fixes. |
| 300 |
|
| 301 |
\item[] Oct 19, 2014 |
| 302 |
|
| 303 |
version 1.6: Emiel van Miltenburg ([email protected]) - Adding a bottom line (the x-axis, this is useful to visually separate different bar charts that are next to each other) and changing the color of the bottom line. |
| 304 |
|
| 305 |
\item[] Nov 21, 2009 |
| 306 |
|
| 307 |
version 1.5: Benno Puetz ([email protected]) made change of colors possible. |
| 308 |
|
| 309 |
\item[] Apr 20, 2009 |
| 310 |
|
| 311 |
version 1.4: Alexander Kowalski ([email protected]) found an error concerning spark-rectangles |
| 312 |
|
| 313 |
\item[] Mar 21, 2007 |
| 314 |
|
| 315 |
version 1.3: User adjustable colors and parameters added by Dan |
| 316 |
Luecking $\[email protected]$\rangle$ |
| 317 |
|
| 318 |
\item[] Mar 19, 2007 |
| 319 |
|
| 320 |
version 1.2: Sparkbars added thanks to Harlan Harris |
| 321 |
$\[email protected]$\rangle$ |
| 322 |
|
| 323 |
\item[] Apr 21, 2005 |
| 324 |
|
| 325 |
version 1.1: bug removed thanks to Mathias Hofmann |
| 326 |
$\[email protected]$\rangle$ |
| 327 |
|
| 328 |
\item[] Dec 12, 2004 |
| 329 |
|
| 330 |
version 1.0: first version of sparklines |
| 331 |
\end{description} |
| 332 |
|
| 333 |
\end{document} |