Skip to content

fix: give usePlot().scales.fx/fy valid ranges for facet-aware positioning#500

Merged
gka merged 2 commits intomainfrom
fix/fx-fy-scale-range
Mar 2, 2026
Merged

fix: give usePlot().scales.fx/fy valid ranges for facet-aware positioning#500
gka merged 2 commits intomainfrom
fix/fx-fy-scale-range

Conversation

@ljodea
Copy link
Copy Markdown
Collaborator

@ljodea ljodea commented Feb 27, 2026

Summary

  • usePlot().scales.fx/fy had empty ranges ([]), making fn() calls return NaN — preventing custom overlay components from using fx/fy scales for facet-aware positioning (labels, tooltips, annotations)
  • After computeScales() returns in computePlotState(), create fresh scaleBand instances for fx/fy with correct range ([0, plotWidth] / [0, plotHeight]) and padding matching FacetGrid's layout
  • Fresh band scales are needed because d3 band scales created with range([]) have corrupted internal ordinal mappings that can't be recovered by later setting the range

Closes #498

Test plan

  • 8 new test cases covering range validity, position correctness, bandwidth, DOM position matching, custom padding, and no-faceting edge case
  • Existing facet tests pass (facet-padding.test.ts, dot-faceted.test.svelte.ts)
  • Full test suite passes (685 tests)
  • svelte-check passes (0 errors)

🤖 Generated with Claude Code

…ning

computeScales() creates fx/fy band scales with empty ranges because
getScaleRange() has no case for these scale names. This made fn() calls
return NaN, preventing custom components from using fx/fy scales for
facet-aware positioning (labels, tooltips, annotations).

After computeScales() returns, create fresh band scales for fx/fy with
the correct range ([0, plotWidth] / [0, plotHeight]) and padding config
matching FacetGrid's layout. Fresh scales are needed because d3 band
scales created with range([]) have corrupted internal ordinal mappings.

Closes #498

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@netlify
Copy link
Copy Markdown

netlify bot commented Feb 27, 2026

Deploy Preview for svelteplot ready!

Name Link
🔨 Latest commit 5c0bd35
🔍 Latest deploy log https://app.netlify.com/projects/svelteplot/deploys/69a5dc7dd2c30700084ce9a1
😎 Deploy Preview https://deploy-preview-500--svelteplot.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@gka gka merged commit e59c76a into main Mar 2, 2026
8 checks passed
gka added a commit that referenced this pull request Mar 2, 2026
## Summary

Stacked on #500.

- **Pointer searched ALL quadtrees** instead of only the hovered facet's
trees — replaced flat `trees[]` array with a keyed `SvelteMap<facetKey,
Quadtree[]>` so only the hovered facet's trees are searched
- **HTMLTooltip positioned via bounding rect diffs** — replaced fragile
`getBoundingClientRect()` offset computation with scale-based offsets
using `plot.scales.fx.fn(fxValue)` (available after #500)
- **Tree array index ≠ facet index** — `groupFacetsAndZ` produces groups
in data-encounter order while `FacetGrid` iterates domain order; keyed
map decouples lookup from array ordering
- **HTMLTooltip event listener leak** — cleanup called
`removeEventListener('mouseleave')` but registration used
`addEventListener('pointerleave')`; both now use `pointerleave`

### New shared helpers (`src/lib/helpers/facets.ts`)

| Helper | Purpose |
|--------|---------|
| `facetKey(fx, fy)` | Stable `JSON.stringify` key for `(fxValue,
fyValue)` pairs |
| `invertBand(scale, domain, pixel)` | Invert a `d3.scaleBand` (no
native `.invert()`) |
| `findFacetFromDOM(target)` | Walk DOM up to `g.facet`, read
`data-facet-x/y` attributes |
| `detectFacet(evt, plot)` | DOM walk first, band-inversion fallback;
returns `{ fxValue, fyValue, offsetX, offsetY }` |

## Test plan

- [x] `invertBand` — 5 unit tests (first/second band, out of range,
single domain, padding)
- [x] `facetKey` — 6 unit tests (same values, different values, null,
boolean, null vs "null", numeric)
- [x] Faceted Pointer — 4 tests (facet A isolation, facet B isolation,
non-faceted regression, empty data)
- [x] HTMLTooltip — 4 tests (facet A datum, facet B datum, non-faceted
regression, pointerleave)
- [x] All 704 existing tests pass
- [x] `pnpm check` — 0 errors
- [x] `pnpm lint` — clean

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: gka <617518+gka@users.noreply.github.com>
Co-authored-by: Gregor Aisch <gka@users.noreply.github.com>
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.

usePlot().scales.fx.fn has empty range — facet scale not usable from overlay/custom components

2 participants