Skip to content

feat(Chart): Add lazy to defer building charts until near the viewport - #996

Open
techniq wants to merge 2 commits into
mainfrom
techniq/add-chart-lazy-to-defer-creation-rendering
Open

techniq wants to merge 2 commits into
mainfrom
techniq/add-chart-lazy-to-defer-creation-rendering

Conversation

@techniq

@techniq techniq commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Resolves #986

Adds lazy to Chart, which skips building a chart until its container is near the viewport. On a page of many charts, the ones far below the fold cost almost nothing until they're scrolled to.

<LineChart {data} x="date" y="value" lazy />

Behaviour

  • The container renders right away at full size, so nothing shifts when the chart builds. Only the inside of the chart waits: marks, axes, tooltip, and the brush/transform code that loads on demand.
  • A chart already on screen builds in the same render pass. Every lazy chart checks its position once before any of them build, so the browser lays out once for all of them, and there's no empty first frame while waiting for the IntersectionObserver.
  • Other charts build when they come within 200px of the viewport. lazy={{ rootMargin, threshold }} tunes this.
  • Once built, a chart stays built, so it keeps its tooltip, brush and zoom state.
  • A chart inside a hidden container (e.g. an inactive tab) builds when the container is shown.
  • With ssr, the server renders the empty container.
  • placeholder snippet: rendered inside the sized container until the chart is built, e.g. a loading state.
  • setSettings({ lazy: true }) makes lazy the default for every chart below it. A chart's own lazy, including false, wins (same pattern as view).

The viewport logic is a small internal attachment, attachments/nearViewport.ts. It isn't exported.

Numbers

  • 50 stacked LineCharts with 500 points each, about 3 on screen: mount time drops from 212 ms to 19 ms. The 19 ms includes fully building the charts that are on screen.
  • Charts not using lazy (benched against main, alternating runs): mount time is unchanged within noise. Chart.base grows by about 300 B gzipped.

Known limitation: experimental.async

With Svelte's experimental.async, a lazy chart that comes into view while an await elsewhere on the page is still pending can freeze the page. This is sveltejs/svelte#18997, which is still open; the fix is sveltejs/svelte#19030, not yet released. The Chart docs carry a note about it.

The bug also showed up as a chart never building at all: in that situation its stored container element (ref) read as undefined. To avoid that, the chart now measures the element the observer hands it when it activates, instead of relying on ref.

For the same reason, the docs keep Example.svelte's own lazy loading rather than switching to setSettings({ lazy: true }). With every example's await starting at page load, /docs/guides/transform froze. That lazy loading also defers each example's data (about 1.5–1.7 MB up front on the GeoPath and transform pages), which lazy doesn't. Worth revisiting once the Svelte fix is released.

Docs

  • New "Lazy rendering" section on the Chart page, with a live Chart/lazy example: 100 charts in a scroll box, each with a placeholder.
  • lazy added to the settings example in the State guide.
  • Catalog and screenshots aren't regenerated; that will be a separate commit.

Tests

  • Chart.lazy.svelte.test.ts: on-screen charts build in the first pass, off-screen ones wait, a chart builds on scroll and stays built, the placeholder shows and goes away, the setting is used as the default, and a chart's own lazy={false} overrides it.
  • Chart and simplified-chart suites (145 tests) and SSR tests (25) pass; svelte-check shows 0 errors.
  • Benchmark: src/lib/bench/lazy.svelte.bench.ts.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9c8591c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
layerchart Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/layerchart@996

commit: 9c8591c

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor
built with Refined Cloudflare Pages Action

⚡ Cloudflare Pages Deployment

Name Status Preview Last Commit
layerchart ✅ Ready (View Log) Visit Preview 2f41ff8

@github-actions

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

Use-Case Scenarios

Core (7 changed)
Scenario Current New Change
🟡 core-canvas 198.49 KB
52.61 gz
199.81 KB
52.94 gz
+1.32 KB (+0.7%)
+0.33 gz (+0.6%)
🟡 core-line 198.47 KB
51.54 gz
199.77 KB
51.89 gz
+1.30 KB (+0.7%)
+0.35 gz (+0.7%)
🟡 core-scatter 191.18 KB
49.39 gz
192.48 KB
49.73 gz
+1.29 KB (+0.7%)
+0.34 gz (+0.7%)
🟡 core-html 184.91 KB
48.38 gz
186.21 KB
48.72 gz
+1.30 KB (+0.7%)
+0.34 gz (+0.7%)
🟡 core-geo 184.46 KB
48.25 gz
185.76 KB
48.58 gz
+1.30 KB (+0.7%)
+0.33 gz (+0.7%)
🟡 core 182.33 KB
47.34 gz
183.63 KB
47.67 gz
+1.30 KB (+0.7%)
+0.33 gz (+0.7%)
🟡 core-svg 182.33 KB
47.69 gz
183.63 KB
48.03 gz
+1.30 KB (+0.7%)
+0.34 gz (+0.7%)
Base (agnostic) (4 changed)
Scenario Current New Change
🟡 base-svg-agnostic 343.20 KB
80.22 gz
344.57 KB
80.62 gz
+1.37 KB (+0.4%)
+0.40 gz (+0.5%)
🟡 base-canvas-agnostic 343.20 KB
80.21 gz
344.57 KB
80.62 gz
+1.37 KB (+0.4%)
+0.41 gz (+0.5%)
🟡 base-html-agnostic 343.20 KB
80.21 gz
344.57 KB
80.62 gz
+1.37 KB (+0.4%)
+0.41 gz (+0.5%)
🟡 base 343.20 KB
80.22 gz
344.57 KB
80.62 gz
+1.37 KB (+0.4%)
+0.40 gz (+0.5%)
Base (layer-specific) (3 changed)
Scenario Current New Change
🟡 base-canvas 327.97 KB
76.69 gz
329.34 KB
77.13 gz
+1.37 KB (+0.4%)
+0.44 gz (+0.6%)
🟡 base-html 321.87 KB
74.87 gz
323.20 KB
75.25 gz
+1.33 KB (+0.4%)
+0.39 gz (+0.5%)
🟡 base-svg 319.31 KB
74.21 gz
320.65 KB
74.62 gz
+1.34 KB (+0.4%)
+0.40 gz (+0.5%)
Cartesian charts (27 changed)
Scenario Current New Change
🟡 PieChart 388.63 KB
89.96 gz
390.00 KB
90.38 gz
+1.37 KB (+0.4%)
+0.42 gz (+0.5%)
🟡 ArcChart 381.64 KB
88.75 gz
383.01 KB
89.16 gz
+1.38 KB (+0.4%)
+0.41 gz (+0.5%)
🟡 LineChart 374.55 KB
87.50 gz
375.93 KB
87.89 gz
+1.38 KB (+0.4%)
+0.39 gz (+0.5%)
🟡 PieChart-canvas 371.41 KB
86.17 gz
372.78 KB
86.51 gz
+1.38 KB (+0.4%)
+0.33 gz (+0.4%)
🟡 AreaChart 366.87 KB
85.22 gz
368.24 KB
85.68 gz
+1.37 KB (+0.4%)
+0.45 gz (+0.5%)
🟡 ArcChart-canvas 365.00 KB
85.07 gz
366.37 KB
85.45 gz
+1.37 KB (+0.4%)
+0.38 gz (+0.4%)
🟡 PieChart-svg 362.92 KB
83.78 gz
364.27 KB
84.15 gz
+1.34 KB (+0.4%)
+0.37 gz (+0.4%)
🟡 line-chart-interactive 362.87 KB
84.61 gz
364.24 KB
85.00 gz
+1.37 KB (+0.4%)
+0.39 gz (+0.5%)
🟡 area-chart 362.18 KB
84.22 gz
363.55 KB
84.62 gz
+1.37 KB (+0.4%)
+0.40 gz (+0.5%)
🟡 pie-chart 356.73 KB
83.23 gz
358.10 KB
83.61 gz
+1.37 KB (+0.4%)
+0.38 gz (+0.5%)
🟡 ArcChart-svg 356.50 KB
82.54 gz
357.84 KB
82.94 gz
+1.34 KB (+0.4%)
+0.40 gz (+0.5%)
🟡 BarChart 354.60 KB
82.62 gz
355.99 KB
82.99 gz
+1.39 KB (+0.4%)
+0.37 gz (+0.5%)
🟡 AreaChart-canvas 351.00 KB
81.65 gz
352.38 KB
82.06 gz
+1.37 KB (+0.4%)
+0.40 gz (+0.5%)
🟡 bar-chart 348.49 KB
81.29 gz
349.87 KB
81.68 gz
+1.38 KB (+0.4%)
+0.38 gz (+0.5%)
🟡 ScatterChart 348.14 KB
81.30 gz
349.52 KB
81.68 gz
+1.38 KB (+0.4%)
+0.38 gz (+0.5%)
🟡 scatter-chart 344.29 KB
80.50 gz
345.66 KB
80.89 gz
+1.37 KB (+0.4%)
+0.40 gz (+0.5%)
🟡 line-chart 343.70 KB
80.25 gz
345.07 KB
80.66 gz
+1.37 KB (+0.4%)
+0.41 gz (+0.5%)
🟡 AreaChart-svg 342.36 KB
79.13 gz
343.71 KB
79.52 gz
+1.35 KB (+0.4%)
+0.39 gz (+0.5%)
🟡 BarChart-canvas 338.43 KB
78.98 gz
339.80 KB
79.39 gz
+1.37 KB (+0.4%)
+0.40 gz (+0.5%)
🟡 LineChart-canvas 335.03 KB
78.24 gz
336.40 KB
78.68 gz
+1.37 KB (+0.4%)
+0.44 gz (+0.6%)
🟡 ScatterChart-canvas 332.19 KB
77.60 gz
333.55 KB
78.00 gz
+1.37 KB (+0.4%)
+0.40 gz (+0.5%)
🟡 BarChart-svg 329.78 KB
76.44 gz
331.13 KB
76.81 gz
+1.34 KB (+0.4%)
+0.38 gz (+0.5%)
🟡 line-chart-canvas 328.00 KB
76.71 gz
329.36 KB
77.14 gz
+1.37 KB (+0.4%)
+0.44 gz (+0.6%)
🟡 LineChart-svg 326.40 KB
75.79 gz
327.73 KB
76.16 gz
+1.33 KB (+0.4%)
+0.37 gz (+0.5%)
🟡 ScatterChart-svg 323.55 KB
75.10 gz
324.88 KB
75.48 gz
+1.33 KB (+0.4%)
+0.38 gz (+0.5%)
🟡 line-chart-html 321.89 KB
74.88 gz
323.22 KB
75.27 gz
+1.33 KB (+0.4%)
+0.39 gz (+0.5%)
🟡 line-chart-svg 319.34 KB
74.22 gz
320.67 KB
74.63 gz
+1.34 KB (+0.4%)
+0.40 gz (+0.5%)
Geo (3 changed)
Scenario Current New Change
🟡 geo-full 417.88 KB
99.56 gz
419.30 KB
99.97 gz
+1.42 KB (+0.3%)
+0.41 gz (+0.4%)
🟡 geo-tiles 365.45 KB
85.91 gz
366.82 KB
86.31 gz
+1.38 KB (+0.4%)
+0.40 gz (+0.5%)
🟡 geo 360.34 KB
84.22 gz
361.71 KB
84.60 gz
+1.37 KB (+0.4%)
+0.38 gz (+0.5%)
Hierarchy (4 changed)
Scenario Current New Change
🟡 hierarchy-tree 353.98 KB
83.16 gz
355.35 KB
83.57 gz
+1.37 KB (+0.4%)
+0.40 gz (+0.5%)
🟡 hierarchy-treemap 350.87 KB
82.39 gz
352.24 KB
82.79 gz
+1.37 KB (+0.4%)
+0.40 gz (+0.5%)
🟡 hierarchy-pack 350.28 KB
82.25 gz
351.66 KB
82.64 gz
+1.38 KB (+0.4%)
+0.39 gz (+0.5%)
🟡 dodge 348.01 KB
81.67 gz
349.38 KB
82.02 gz
+1.37 KB (+0.4%)
+0.35 gz (+0.4%)
Graph / network (4 changed)
Scenario Current New Change
🟡 dagre 412.80 KB
99.13 gz
414.17 KB
99.58 gz
+1.38 KB (+0.3%)
+0.45 gz (+0.5%)
🟡 force 356.51 KB
83.98 gz
357.88 KB
84.37 gz
+1.37 KB (+0.4%)
+0.39 gz (+0.5%)
🟡 sankey 356.18 KB
83.40 gz
357.56 KB
83.78 gz
+1.37 KB (+0.4%)
+0.39 gz (+0.5%)
🟡 chord 352.87 KB
82.46 gz
354.24 KB
82.85 gz
+1.37 KB (+0.4%)
+0.39 gz (+0.5%)
Components (2 changed)
Scenario Current New Change
🟡 Tooltip 114.54 KB
28.52 gz
114.66 KB
28.55 gz
+0.13 KB (+0.1%)
+0.02 gz (+0.1%)
🟡 Layer 77.55 KB
19.37 gz
77.67 KB
19.40 gz
+0.13 KB (+0.2%)
+0.03 gz (+0.1%)
Worst case (1 changed)
Scenario Current New Change
🟡 all 1101.45 KB
261.64 gz
1102.81 KB
262.03 gz
+1.36 KB (+0.1%)
+0.39 gz (+0.1%)

Understanding this report
  • Use-case scenarios measure the bundle cost of common chart configurations (e.g. a line chart with axes)
  • Individual components measure each component imported in isolation
  • Svelte runtime is excluded; sizes reflect layerchart + its dependencies (d3, etc.)
  • When multiple components share dependencies (e.g. d3-scale), the real-world cost is lower than the sum of individual sizes
  • Changes smaller than 10 bytes or 0.1% are considered insignificant

@github-actions

Copy link
Copy Markdown
Contributor

Preview deployment

https://pr-996-layerchart.techniq.workers.dev

Built from 9c8591c.

This branch was successfully deployed

1 active (outdated) deployment
Preview — 2f41ff89 Deployed Nov 13, 2026 by github-actions[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add <Chart lazy> to defer creation/rendering

1 participant