Skip to main content
cngx-src documentation

downsampleLTTB

Functioncommon/chart/buffer

projects/common/chart/buffer/lttb.ts

Description#

Largest-Triangle-Three-Buckets downsampling. Reduces a dense series to targetSize points while preserving the perceptual shape of the line - peaks, troughs, and inflections survive where naive uniform (every-Nth) sampling would drop them. This is the realtime-charting literature's default downsampler (Steinarsson 2013).

Pure TS: no Angular, no signal, no RxJS dependency. Tree-shakeable, so static-chart consumers that never buffer pay nothing. The <cngx-chart> buffer (injectChartBuffer) is the single downsampling boundary at v2; this is its algorithm.

The first and last points are always kept. The middle targetSize - 2 points are chosen one per bucket by the largest-triangle-area heuristic: for each bucket, the point forming the largest triangle with the previous selected point and the next bucket's average is retained.

Signature#

downsampleLTTB(data, targetSize: number, xAccessor, yAccessor)

Parameters#

@paramdata

source series, read-only.

@paramtargetSizenumber

desired output length. When >= data.length (or <= 0), the input is returned unchanged by reference - no allocation, no copy.

@paramxAccessor

projects a row to its numeric X (typically a timestamp or positional index).

@paramyAccessor

projects a row to its numeric Y (the value the triangle area is computed against).

Returns#

T[]

a fresh array of at most targetSize rows, or the input by reference when no reduction is needed.