String templates
Sometimes you may need to construct a simple tooltip
or label
for each data point using attributes in the data. The simplest method would be to create a column in your original data file that contains the text for each tooltip. But you may not have the luxury of editing the original data. In those cases you can use simple text templates to construct a "virtual column" and refer to this column for the tooltip.
We've allowed pattern-based-strings to be defined which will take values from the data using a double curly braces syntax e.g. {{ key }}
. For instance, say the data rows for a visualisation contain the attributes/columns ConstituencyName
and population
. We can build a very simple tooltip string as {{ ConstituencyName }}: {{ population }}
. In this case {{ ConstituencyName }}
will be replaced by that row's ConstituencyName
value and {{ population }}
by the population
value.
Filter functions§
You can also use the following filters:
colourScale(scale,min,max)§
This function will convert a value into a colour using a named scale
and the range min
to max
. This is useful to, for example, get a colour to use as a background style.
{{ value | colourScale("Viridis",0,100) }}
will convert a value into the Viridis colour scale
contrastColour§
Get a contrasting colour (e.g. black or white) for a given input colour.
decimalYear() Since v0.15.10§
Calculate the decimal year value of a date. Example:
2024-07-01
would be converted to2024.49727
because 1st July is 0.49727 of the way through the year 2024.
multiply(n)§
Use this to scale a value by n
.
strftime("%Y-%m-%d") Since v0.11.0§
Convert an ISO8601 date or Unix timestamp into a specific date format. Examples:
- Convert a date to an ISO8601 string
2024-07-04
usingstrftime("%Y-%m-%d")
- Convert a date to a British date string
04/07/2024
usingstrftime("%d/%m/%Y")
- Convert a date to a string of the form
4 July 2024
usingstrftime("%e %B %Y")
strptime("%Y-%m-%d") Since v0.15.10§
Parse a date into a numeric representation using a specific date format. This example would parse an ISO8601 date. If no day is given in the input string it will default to the 1st of the month. Hours, minutes and seconds default to 0
. Examples:
- Convert a date of the form
Jul 2024
, into a numeric date representation using{{ Jul 2024 | strptime("%b %Y") }}
- Convert a date of the form
15/06/2024
, into a numeric date representation using{{ 15/06/2024 | strptime("%d/%m/%Y") }}
toFixed(n)§
To limit a value to n
decimal places.
toLocaleString()§
To use the locale's (of the server) preferred formatting for a value. Examples:
- To convert
{{ 1500 }}
to a locale string of the form1,500
using{{ 1500 | toLocaleString("en-GB") }}