Choose a visible-resolution data product and preserve first, last, minimum and maximum values in each bucket.
Large chart series: reduce marks without erasing spikes
More samples than pixels need a display rule
A line chart with hundreds of thousands of readings across a 900-pixel plot cannot show each reading distinctly. Sending every point still costs transfer, parsing, memory, layout and interaction time. A random sample or every-50th-point stride can erase a brief but operationally important spike. Decide whether the reader is inspecting shape, extrema, exact intervals or anomalies. The chart task determines the reduction method. The full-resolution data remains the source; the plotted series is a deliberately derived view.
Preserve a bounded envelope
For each time bucket, retain its first, last, minimum and maximum observations, ordered by timestamp and deduplicated by identity. This keeps endpoints connected and exposes local peaks and troughs that a mean would smooth away. It is not lossless: the order of several internal turns and narrow oscillations may still be compressed. Show the bucket width or resolution when it affects interpretation, and fetch raw samples on zoom or request. If missing intervals exist, split segments before reducing; joining across a gap creates a fictional line. Gap policy applies before level of detail.
Build levels against stable data versions
Precomputed hourly, daily or pixel-width buckets must use a fixed time origin and a named aggregation function. If a late event revises an old bucket, invalidate that level together with raw data. Cache keys need range, resolution, filters, unit and source generation. A multi-resolution chart can request a coarser level during pan, then replace it with fine points after the interaction settles; label provisional rendering when it might change a decision. Serving freshness constrains what a pre-aggregated chart can claim.
Keep exactness available
Tooltips on reduced marks should describe the bucket, extrema and sample count. Calling a retained minimum a point-in-time average is wrong. A downloadable CSV should offer the filtered raw interval or clearly name the reduced level. If a user clicks a peak to investigate a machine stoppage, resolve it to source IDs rather than an arbitrary nearby display point. The export manifest should state whether the series is raw or reduced.
Measure end-to-end cost
Track transfer bytes, parse time, memory, first paint, pan latency and number of marks actually drawn. A server-side envelope can reduce browser work but adds query and cache-maintenance cost. A canvas renderer can draw many marks faster than DOM-based SVG, yet it needs deliberate keyboard and text alternatives. Test a single-sample range, identical timestamps, gaps and a narrow spike at each bucket boundary. The acceptance check is not only a speedup: the operational alert must remain visible at the default zoom.
Worked contract
def extrema_envelope(samples: list[tuple[int, float]],
bucket_size: int) -> list[tuple[int, float]]:
if bucket_size < 1 or any(samples[index][0] >= samples[index + 1][0]
for index in range(len(samples) - 1)):
raise ValueError("bucket size or time order is invalid")
reduced = []
for start in range(0, len(samples), bucket_size):
bucket = samples[start:start + bucket_size]
selected = {bucket[0], bucket[-1],
min(bucket, key=lambda point: point[1]),
max(bucket, key=lambda point: point[1])}
reduced.extend(sorted(selected, key=lambda point: point[0]))
return reduced
readings = [(47, 4.0), (48, 4.1), (49, 81.0), (50, 4.2),
(51, 4.0), (52, 3.9)]
visible = extrema_envelope(readings, 4)
assert (49, 81.0) in visible and visible[0] == readings[0]
assert len(visible) <= 4 * ((len(readings) + 3) // 4)Performance and operating cost
For N sorted samples and bucket size B, the demonstration runs in O(N + (N/B) log 4) time and emits at most four observations per bucket, O(N/B) output space. Python slices hold up to O(B) temporary items. It assumes strictly increasing timestamps and one continuous segment; production data with same-time events needs an explicit tie policy, and gaps need separate segments. Maintaining a pyramid of levels increases storage but can make viewport requests proportional to visible resolution rather than total history.
Common Mistakes
- Taking every nth reading and deleting a short-lived alarm spike.
- Reducing across a missing-data gap and drawing a continuous line.
- Calling a retained minimum or maximum a bucket average.
- Caching a reduced level without the filter and source-version keys.
Validation drill
Reduce a 47,000-point sensor series to a 900-pixel view. Insert 19 one-point spikes at varied bucket positions and verify every spike survives the chosen display reduction; compare load and pan times with the raw series.
Read next
- Time-series charts: distinguish missing days, zeros and provisional periods
- Interactive dashboards: recompute cohorts after linked filters
- Chart exports: carry the data, filters and render contract together
- Serving indexes and freshness contracts
