@opendatasoft/visualizations
Version:
Opendatasoft's components to easily build dashboards and visualizations.
30 lines (29 loc) • 1.43 kB
TypeScript
/**
* Time Scale Validation
*
* Prevents ChartJS from crashing when a time unit is incompatible with the data range.
* Example: timeUnit="day" with data spanning 1600-2020 (420 years) would generate
* ~153,000 ticks, exceeding ChartJS's 100,000 limit and causing a hard crash.
*/
import type { DataFrame } from 'types';
import type { TimeCartesianAxisConfiguration } from './types';
export type TimeUnit = NonNullable<TimeCartesianAxisConfiguration['timeUnit']>;
/**
* Checks if a time unit is safe to use with the given min/max values.
* Used by the Platform to filter available timescale options in the UI.
*
* @example
* isTimeUnitCompatible("1600-01-01", "2024-01-01", "day") // false (424 years!)
* isTimeUnitCompatible("1600-01-01", "2024-01-01", "year") // true
* isTimeUnitCompatible("2024-01-01", "2024-12-31", "day") // true
*/
export declare function isTimeUnitCompatible(min: unknown, max: unknown, unit: TimeUnit): boolean;
/**
* Returns a safe time unit for ChartJS, or undefined to let it auto-detect.
* Used internally by scales.ts to prevent crashes.
*
* @param dataFrame - The chart data
* @param requestedUnit - The user-configured time unit (may be undefined)
* @param labelColumn - The column containing date values (default: 'x')
*/
export declare function getSafeTimeUnit(dataFrame: DataFrame, requestedUnit: TimeUnit | undefined, labelColumn?: string): TimeUnit | undefined;