import { moneyFormaterFull } from '../../formatters'; /** * Converts a textual date into a localized Date object. * @param date date what will be formatted * @param config for view "Mar 21" set { month: "short", year: "numeric"} or for view "Mar" set { month: "short" } */ const textToDate = (date: string, config: Intl.DateTimeFormatOptions) => { if (date && date !== 'auto') { const dateForFormat = new Date(date); const newViewDate = new Intl.DateTimeFormat('en-US', config).format(dateForFormat); return newViewDate; } return ''; }; /** * Formats a value for use in charts and chart tooltips. * @param value - if value has date format, it will be formatted * @param dataKey optional, type of data for X/Y Axis, if date it will be formatted by type of dataFormat * @param dataFormat optional, type of XAxis date format, it can be: month, day, hour */ const formatterData = (value: string, dataKey?: string, dataFormat?: string) => { if (value === 'auto') return ''; switch (dataKey) { // X axis case 'date': switch (dataFormat) { case 'month': return textToDate(value, { month: 'short' }); case 'day': return textToDate(value, { day: '2-digit', month: 'short' }); case 'hour': return textToDate(value, { hour: 'numeric', minute: 'numeric', hourCycle: 'h23' }); } break; // Y axis default: switch (dataFormat) { case 'percent': return value + '%'; case 'money': return moneyFormaterFull(value); } } return value; }; /** * Formats a label for use in chart tooltips. * @param label - if label has date format, it will be formatted * @param dataKey optional, type of data for XAxis, if date it will be formatted by type of dataFormat * @param dataFormat optional, type of XAxis date format, it can be: month, day, hour */ const formattedLabel = (label: string, dataKey?: string, dataFormat?: string) => { switch (dataKey) { // X axis case 'date': switch (dataFormat) { case 'month': return textToDate(label, { month: 'short', year: 'numeric' }); case 'day': return textToDate(label, { day: '2-digit', month: 'short', year: 'numeric' }); case 'hour': return textToDate(label, { day: '2-digit', month: 'short', year: 'numeric', hour: 'numeric', minute: 'numeric', hourCycle: 'h23' }); } break; } return label; }; /** * Calculates the dynamic maximum height of the chart. * @param dataMax known maximum value from chart data * @param domainCalc set it true if you want to use dynamic maximum value the Y axis * @param domainMax initial maximum value for the Y axis * @param secondYAxisDomainCalcMultiplier optional, calc multiplier */ const setDomainCalc = ( dataMax: number | undefined, domainCalc: boolean, domainMax: number, secondYAxisDomainCalcMultiplier = 1 ) => { if (dataMax && domainCalc) { return ( Math.ceil(dataMax * secondYAxisDomainCalcMultiplier * 1.2) + (10 - (Math.ceil(dataMax * secondYAxisDomainCalcMultiplier * 1.2) % 10)) ); } return domainMax; }; /** * Formats the string to a valid ID. * @param id input string */ const formatterId = (id: string) => { return id.replace(/\s+/g, '_'); }; /** * Formats the number to a string with percentage unit. * @param value number representing percentage */ const { format: formatPercent } = new Intl.NumberFormat('en-US', { style: 'unit', unit: 'percent', maximumFractionDigits: 1 }); export { formatterData, formattedLabel, setDomainCalc, textToDate, formatterId, formatPercent };