Skip to main content

useFormattedMetricValue

Formats the value of a ChannelValue for display, based on the value type of the metric: numbers are rounded, dates are formatted, and so on.

function useFormattedMetricValue(
channelValue: ChannelValue | null,
options: FormatMetricValueOptions
): FormattedMetricValue | null

To format values outside of rendering, e.g. within the memo function of useMemoizedMetricField, use useFormatMetricValue. It returns the formatting function itself:

function useFormatMetricValue(): (
channelValue: ChannelValue | null,
options: FormatMetricValueOptions
) => FormattedMetricValue | null

Arguments

ArgumentTypeDescription
channelValueChannelValue | nullThe value to format, e.g. as returned by useMetricField.
optionsFormatMetricValueOptionsFormatting options, see below. Pass {} for the defaults.

Options

All options are optional. Each option only applies to the listed value types.

OptionTypeValue typesDescription
precisionnumberNumeric, DurationThe number of decimals. Defaults to 2.
autoConvertUnitbooleanNumeric, DurationConverts the value to the largest unit of the metric type in which the value is still at least 1, e.g. 1.5 GB instead of 1536 MB.
dateTimeFormatstringDateTimeThe format of date and time values, e.g. yyyy-mm-dd HH:MM:ss.
dateFormatstringDateOnlyThe format of date values, e.g. yyyy-mm-dd.
timeFormatstringTimeOnlyThe format of time values, e.g. HH:MM:ss.
formatDateTime(date: Date) => ReactNodeDateTimeCustom formatting of date and time values. Takes precedence over dateTimeFormat.
formatDate(date: Date) => ReactNodeDateOnlyCustom formatting of date values. Takes precedence over dateFormat.
formatTime(date: Date) => ReactNodeTimeOnlyCustom formatting of time values. Takes precedence over timeFormat.
timezonestringDateTime, DateOnly, TimeOnlyConverts dates to this timezone. Only applied together with language.
languagestringDateTime, DateOnly, TimeOnlyThe language used to convert dates to the timezone.
formatResource(resourceId: string) => ReactNodeResourceRenders a resource, e.g. as image.
baseUrlstringResourceRenders the resource as <object>, loaded from this base URL. Ignored if formatResource is set.

The format strings use the masks of the dateformat library, e.g. yyyy for the year, mm for the month, HH for 24-hour hours, MM for minutes and TT for AM/PM.

Returns

A FormattedMetricValue, or null if channelValue is null:

PropertyTypeDescription
valueReactNodeThe formatted value. Strings starting with http:// or https:// are rendered as links.
unitstringThe abbreviation of the unit, if the value has one.
unitPosition'before' | 'after'Whether to display the unit before the value (e.g. currencies) or after it.

How values are formatted depends on the value type of the metric:

Value typeFormatting
Numeric, DurationRounded to precision decimals, unit after the value
CurrencyTwo decimals in the format of the device's locale, unit before the value
DateTime, DateOnly, TimeOnlyFormatted using the options above, otherwise the ISO string. No unit.
ResourceRendered using formatResource or baseUrl, otherwise the resource id. No unit.
Other value typesThe value as string, unit after the value

A metric without a value is formatted as an empty string.

Example

src/widgets/example/Example.tsx
import React from 'react'
import {useFormattedMetricValue, useMetricField} from '@modbros/dashboard-sdk'
import {createMetricResourcePath} from '@modbros/dashboard-core'

export default function Example() {
const channelValue = useMetricField({field: 'metric'})
const formatted = useFormattedMetricValue(channelValue, {
precision: 1,
dateTimeFormat: 'yyyy-mm-dd HH:MM',
formatResource: (resourceId) => <img src={createMetricResourcePath(resourceId)} alt={resourceId}/>
})

if (!formatted) {
return null
}

return (
<span>
{formatted.unitPosition === 'before' && formatted.unit}
{formatted.value}
{formatted.unitPosition === 'after' && formatted.unit}
</span>
)
}

createMetricResourcePath, exported by @modbros/dashboard-core, returns the URL of a resource provided by a plugin.