UNPKG

@carbon/charts

Version:
134 lines 5.63 kB
import { format } from 'date-fns'; import { Tools } from '../tools'; // D3 Imports import { min } from 'd3-array'; export var TIME_INTERVALS = [ ['15seconds', 15 * 1000], ['minute', 60 * 1000], ['30minutes', 30 * 60 * 1000], ['hourly', 60 * 60 * 1000], ['daily', 24 * 60 * 60 * 1000], ['monthly', 30 * 24 * 60 * 60 * 1000], ['quarterly', 3 * 30 * 24 * 60 * 60 * 1000], ['yearly', 12 * 30 * 24 * 60 * 60 * 1000], ]; // Return true if the tick is a primary tick, false otherwise export function isTickPrimary(tick, i, allTicks, interval, showDayName) { var isFirstTick = i === 0; var hasANewWeekStarted = Number(format(new Date(tick), 'c')) === 2; var isFirstQuarter = Number(format(new Date(tick), 'q')) === 1; var previousTick = i !== 0 ? allTicks[i - 1] : null; switch (interval) { case '15seconds': return (isFirstTick || isDayOfMonthChanged(tick) || isMonthChanged(tick, previousTick) || isYearChanged(tick)); case 'minute': return (isFirstTick || isDayOfMonthChanged(tick) || isMonthChanged(tick, previousTick) || isYearChanged(tick)); case '30minutes': return (isFirstTick || isDayOfMonthChanged(tick) || isMonthChanged(tick, previousTick) || isYearChanged(tick)); case 'hourly': return (isFirstTick || isDayOfMonthChanged(tick) || isMonthChanged(tick, previousTick) || isYearChanged(tick)); case 'daily': if (!showDayName) { // daily return (isFirstTick || isMonthChanged(tick, previousTick) || isYearChanged(tick)); } else { // weekly return isFirstTick || hasANewWeekStarted || isYearChanged(tick); } case 'monthly': return isFirstTick || isYearChanged(tick); case 'quarterly': return isFirstTick || isFirstQuarter; case 'yearly': return false; default: throw new Error(interval + " is not a valid time interval."); } } // Return the formatted current tick export function formatTick(tick, i, allTicks, interval, timeScaleOptions) { var showDayName = timeScaleOptions.showDayName; var intervalConsideringAlsoShowDayNameOption = interval === 'daily' && showDayName ? 'weekly' : interval; var date = new Date(tick); var formats = Tools.getProperty(timeScaleOptions, 'timeIntervalFormats')[intervalConsideringAlsoShowDayNameOption]; var primary = Tools.getProperty(formats, 'primary'); var secondary = Tools.getProperty(formats, 'secondary'); var formatString = isTickPrimary(tick, i, allTicks, interval, showDayName) ? primary : secondary; var locale = timeScaleOptions.localeObject; return format(date, formatString, { locale: locale }); } // Given a timestamp, return an object of useful time formats // Use Unicode date field symbol (https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table) export function getTimeformats(timestamp) { var date = new Date(timestamp); return { M: date.getMonth() + 1, d: date.getDate(), H: date.getHours(), m: date.getMinutes(), s: date.getSeconds(), }; } // Find the differences between consecutive numbers in an array function getConsecutiveDifferences(elements) { if (!elements) { return; } return elements.slice(1).map(function (elem, i) { return elem - elements[i]; }); } // Given a duration in ms, return the closest TIME_INTERVAL name function closestTimeIntervalName(duration) { var index = TIME_INTERVALS.reduce(function (nearestIndex, _a, i) { var intervalName = _a[0], delta = _a[1]; var deltaNearest = TIME_INTERVALS[nearestIndex][1]; var oldNearestSpan = Math.abs(deltaNearest - duration); var currentSpan = Math.abs(delta - duration); return oldNearestSpan < currentSpan ? nearestIndex : i; }, 0); return TIME_INTERVALS[index][0]; } // Given an array of timestamps, return the interval name // between 15seconds, minute, 30minutes, hourly, daily, weekly, monthly, quarterly, yearly export function computeTimeIntervalName(ticks) { // special case: if the dataset has only one datum, we show the tick in the most detailed way possible if (ticks.length === 1) { return '15seconds'; } var differences = getConsecutiveDifferences(ticks); var minDifference = min(differences); return closestTimeIntervalName(minDifference); } // Return true if the day of the month (D = 1-31) is changed, false otherwise function isDayOfMonthChanged(timestamp) { var _a = getTimeformats(timestamp), s = _a.s, m = _a.m, H = _a.H; return H === 0 && m === 0 && s === 0; } // Return true if the month (M = 1-12) is changed from previous tick's timestamp, false otherwise function isMonthChanged(timestamp, previousTimestamp) { var currentMonth = getTimeformats(timestamp).M; var previousMonth = getTimeformats(previousTimestamp).M; return currentMonth !== previousMonth; } // Return true if the year (YYYY) is changed, false otherwise function isYearChanged(timestamp) { var _a = getTimeformats(timestamp), M = _a.M, d = _a.d, s = _a.s, m = _a.m, H = _a.H; return M === 1 && d === 1 && H === 0 && m === 0 && s === 0; } //# sourceMappingURL=../../src/services/time-series.js.map