/** * Copyright (c) 2025 Elara AI Pty Ltd * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details. */ /** * The scale a canvas positions by (#1148): Plan's canvas and the Calendar's * views share it. It is pure arithmetic, with no DOM: a window cut into whole * periods, its buckets and their ruler labels, an instant's place in it as a * window fraction, snapping, and the steps a move takes. * * A scale is built over a numeric DOMAIN ({@link ScaleDomain}): epoch ms for * time, a value, or a list's index. One engine ({@link bucketScale}) serves * every axis a canvas has. {@link timeScale} builds a time scale over `Date`s, * the Calendar's, and Plan builds its time, number and ordinal axes over its * own instant values, with {@link timeDomain} as its time arm. * * Every row positions against the one scale. Span bars position * **continuously** (`fracOf` on real instants), and what is quantised to a * bucket (heat, tables, cards, a calendar's columns) by the **bucket** * (`bucketOf`). Both truths coexist on the one scale, so a 12-week window at * WEEK resolution is exactly 12 columns, and sibling rows line up to the * pixel. * * An instant of another kind than the domain's positions nowhere (`NaN`, * `-1`, `undefined`). On a domain whose interval END names its last bucket * (`endInclusive`: Plan's ordinal axis) `endFracOf` is the far edge of the * named bucket; on the others intervals stay half-open and `endFracOf` is * `fracOf`. * * Time is **UTC** (#326): East DateTime values are UTC instants, so a window * derives the same columns under any viewer's timezone. Weeks are **ISO * weeks**, Monday-start (the `W27` ruler), so the week interval is * `utcMonday`, not d3's Sunday-based `utcWeek`. * * @packageDocumentation */ import { type TimeInterval } from "d3-time"; import { type Formatters } from "@elaraai/east-ui-components"; import { type TimeResolution } from "./calendar-math.js"; export { addMonthsClamped, shiftCalendar, type TimeResolution } from "./calendar-math.js"; /** A half-open time window `[min, max)`. */ export interface TimeWindow { /** Inclusive start instant. */ min: Date; /** Exclusive end instant. */ max: Date; } /** * The most buckets a scale cuts. Every bucket is a ruler tick and a grid * column, so an absurd window at a fine resolution truncates rather than * locking the tab. The scale says so (`Scale.truncated`), and a canvas shows * it: a silently truncated axis reads as data that stops (#811). */ export declare const MAX_BUCKETS = 500; /** * Whole periods a scale lays out beyond each window edge (#619): the slide a * pan can travel before its revealed edge runs out of content. Marks there * are clipped at rest, so they cost DOM but no pixels. A list (Plan's ordinal * axis) has nothing beyond it, and lays out none. */ export declare const OVERSCAN_BUCKETS = 2; /** * The finer unit a move takes under Shift on a time scale, in ms, per * resolution (#825): fifteen minutes under an hour, an hour under a day, and a * day under a week, a month, a quarter or a year. */ export declare const FINE_MS: Readonly>; /** * The d3-time UTC interval for a resolution: a quarter is three months, and a * week the ISO week, Monday-start. * * @param res - The resolution * @returns Its interval */ export declare function resolutionInterval(res: TimeResolution): TimeInterval; /** * The ISO-8601 week number of a UTC instant. * * @param d - The instant * @returns Its week, 1–53 */ export declare function isoWeekUTC(d: Date): number; /** * What a time scale says itself (#820): its ruler's week and quarter ticks, * and a week or a quarter read out. Each is a function of named parameters, * already formatted for the locale, so a translation puts them where its * grammar wants them. A canvas's own message table extends it. */ export interface TimeMessages { /** A week tick: `W27`. */ rulerWeek: (p: { week: string; }) => string; /** A quarter tick: `Q3`. */ rulerQuarter: (p: { quarter: string; }) => string; /** A week as words: `Week of Jun 29, 2026`. */ periodWeek: (p: { date: string; }) => string; /** A quarter as words: `Q3 2026`. */ periodQuarter: (p: { quarter: string; year: string; }) => string; } /** The time scale's English messages: the default table. */ export declare const timeMessages: TimeMessages; /** * The words a time scale speaks: its messages, and its locale's formatters * (the shared format module's, #850), so a ruler prints dates exactly as * every other component in the app does. Dates are UTC: the words for a * bucket never depend on the viewer's timezone. */ export interface TimeWords extends Formatters { /** The message table in effect. */ m: TimeMessages; } /** * Build a time scale's words for a locale and a message table. * * @param locale - The BCP 47 locale * @param m - The message table ({@link timeMessages} when omitted) * @returns The words */ export declare function timeWords(locale: string, m?: TimeMessages): TimeWords; /** The words a time scale speaks when it is given none: English, in `en-US`. */ export declare const TIME_WORDS: TimeWords; /** * The default ruler label of the period starting at a UTC instant, in the * scale's locale (#820): week ⇒ the ISO week (`W27`), day ⇒ the uppercase * short weekday (`MON`), hour ⇒ the 24-hour `HH:mm`, month ⇒ the uppercase * short month (`JUN`), quarter ⇒ `Q3`, year ⇒ the year. The week and quarter * prefixes are messages; the rest is the locale's own names and digits. * * @param start - The period's start (UTC) * @param res - The resolution * @param w - The scale's words * @returns The label */ export declare function defaultTickLabel(start: Date, res: TimeResolution, w?: TimeWords): string; /** * The period starting at a UTC instant as words, at a resolution: what a * reader hears where the ruler only has a tick (#819), in the scale's locale * (#820). * * @param start - The period's start (UTC) * @param res - The resolution * @param w - The scale's words * @returns `Week of Jun 29, 2026`, `Mon, Jun 29, 2026`, `June 2026`, `Q3 2026`, … */ export declare function periodText(start: Date, res: TimeResolution, w?: TimeWords): string; /** One bucket of a scale. */ export interface ScaleBucket { /** Bucket index (0-based). */ index: number; /** Bucket start instant (period-aligned: the window widens to whole periods, #949). */ start: I; /** Bucket end instant (exclusive). On a domain whose end names its last * bucket (`endInclusive`) the bucket IS its value, so `end` names the same * value as `start`. */ end: I; /** Left edge as a window fraction (0–1). */ x0: number; /** Right edge as a window fraction (0–1). */ x1: number; /** The ruler tick label. */ label: string; } /** * The period a scale cuts by, as a fold reads it (#824): ONE object per key, * shared by every scale with the same resolution or step, so what depends on * it re-derives when the resolution changes and not when the window pans. */ export interface ScalePeriod { /** Names the period: two periods with one key floor every instant alike. */ readonly key: string; /** The start of the period holding an instant; the instant itself when it is of another kind than the domain's. */ floor(t: I): I; } /** * An axis's numeric domain: what {@link bucketScale} cuts into periods, and * how an instant of its kind reads as a number on it. * * @typeParam I - The instant the scale positions * @typeParam K - The axis's kind */ export interface ScaleDomain { /** The axis's kind: every instant must be of it. */ kind: K; /** Names the period: what `floor` depends on, and nothing else (never the window). */ periodKey: string; /** The window's start, as a domain number, before it widens to whole periods. */ minN: number; /** The window's exclusive end, as a domain number, before it widens to whole periods. */ maxN: number; /** Period-align a domain number downward. */ floor(n: number): number; /** Shift a domain number by `k` whole periods. */ offset(n: number, k: number): number; /** * A move's step (#825): a domain number `k` units on. A time domain steps * by the calendar (a month on from the 31st is a shorter month's last * day), or by its finer unit under Shift (`fine`); a domain whose bucket is * its step steps as `offset` does. */ shift(n: number, k: number, fine: boolean): number; /** The finer unit a move takes under Shift, in domain numbers; `undefined` where a bucket is the step. */ fineUnit: number | undefined; /** An instant as a domain number: `NaN` for one of another kind, or unknown. */ toN(t: I): number; /** The instant at a domain number. */ fromN(n: number): I; /** The drag grammar's slot key for an instant of this domain (`CellRefType.slot`), as its kind spells it. */ slot(t: I): string; /** The ruler label of the period starting at a domain number. */ label(n: number): string; /** A domain number as words: an accessible name's instant (#819). */ text(n: number): string; /** The period starting at a domain number, as words (#819). */ periodText(n: number): string; /** Whole periods laid out beyond each window edge. */ overscan: number; /** Whether an interval END names its last bucket (inclusive), rather than an edge. */ endInclusive: boolean; /** Whether there is nothing beyond the window: a list, where a move stops at its ends. */ bounded: boolean; /** The period's floor where it is not the domain's own: a list's period is the identity, whatever the list. */ periodFloor?: ((t: I) => I) | undefined; /** The concrete resolution: a time domain's only. */ resolution: TimeResolution | undefined; /** The now instant, as a domain number. */ now: number | undefined; } /** * The one scale a canvas positions against: its window, its buckets, and the * continuous and quantised mappings. Every function takes an instant of the * domain's kind; one of another kind positions nowhere. * * @typeParam I - The instant it positions * @typeParam K - The axis's kind */ export interface Scale { /** The axis's kind: every instant must be of it. */ kind: K; /** The window `[min, max)` as instants, widened to whole periods (#949). On * a domain whose end names its last bucket, `max` names its last value. */ window: { min: I; max: I; }; /** The concrete bucket resolution: a time scale's only. */ resolution: TimeResolution | undefined; /** Bucket count (`n = window ÷ period`: whole periods, #949). */ n: number; /** * Set when the window holds more periods than {@link MAX_BUCKETS}: the grid * covers only the first `shown` buckets of it. A canvas says so ("showing * the first 500 buckets — zoom in", #811). */ truncated: { shown: number; } | undefined; /** The buckets, in order. */ buckets: ReadonlyArray>; /** Continuous position: the window fraction of an instant, clamped to [0, 1]. */ xOf(t: I): number; /** Continuous position, unclamped: negative before the window, over 1 past it (runoff detection). */ fracOf(t: I): number; /** * The fraction an interval END lands at: `fracOf(t)` on a half-open * domain; on one whose end names its last bucket, that bucket's far edge. */ endFracOf(t: I): number; /** The bucket index containing an instant, or −1 outside the window. */ bucketOf(t: I): number; /** * The bucket index at a window FRACTION (pointer x ÷ plot width), or −1 * when the fraction lands in no bucket. * * The one fraction → bucket resolver: the cursor readout, the drop * coordinate and the landing preview all read it (#617). Buckets are * half-open, which would make the covered range's exact right edge * unreachable, so a pointer there closes into the last bucket. On a * truncated scale the uncovered remainder is NO bucket (#618): fractions * past the last bucket's end answer −1, exactly as `bucketOf` does. */ bucketAtFrac(frac: number): number; /** Snap an instant to the nearest bucket edge. */ snap(t: I): I; /** Period-align an instant downward: the period containing it. */ floor(t: I): I; /** The period this scale buckets by, as a fold reads it (#824): see {@link ScalePeriod}. */ period: ScalePeriod; /** Shift an instant by `k` whole periods: pans, zooms, the one-period floor. */ offset(t: I, k: number): I; /** * Move an instant `k` units, as a move does (#825): by the calendar on a * time scale, or by its finer unit under Shift (`fine`); by the step * elsewhere. An instant of another kind stays where it is. */ shift(t: I, k: number, fine: boolean): I; /** The finer unit a move takes under Shift, in domain numbers; `undefined` where a bucket is the step. */ fineUnit: number | undefined; /** Whether an interval END names its last bucket (inclusive), rather than an edge. */ endInclusive: boolean; /** Whether there is nothing beyond the window: a list, where a move stops at its ends. */ bounded: boolean; /** The drag grammar's slot key for an instant of the scale's kind (`CellRefType.slot`). */ slotOf(t: I): string; /** An instant as a number on the scale's own domain (`NaN` for one of another kind). */ toNumber(t: I): number; /** The inverse of {@link toNumber}: the instant at a domain number. */ fromNumber(n: number): I; /** The now instant's window fraction, when inside the window. */ nowFrac: number | undefined; /** * The RENDER-cull bounds, in window fractions (#619): the window plus * {@link OVERSCAN_BUCKETS} whole periods each side (the right side * extends from the COVERED edge on a truncated scale). Marks anchored to * the window cull against these instead of `[0, 1]`, so a pan reveals real * content; the ruler, the grid, `bucketOf` and every drop coordinate stay * on the window's buckets. */ renderMin: number; /** The far RENDER-cull bound: see {@link renderMin}. */ renderMax: number; /** * The bucket containing an instant across the window and its overscan: * RENDER geometry only (interactions use {@link bucketOf}); `undefined` * outside the render bounds. Overscan buckets carry indices outside the * window's (negative on the left, `n` and up on the right) and fractions * outside `[0, 1]`. */ renderBucketOf(t: I): ScaleBucket | undefined; /** * An instant as words, as an accessible name says it (#819): a full UTC * date on a time scale, in its locale (`Jun 29, 2026` in `en-US`, with * the time when the instant has one or the scale runs at hour * resolution). `""` for an instant of another kind. */ instantText(t: I): string; /** * A bucket as words (#819): the period it covers, where the ruler label * is only a tick (`W27`, `MON`): `Week of Jun 29, 2026`, `Mon, Jun 29, * 2026`, `July 2026`, `Q3 2026` in `en-US`. */ bucketText(b: ScaleBucket): string; } /** * The bucket a window fraction falls in, clamped into the buckets the scale * covers: before the window, its first bucket; past its covered end, its last. * Where a drag that must land somewhere lands. * * @param scale - The scale * @param frac - The window fraction (one that is not finite reads as the window's start) * @returns The bucket's index */ export declare function clampedBucketAt(scale: Scale, frac: number): number; /** * Cut a domain's window into a scale. * * The window widens OUTWARD to whole periods (#949): its start floors to the * period containing it, and its end rises to the next period edge, so every * column is a whole period. A 12-week window at MONTH resolution draws whole * months (June to September), never a two-day June sliver beside two thirds * of September. An aligned window is unchanged: a 12-week window at WEEK * resolution is exactly 12 equal columns. * * @param dom - The domain, with its window * @param periods - The periods the caller has handed out, by key: one map per * instant type, so every scale over that type with the same period shares * its {@link ScalePeriod} * @returns The scale, or `undefined` for an empty or inverted window */ export declare function bucketScale(dom: ScaleDomain, periods: Map>): Scale | undefined; /** What a time scale is built from. */ export interface TimeScaleSpec { /** The window the scale covers; it widens outward to whole periods. */ window: TimeWindow; /** The period the window is cut into. */ resolution: TimeResolution; /** The instant the now line marks, when inside the window. */ now?: Date | undefined; /** A date-token pattern for the ruler's labels (East's date tokens), the author's own; the locale's ruler vocabulary otherwise. */ format?: string | undefined; /** The words its ruler and its accessible names speak ({@link TIME_WORDS} when omitted). */ words?: TimeWords | undefined; } /** * A time window's domain: epoch ms, cut into the resolution's periods, over * any instant type. `wrap` makes an instant of a `Date`, and `toMs` reads one * back (`NaN` for an instant of another kind, which then positions nowhere). * Its slot key is the instant as East prints a DateTime, through the drag * layer's shared codec. * * @param spec - The window, the resolution and the words * @param wrap - An instant at a `Date` * @param toMs - An instant's epoch ms, or `NaN` * @returns The domain */ export declare function timeDomain(spec: TimeScaleSpec, wrap: (d: Date) => I, toMs: (t: I) => number): ScaleDomain; /** * A time scale over `Date`s: a window cut into a resolution's whole periods. * * @param spec - The window, the resolution and the words * @returns The scale, or `undefined` for an empty or inverted window */ export declare function timeScale(spec: TimeScaleSpec): Scale | undefined; //# sourceMappingURL=scale.d.ts.map