{sparkline}
Draws a word-sized line chart as inline SVG - no axes, the last point marked - to stand in a sentence or a table cell.
Syntax
{sparkline data='name', label='field', value='field', title='text', width=120, height=32}
{sparkline sequence='fibonacci', rows=12}
{sparkline type='json'} ... {/sparkline}
Options
-
data -
The rows: a
{data}store, a sequence store, an array of the page (or a field of the current row holding a list) or a_datafile of that name, or a literal (data='[3,1,4]'). Left out on a pair, the content is the data. -
type -
For a pair: the format of the content -
json,yaml,xmlorcsv- when it is not to be recognised on sight. -
sequence -
Plot the first
rowsterms (default 10) of a sequence type. -
value - The field with the number - else the first numeric field.
-
label - The field naming each point, used in the description read by a screen reader.
-
title -
The accessible name, the SVG's one
<title>. -
width - The width in pixels, default 120.
-
height - The height in pixels, default 32.
Description
{sparkline} is {chart 'sparkline'} by a name of its own: a small line with no axes, no grid and no labels, the last point marked with a dot. Its job is the shape of a series - up, down, flat or jumpy - in the space of a word, so it fits in running text or in a cell of a table.
The data is found as for a chart: data= names a {data} store, a sequence store, an array of the page or a _data file - and inside a loop a field of the current row that holds a list, so each row of a table can draw its own line. sequence= plots the first rows terms of a sequence type, and as a pair the content is the data (JSON, YAML, XML or CSV). A plain list of numbers is enough; rows with fields take value=, else their first numeric field.
The size is 120 x 32 by default. The line is scaled to its own minimum and maximum, not to zero, so even a small change shows; equal values draw a flat line through the middle. Every finite number is taken - a sparkline has no axis to keep in range. Unlike the bigger kinds it has only the chart's one <title>, no tooltip per point; the colour is --pad-chart-series on .pad-chart.
Examples
In a sentence
A literal list of numbers as data, drawn the height of the text around it.
<p>Sign-ups this fortnight {sparkline data='[12, 15, 14, 18, 22, 19, 24, 27, 25, 31, 29, 34, 38, 41]', height=20, title='Sign-ups per day'} - up again.</p>
Sign-ups this fortnight - up again.
A line per table row
Inside a loop, data= names a field of the row that holds a list.
<table>
<tr><th>Share</th><th>Ten days</th><th>Last</th></tr>
{shares}
<tr><td>{$name}</td><td>{sparkline data='closes', title=$name}</td><td>{$last}</td></tr>
{/shares}
</table>
<?php
$shares = [
[ 'name' => 'Acme', 'closes' => [ 64, 65, 64, 66, 67, 66, 68, 67, 69, 70 ], 'last' => 70 ],
[ 'name' => 'Globex', 'closes' => [ 21.4, 21.1, 20.8, 21.0, 20.6, 20.2, 20.5, 19.9, 19.6, 19.8 ], 'last' => 19.8 ],
[ 'name' => 'Initech', 'closes' => [ 88, 87, 89, 86, 88, 90, 87, 85, 88, 89 ], 'last' => 89 ] ];
?>
| Share | Ten days | Last |
|---|---|---|
| Acme | 70 | |
| Globex | 19.8 | |
| Initech | 89 |
A sequence as data
sequence= plots the first rows terms of a sequence type.
<p>Fibonacci {sparkline sequence='fibonacci', rows=12} and the primes {sparkline sequence='prime', rows=12}</p>
Fibonacci and the primes
See also
- {chart} Draws a chart as inline SVG on the server - 46 kinds, from bars and lines to sankeys, maps of a hierarchy and org charts.
- {sequence} Generates a range, a list or one of 80 mathematical sequences, and iterates its values.
- {data} Turns its content - JSON, XML, YAML, CSV, a list or a range - into a named data set for a later tag to iterate.