fix: correct tooltip coordinate mapping when chart is zoomed + fix pa…#649
Conversation
…n/zoom dead zones
🦋 Changeset detectedLatest commit: 8e5bf11 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
@wvanooijen92 is attempting to deploy a commit to the formidable-labs Team on Vercel. A member of the Team first needs to authorize it. |
There was a problem hiding this comment.
Pull request overview
Fixes gesture “dead zones” after pan/zoom and corrects tooltip coordinate-to-data mapping when the chart is zoomed, by decoupling the gesture hit-area from the chart transform and properly inverting pan/zoom when converting touch coordinates.
Changes:
- Make
GestureHandlerfill its parent container instead of following the chart’s transform matrix. - Fix
CartesianCharttouch-to-canvas mapping under zoom by usingtouch.x/touch.yand dividing by the current scale. - Update the pie/donut example to demonstrate PolarChart pan/zoom + add a patch changeset.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
lib/src/shared/GestureHandler.tsx |
Removes transform-following layout; pins gesture hit area to parent bounds. |
lib/src/polar/PolarChart.tsx |
Updates GestureHandler usage to match the new API (no dimensions). |
lib/src/cartesian/CartesianChart.tsx |
Adjusts touch coordinate mapping to account for zoom scale and view-relative coordinates. |
example/app/pie-and-donut-charts.tsx |
Adds an example donut chart demonstrating pan/zoom with a reset button. |
.changeset/fix-gesture-dead-zones-zoom-tooltip.md |
Adds a patch changeset describing the fixes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Fix: gesture dead zones + wrong tooltip position when chart is zoomed
Fixes #515 (partially) and a related coordinate mapping bug.
Description
This PR fixes two separate but connected bugs that both surface when
transformState(pan/zoom) is active alongsidechartPressState.Bug 1; Dead zones after panning or zooming
GestureHandlerwas applying the chart's transform matrix to its own React Native view style, so the gesture hit area physically moved along with the chart content. After panning far enough, the hit area drifted off-screen or into a region the user couldn't reach, making pan/zoom gestures silently fail. The fix is straightforward: the handler fills its entire parent container instead of following the transform.CartesianChart'shandleTouchalready reads the current pan offset out of the matrix and compensates, so tooltip accuracy is not affected by this change.Bug 2; Tooltip snaps to the wrong data point when zoomed
handleTouchwas computing the canvas-space touch position as:Subtracting the translate cancels the pan, but zoom scale is never factored out. At
scaleX = 2, a finger in the centre of the screen produces a value roughly twice the correct canvas coordinate, which consistently maps to a data point near the far end of the range; the chart appears to "jump to max X" every time you lift and re-place your finger.The fix uses view-relative
touch.x/touch.y(which already accounts for where the container sits on screen) and divides by the current scale:This works for all combinations of pan and zoom. When no transform is applied (
scaleX = 1,translateX = 0) the formula reduces totouch.x, which is equivalent to the old behaviour.The two fixes are coupled: switching to
touch.xis only correct onceGestureHandleris stationary (Bug 1 fix), becausetouch.xis relative to the handler view itself.Type of Change
How Has This Been Tested?
Tested manually on android with a
CartesianChartusingchartPressStateandtransformStatetogether:Checklist
yarn run check:codeand all checks pass