Formatting & Metadata
Apply number formatting and access card metadata in custom visuals
Custom visuals receive two types of configuration from Semaphor: settings (user-configurable options you define) and card metadata (read-only context Semaphor builds from the source card). Understanding the difference helps you build visuals that respect user configuration.
Settings vs Card Metadata
| Type | Source | Modifiable | Example |
|---|---|---|---|
| Settings | Your plugin manifest | Yes (user configures in UI) | showLegend, accentColor |
| Card Metadata | Semaphor builds from card | No (read-only) | title, formatConfig, kpiConfig |
Settings are options you define in components.config.ts. Users configure them in Semaphor's visual editor.
Card metadata comes from the source card's configuration. It includes the card's title, description, type, and any number formatting the user configured in the native Semaphor editor.
FormatConfig Structure
The formatConfig object contains number formatting rules configured by users in Semaphor's visual editor. Semaphor extracts this from the source card and passes it to your visual.
Which format to use
| Card Type | Use This Format |
|---|---|
| KPI | formatConfig.kpi.primary |
| KPI comparison | formatConfig.kpi.comparison |
| Bar, line, area charts | formatConfig.dataLabels or formatConfig.axes.yAxis |
| Combo charts | formatConfig.axes.yAxis (primary), formatConfig.axes.secondaryYAxis (secondary) |
| Tables | formatConfig.tables.columns or formatConfig.tables.columnMap |
FormatOptions Type
The FormatOptions type defines how numbers should be formatted.
Field reference
| Field | Purpose | Example |
|---|---|---|
type | Format style | 'currency' formats as money |
decimalPlaces | Decimal precision | 2 gives 1,234.56 |
currency | ISO currency code | 'EUR' for euros |
locale | Number/currency locale | 'de-DE' for German formatting |
prefix | Text before value | '~' gives ~$1,234 |
suffix | Text after value | ' ARR' gives $1,234 ARR |
useSuffix | Abbreviate large numbers | true gives 1.2M instead of 1,200,000 |
multiplyBy | Scale factor | 100 for decimal-to-percent |
Formatting Examples
Here's what different format configurations produce:
Currency
Percent
Compact numbers
With prefix and suffix
European locale
Using formatKPIValue Helper
The quickstart template includes a formatKPIValue helper that handles common formatting scenarios. You can copy this into your project or use it as reference.
Using the helper
Building Your Own Formatter
For more control, build a formatter using the native Intl.NumberFormat API.
Date Formatting Utilities
KPI comparisons often include date ranges. The quickstart template provides date formatting utilities that handle timezone correctly.
KPI comparison dates are logical calendar dates resolved on the backend. Always format in UTC to prevent browser timezone shifting (e.g., "2024-01-14" displaying as "Jan 13").
formatDateRange
Formats a date range as "Jan 1, 2024 - Jan 31, 2024":
formatDateShort
Formats with year de-duplication when both dates are in the same year:
Color Ranges
Color ranges let users define conditional coloring based on value thresholds. Use them to highlight good/bad performance.
Applying color ranges
Example usage
Typical color range configuration
Users might configure ranges like:
Comparison Metadata
For KPI cards with comparisons, Semaphor provides metadata about the comparison type and period.
Getting comparison labels
Showing comparison with date range
Calculating Percent Change
For visuals showing comparisons, calculate and display percent change:
Displaying percent change with color
Title and Description Auto-Prefill
Settings named title automatically prefill from the card's title. Users can override this in your plugin's settings panel.
In your component, prefer settings.title (user override) over cardMetadata.title:
Complete Example: Formatted KPI Card
Here's a complete KPI component using all the formatting features:
Next Steps
- Single-Input Visuals - Build visuals with one data source
- Multi-Input Visuals - Combine multiple data sources
- API Reference - Complete props and types documentation