Drafter.Visualization.LTTB (drafter v0.3.1)

Copy Markdown View Source

Largest Triangle Three Buckets downsampling algorithm.

Reduces a dataset to a target number of points while preserving the visual shape of the data. Operates in O(n) time with a single pass.

Summary

Functions

Downsamples a list of {x, y} tuples or [x, y] lists to target_count points.

Downsamples a simple list of y-values to target_count points.

Functions

downsample(points, target_count)

@spec downsample([{number(), number()} | [number()]], pos_integer()) :: [
  {number(), number()} | [number()]
]

Downsamples a list of {x, y} tuples or [x, y] lists to target_count points.

Always keeps the first and last points. For each intermediate bucket, selects the point that forms the largest triangle area with the previously selected point and the average of the next bucket.

Returns points unchanged when length(points) <= target_count, and just the first point when target_count is less than 2. Points may be {x, y} tuples or [x, y] lists; whichever form goes in comes back out.

Examples

iex> Drafter.Visualization.LTTB.downsample([{0, 0}, {1, 5}, {2, 1}], 5)
[{0, 0}, {1, 5}, {2, 1}]

iex> Drafter.Visualization.LTTB.downsample([{0, 0}, {1, 9}, {2, 1}, {3, 2}], 3)
[{0, 0}, {1, 9}, {3, 2}]

iex> Drafter.Visualization.LTTB.downsample([{0, 0}, {1, 9}, {2, 1}], 1)
[{0, 0}]

iex> Drafter.Visualization.LTTB.downsample([], 10)
[]

downsample_series(values, target_count)

@spec downsample_series([number()], pos_integer()) :: [number()]

Downsamples a simple list of y-values to target_count points.

Generates implicit x indices, applies LTTB downsampling, and returns only the y-values from the result.

Returns the values unchanged when length(values) <= target_count.

Examples

iex> Drafter.Visualization.LTTB.downsample_series([1, 2, 3], 10)
[1, 2, 3]

iex> Drafter.Visualization.LTTB.downsample_series([1, 9, 2, 3], 3)
[1, 9, 3]