See the Value Formatters for more information on getting
started with valueFormatter
.
d3-format
Dash AG Grid includes the d3-format library, so you have access to all d3-format
functions to use with valueFormatter
.
The basic syntax for the d3 function is: d3.format(specifier)(value)
For example:
d3.format(".0%")(0.123); # rounded percentage, "12%"
d3.format("($.2f")(-3.5); # localized fixed-point currency, "(£3.50)"
d3.format("+20")(42); # space-filled and signed, " +42"
d3.format(".^20")(42); # dot-filled and centered, ".........42........."
d3.format(".2s")(42e6); # SI-prefix with two significant digits, "42M"
d3.format("#x")(48879); # prefixed lowercase hexadecimal, "0xbeef"
d3.format(",.2r")(4223); # grouped thousands with two significant digits, "4,200"
For more options, see d3-format examples
If this looks familiar to you, it’s because this JavaScript library is based on Python!
So d3.format(".0%")(0.123)
is like "{:.0%}".format(0.123)
in Python. The specifiers are based on
the Python’s Format Specification.
In dash-ag-grid, use the d3.format function in the column definitions like this:
# example valueFormatter for currency
columnDefs = [
{
"valueFormatter": {"function": "d3.format('($,.2f')(params.value)"},
},
]
Here is a simple example of formatting numbers with d3.format function:
Here are examples using the Plolty “Gapminder” dataset with and without number formatting.
Here are examples of formatting numbers with some of the specifiers available in the d3.format
function.
Locales adapt the behavior of d3-format to various languages and places. The definition may include the following
properties:
decimal
- the decimal point (for example “.”).thousands
- the group separator (for example ","
).grouping
- the array of group sizes (for example [3]
), cycled as needed.currency
- the currency prefix and suffix (for example ["$", ""]
).numerals
- optional; an array of ten strings to replace the numerals 0-9
.percent
- optional; the percent sign (defaults to "%"
).minus
- optional; the minus sign (defaults to "−"
).nan
- optional; the not-a-number value (defaults "NaN"
).Note that the thousands
property is a misnomer, as the grouping definition allows groups other than thousands.
The default locale for d3.format
is U.S. English, which sets:
{
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["$", ""]
}
To render currency numbers in French, you could override these default marks by specifying instead a locale such as:
{
"decimal": ",",
"thousands": " ",
"grouping": [3],
"currency": ["", "€"],
"nan": "",
}
The decimal separator becomes a comma, thousands are separated by a space, and the default currency mark, the euro
symbol, appears after the number.
This example includes changing the default for not-a-number from NaN
to ""
Locales are not loaded by default - you can find them here. Their
definition can be fed to d3.formatLocale
:
locale_en_GB = """d3.formatLocale({
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["£", ""]
})"""
Then in can be used in the valueFormatter
:
columnDefs = [
{
"field": "UK",
"valueFormatter": {"function": f"{locale_en_GB}.format('$,.2f')(params.value)"},
},
]
In this example, each column is formatted with the specifier '$,.2f'
. The locale determines how the numbers will be
displayed.
Note that we also set a custom NaN value in the “France” column. Instead of displaying “NaN” it will be blank.
In this table try changing the specifier and the values.
d3-time-format
Dash AG Grid includes the d3-time-format library, so you have access to
all d3-time-format functions to use with valueFormatter
.
The basic syntax for formatting a date object is:
formatted_date_string = d3.timeFormat(specifier)(date object)
d3.timeFormat()
is like strftime()
in Python.
Note that even if your datetime data is an object on the server, when it’s sent to the grid in the browser it’s
converted to a string.
See Example: Formatting Dates defined as datetime.
If the datetime data uses the ISO string format 'yyyy-mm-dd'
it will be automatically inferred into JavaScript date
object the first time that row data is passed into the grid,
using Cell Data Types.
If the datetime data uses another format like 'dd/mm/yyyy'
, it is possible to:
date_obj= d3.timeParse(specifier)(date string)
d3.timeParse()
is like strptime()
in Python
When the date is converted to a JavaScript date object, then the AG Grid date filter agDateColumnFilter
will work out
of the box, and no additional date filter comparator functions are required.
Here are the specifiers:
%a
- abbreviated weekday name. *%A
- full weekday name. *%b
- abbreviated month name. *%B
- full month name. *%c
- the locale’s date and time, such as %x, %X. *%d
- zero-padded day of the month as a decimal number [01,31].%e
- space-padded day of the month as a decimal number [1,31]; equivalent to %_d
.%f
- microseconds as a decimal number [000000, 999999].%g
- ISO 8601 week-based year without century as a decimal number [00,99].%G
- ISO 8601 week-based year with century as a decimal number.%H
- hour (24-hour clock) as a decimal number [00,23].%I
- hour (12-hour clock) as a decimal number [01,12].%j
- day of the year as a decimal number [001,366].%m
- month as a decimal number [01,12].%M
- minute as a decimal number [00,59].%L
- milliseconds as a decimal number [000, 999].%p
- either AM or PM. *%q
- quarter of the year as a decimal number [1,4].%Q
- milliseconds since UNIX epoch.%s
- seconds since UNIX epoch.%S
- second as a decimal number [00,61].%u
- Monday-based (ISO 8601) weekday as a decimal number [1,7].%U
- Sunday-based week of the year as a decimal number [00,53].%V
- ISO 8601 week of the year as a decimal number [01, 53].%w
- Sunday-based weekday as a decimal number [0,6].%W
- Monday-based week of the year as a decimal number [00,53].%x
- the locale’s date, such as %-m/%-d/%Y
. *%X
- the locale’s time, such as %-I:%M:%S %p
. *%y
- year without century as a decimal number [00,99].%Y
- year with century as a decimal number, such as 1999.%Z
- time zone offset, such as -0700, -07:00, -07, or Z.%%
- a literal percent sign (%).