/** * Shared scale / perf / sanitising helpers for charts under high-volume or hostile data. * * PERF + SIZE GUARD (read before building a chart on top of this): * - Never render more marks than the plot has pixels for. Downsample line/area/scatter * series to ~2 points per horizontal pixel (`downsample`), and bucket bar-like data * (`bucketAggregate`) to the number of bars that fit. * - Never let NaN / Infinity / null reach an SVG attribute. Run values through * `sanitizeNumber` / `finiteValues` and build paths with `pathFromPoints`. * - Attach ONE pointermove handler to the svg (`usePointerHover` + `nearestIndexByX`) * instead of one listener per mark. * - Skip entrance animation above ~300 marks (`useAnimateBudget`), cap the hidden * screen-reader table (`SrOnlyChartTable maxRows`), and thin/truncate category labels. * * Everything here is pure and O(n) (or O(log n)) and never throws on bad input. */ /** A chart point. `y` may be NaN / non-finite to mark a gap. */ export interface XY { x: number; y: number; } export type Aggregate = 'sum' | 'mean' | 'min' | 'max'; /** Finite number, or numeric string parsed to one; otherwise `null` (NaN, ±Infinity, null, junk). */ export declare function sanitizeNumber(v: unknown): number | null; /** Finite number or `fallback` (default 0). */ export declare function safeNum(v: unknown, fallback?: number): number; /** Only the usable numbers from `values`, in order (invalid entries dropped). */ export declare function finiteValues(values: readonly unknown[] | null | undefined): number[]; /** [min, max] of the usable numbers (loop-based: safe for 1M items), or `null` if none. */ export declare function extent(values: readonly unknown[] | null | undefined): [number, number] | null; /** * SVG polyline path. Points with a non-finite x/y (or `null`) break the line (gap) and * are never written to the path, so "NaN" can't appear in `d`. Returns '' when empty. */ export declare function pathFromPoints(points: readonly (XY | null | undefined)[]): string; /** * LTTB downsample to ~`maxPoints` points (first/last and visual shape preserved). * Points with non-finite x/y are gaps: each run of valid points is downsampled * separately (budget split by run length) and ONE gap point is kept between runs, so * `pathFromPoints` still breaks the line. Input is returned as-is when already small. * Output points are the original objects (never copies). */ export declare function downsample(points: readonly T[] | null | undefined, maxPoints: number): T[]; /** * Aggregate `values` into at most `maxBuckets` buckets (contiguous, equal index span). * Invalid entries (NaN/null/Infinity/junk) are skipped; a bucket with no valid entry * is `NaN` (a gap). When already small, returns the values with invalid -> NaN. */ export declare function bucketAggregate(values: readonly unknown[] | null | undefined, maxBuckets: number, agg?: Aggregate): number[]; /** * "Nice" [lo, hi] + ticks covering [min, max]. Handles min==max (incl. 0 and tiny/huge), * negatives, inverted input, non-finite input (-> 0..1), 1e-9 and 1e15 magnitudes. * (Named `niceScale` — `niceTicks` in chartUtils is the legacy 0..max variant.) */ export declare function niceScale(min: number, max: number, count?: number): { lo: number; hi: number; step: number; ticks: number[]; }; /** 1234 -> "1.2K", 3.4e6 -> "3.4M", 5e9 -> "5B"; negatives; small decimals keep 3 significant digits. */ export declare function formatCompact(n: number, locale?: string): string; /** Shorten to `maxChars` with an ellipsis (code-point safe). Callers keep the full text for `title`/tooltip. */ export declare function truncateLabel(str: unknown, maxChars: number): string; /** * Which category labels to draw so they don't collide. * `indices` = positions to show (always includes 0); `rotate` = draw them rotated * (-45deg) when horizontal text can't fit but a rotated line height (~14px) can. */ export declare function thinLabels(labels: readonly unknown[], availablePx: number, charPx?: number, opts?: { allowRotate?: boolean; gap?: number; lineHeight?: number; }): { indices: number[]; step: number; rotate: boolean; }; /** * Top-left position for a tooltip box anchored at (x, y) inside a container. * Default placement is above-centred; flips below when it would clip the top, and is * then shifted so the box stays fully inside [0..containerW] x [0..containerH] * (a box larger than the container pins to 0). */ export declare function clampTooltip(x: number, y: number, boxW: number, boxH: number, containerW: number, containerH: number, gap?: number): { left: number; top: number; placement: 'top' | 'bottom'; }; /** * Index of the x closest to `px` in an ascending-sorted array (binary search, O(log n)). * Returns -1 for an empty array or non-finite `px`. */ export declare function nearestIndexByX(sortedXs: ArrayLike, px: number): number;