SplitByFormatter
The SplitByFormatter class enables conditional formatting based on the value of a split-by field. This allows you to apply different formatters to different categories or groups within your data, making it ideal for scenarios where different measures require different formatting rules when using measure names/measure values.
Constructor
new SplitByFormatter(options)
Parameters
| Property | Type | Required | Description |
|---|---|---|---|
| options | SplitByFormatterOptions | true | Configuration specifying the split field and value-specific formatters |
Options
{
splitByField: string,
values: {
[value: string]: PlainFormatter | undefined
}
}
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
| splitByField | string | true | '' | The name of the field whose values determine which formatter to apply |
| values | Object | true | Map of field values to their corresponding formatters |
Basic Usage
const splitFormatter = new SplitByFormatter({
splitByField: 'Product Category',
values: {
'Electronics': new NumberFormatter('currency', { currency: 'USD' }),
'Food': new NumberFormatter('decimal', { decimalDigits: 2 }),
'Clothing': new NumberFormatter('currency', { currency: 'USD', decimalDigits: 0 })
}
});
// The formatter will apply different formatting based on the category
// For an Electronics row: $1,234.56
// For a Food row: 1,234.56
// For a Clothing row: $1,235
Methods
format()
Formats a value based on the split-by field value in the row data.
format(value: MuzeDatum, rowData: RowData, defaultFormattedValue?: string): string
Parameters:
| Property | Type | Required | Description |
|---|---|---|---|
| value | MuzeDatum | true | The value to format |
| rowData | RowData | true | The complete row data containing the split-by field |
| defaultFormattedValue | string | false | Fallback value if no formatter matches |
Returns: String - The formatted value
formatterFunc()
Returns a formatter function for use with Muze visualizations.
formatterFunc(): (dataInfo: MuzeType) => string
Returns: Function - A formatter function that can be passed to Muze configuration
The returned function automatically extracts row data from the Muze data context and applies the appropriate formatter.