Skip to content

Discard cartesian pan and zoombox drags on touchcancel - #8221

Open
dominikvrbic wants to merge 16 commits into
plotly:mainfrom
dominikvrbic:fix-cartesian-drag-cancel
Open

dominikvrbic wants to merge 16 commits into
plotly:mainfrom
dominikvrbic:fix-cartesian-drag-cancel

Conversation

@dominikvrbic

@dominikvrbic dominikvrbic commented Dec 1, 2026 •

Copy link
Copy Markdown

Fixes #8220

This change makes Cartesian drags handle touchcancel. In version 4.3.1, the plot keeps the drag preview when the browser cancels the touch. The fix covers pan, box zoom, axis-end drags and axis-pan drags.

Reproduction

The red lines cross at (5, 5). After a 150 px pan, Chromium cancels the touch. A double tap then resets the axes. Standalone reproduction

Left: 4.3.1. Right: this branch.

Pan: in 4.3.1, the plot stays 150 px from its axes after the cancel and a double tap. On this branch, the marker at (5, 5) goes back to the crosshair.

Box zoom: in 4.3.1, the zoom box stays on the plot and the drag stays active after the cancel. On this branch, the zoom box goes away.

Behavior

A touch drag now belongs to the touch that started it. Other touches do not move, end or cancel the drag. A mouse drag ends only on mouseup. While a drag is active in a plot, a second touch or another mouse button does not start a new drag.

If the browser cancels the touch that started a drag, dragElement removes the document listeners of the drag and clears the drag state. It does not call doneFn, so the cancel commits no edit. A component that supplies cancelFn gets a call to cancelFn. A component without cancelFn keeps its preview on the screen, but its next drag starts with no old state.

Sometimes the browser does not deliver the end event of a drag, for example when the window loses focus. In that case, a new touch cancels the old touch drag and starts a new one. This also applies when the browser gives the new touch the identifier of the old touch. On a device without hover, the next press of the same mouse button does the same. The new drag measures from its own start point. On a device with hover, the drag cover gets the next mouse press, so the behavior stays the same as on main.

Plotly.purge and Plotly.newPlot end an active drag before they clear the plot. Before this change, a legend drag kept its document listeners after Plotly.purge, and the next touchmove threw an error.

Pan, axis-pan and axis-end drags

  • While the touch moves, each move emits plotly_relayouting with the preview ranges, as before.
  • On a cancel after movement, the drag handler restores the ranges, ticks, components and trace positions. It does not call Plotly.redraw. Thus a plotly_beforeplot handler cannot keep the preview.
  • Constrained axes (scaleanchor with constrain: 'domain' or 'range') get their original ranges back. The drag does not change gd.layout at any time.
  • After the restore, the plot emits one final plotly_relayouting. It is the last event of the gesture.
  • If a transition with a duration runs at the time of the cancel, the restore and the final event wait for plotly_transitioned.

Box zoom

  • While the touch moves, each move emits plotly_relayouting, as before. This includes the empty payloads that zoom emits now.
  • On a cancel after movement, the plot removes the zoom box and its corners at once. Then the plot emits one final plotly_relayouting, after any running transition ends.

Final event, for pan and for zoom

  • The payload has .range[0] and .range[1] for every axis that a nonempty preview reported. Thus, if the zoom box changed from xy to y only, x is in the payload too.
  • The payload shows the ranges on the screen after the cancel. Usually these are the ranges from before the touch.
  • If an application update or an animation changed the ranges during the drag, the payload has the new ranges.
  • If the drag emitted no nonempty preview, the cancel emits no final event. For example, a pan emits no preview while a transition runs.

All cancels

  • A cancel emits no plotly_relayout, plotly_click, plotly_doubleclick, plotly_beforeplot, plotly_afterplot or plotly_redraw.
  • A cancel without movement emits no event, and the next tap counts as a first tap.
  • If Plotly.react removes the subplot of an active drag, the drag ends. On main, the drag stays active, and later moves emit plotly_relayouting for axes that no longer exist.
  • If an application sets fixedrange on the two axes during a pan, the cancel or the release still removes the pan transform.
  • If an application calls relayout, react or a resize during the drag, the cancel gives the same result as the same update without a drag.

Callers of dragElement.init

Annotations, colorbar, legend, shapes with shape outlines, the Cartesian dragbox with select, lasso and draw, geo, map, polar, ternary and sankey. Only the Cartesian dragbox supplies cancelFn.

Tests

New Jasmine specs in cartesian_interact_test.js and dragelement_test.js cover these cases:

  • The final event order and payload for pan and for zoom, and no events for a cancel without movement
  • The point position on the screen after a cancel. The old assertion measured the offset from the axes, which is 0 on main too. The new assertion compares the client position before and after the cancel (+100 px on main).
  • Axis-end drags with scaleanchor and constrain: 'domain' or 'range', and axis pans on the ew and ns draggers
  • A second touch on the same dragger, a second mouse button, and a foreign touch cancel during a mouse drag
  • A lost mouseup, a lost touchend, a reused touch identifier, and a Cartesian pan after a lost mouseup
  • A plotly_beforeplot handler that returns false
  • Inside tick labels with autorange
  • A cancelled legend drag, then a cancelled pan, then a full pan. The full pan commits its ranges, and the legend stays in its position.
  • Plotly.react that removes the subplot of an active pan or zoom
  • Plotly.purge during a legend drag and during a pan, with touch and with a mouse
  • fixedrange on the two axes during a pan, for touchcancel and touchend, with the clip and plot transforms
  • A cancel without movement when the two axes have fixedrange
  • relayout, react and a resize during the drag, compared with the same update without a drag
  • A trace animation, and pan and zoom cancels during and after an axis-range animation
  • A pan that moved only during a transition

The specs wait for the events, not for a fixed delay. destroyGraphDiv releases a drag that a failed spec leaves active. Without this release, the drag can affect the next spec.

I ran cartesian_interact, dragelement and plot_promise together in Chromium 159 headless shell:

Source Passed Failed
main at 6c1837503 120 1
This branch 160 1

The branch adds 40 specs. The one failure is the same on the two sources. It is the multicategory tick-label span spec (438.3 px against 430 ± 5.5 px), and it depends on the local fonts.

I also ran each new spec alone against the old source with the new test files. 39 of the 40 specs fail on main. The spec that passes on main makes sure that a touchcancel does not end a mouse drag. The first version of this PR broke that behavior.

36 of the 40 specs fail on the first version of this PR (b6f50be85). Four specs pass there: its own two dragElement specs, the unmoved zoom with interactive axes, and the pan that moved only during a transition.

CI checks run locally

I ran the CI jobs on main and on this branch, with CI=1 and TZ=America/Anchorage, Node 22 and Chromium 159 headless shell.

Job main This branch
lint, typecheck, test-syntax, schema-typegen-diff-check, entry-point-types-check, test-types, test-plain-obj, test-mock pass pass
No-WebGL Jasmine, 4 shards 6620 run, 17 failed 6659 run, 18 failed
WebGL Jasmine, 8 shards pass pass
Virtual WebGL Jasmine, 8 shards pass pass
Hover in UTC, Europe/Berlin, Asia/Tokyo and America/Toronto 235 passed in each 235 passed in each
noCI and noCIdep 94 passed 94 passed

The shard split uses file sizes, so the new specs move some files to other shards. All 17 failures on main also fail on the branch. They come from the local environment, for example fonts, automargin titles and uniformtext.

The extra failure on the branch is "should be able to relayout imaginary axis ticks" in smith_test. It also fails on main when it runs with the same shard files. That shard has the same 6 failures on the two sources, and the spec uses no drag.

I did not run test-bundle, the flaky shard or the image tests. The CI run used 3df6b6585. The later commits change only the draftlog and the touch identifier check in dragElement. On those commits, I ran the three suites above and test-syntax again.

Follow-ups

  • Restore the preview on cancel for the other drag components: shapes, annotations, legend, colorbar, polar, ternary, geo, map, rangeslider, sliders and parcoords. The components that use dragElement now release the gesture on cancel, but a moved preview, such as a legend position, stays on the screen. Rangeslider, sliders and parcoords have their own drag handlers, and this PR does not change them.
  • On a device with hover, the drag cover gets the next mouse press after a lost mouseup, so the old drag continues. main has the same behavior.

🐦 An agent wrote the initial patch.

dragElement listened for touchend but not for touchcancel. When the
browser cancelled a touch drag, the pan preview and the zoombox stayed
on screen, and gd._dragdata kept the drag offset. A later redraw then
replayed the drag, so the traces sat away from the axis ranges.

dragElement now accepts a cancelFn. On touchcancel it ends the drag and
calls cancelFn instead of doneFn or clickFn. The cartesian dragbox
removes the zoombox, and after a pan it redraws from gd.layout, which
still holds the ranges from before the drag.
Cancel the active drag when its touch ends with touchcancel. Release the listeners even when a component has no cancel callback.
Keep preview ranges separate from input ranges. Restore the ranges and transforms on cancellation and report the original displayed ranges in the final preview event.
The transition rebuilds the axes from the layout. Report the ranges that it shows, not the ranges from before the pan.
The cancel restores the ranges, the points and the shapes without a plot call.
A failed spec can leave a drag active. Release it in destroyGraphDiv so that it does not affect the next spec in any suite.
The marker position alone does not show the translated clip path.
Pan and zoom use one path for the final plotly_relayouting. It runs after a transition ends and reports the ranges on the screen, also when an animation changed them during the drag. A drag without a nonempty preview emits no final event.
A new press of the same mouse button, or a touch without the owning contact, cancels the active drag and starts a new one. The new drag measures from its own start. A Cartesian drag keeps its restore state for mouse input too, so that the cancel restores its preview.
Before, a legend drag kept its document listeners and drag cover after a purge, and the next touchmove threw an error.
…e old one

After a lost touchend, Chromium can give the next contact the same identifier. A touchstart that holds the owner identifier in changedTouches starts a new contact.
dominikvrbic added a commit to Jan-IngenHousz-Institute/open-jii that referenced this pull request Dec 2, 2026
## Type of PR (check all applicable)

- [x] Feature
- [x] Bug Fix
- [ ] Refactor
- [x] Documentation Update
- [ ] Performance Improvement
- [x] Test Update
- [ ] Chore
- [ ] Other (please describe)

## Description

A reference line can now be **Sloped** as well as horizontal or
vertical. A sloped line follows y = slope × x + intercept in the chart's
own units, such as the 1:1 line that readings from two methods should
agree on. It has the same color, dash and label settings as the other
lines.

- Scatter, line, bubble and area charts offer Sloped. Box, violin and
histogram don't.
- A new sloped line starts at slope 1 and intercept 0, keeping any style
already set on the row.
- The line runs from edge to edge of every facet it crosses. Its label
appears in the first facet it crosses. The data still decides the axis
ranges.
- While pan or zoom is in progress, the sloped line and label are
hidden. They redraw at the resulting ranges when the interaction
completes.
- On a logarithmic axis the line follows its equation, so it can curve,
and the parts at or below zero are left out.
- When either axis shows dates or categories, Sloped is disabled. A
saved sloped line is hidden, and the numeric-axes note appears only if
the chart has a sloped line.
- Slope and intercept must both be finite numbers. If either field is
empty or invalid, the editor shows a field error and pauses autosave.
- Saved horizontal and vertical lines draw exactly as before.

## Linear issues

Closes OJD-2316

## GitHub issues

- plotly/plotly.js#8220 (upstream bug)

## Testing Instructions

Environment: dev web, with edit access to an experiment whose table has
two positive number columns, a date column and a group column. This
touches Critical Flows tier 2.

- [ ] On a scatter of the two number columns, add a Sloped reference
line. Expected: a 1:1 line appears and the ticks stay put.
- [ ] Enter another slope and intercept. Expected: the line follows that
equation from edge to edge.
- [ ] Clear either coefficient, then enter 0. Expected: a field error
appears and autosave pauses. Entering 0 clears the error and autosave
resumes.
- [ ] Pan or zoom the chart. Expected: the sloped line and label hide
during the gesture and reappear aligned with the data when it completes.
- [ ] Set a color, a dash and a label. Expected: the line shows all
three.
- [ ] Split the chart into panels by the group column. Expected: the
line appears in every panel it crosses, with its label in the first
panel it crosses.
- [ ] Set the Y axis to Log with a negative intercept. Expected: the
line curves and stops where its values reach zero.
- [ ] Put the date column on X. Expected: Sloped is disabled, the saved
sloped line is hidden, and the note appears. Remove the sloped line.
Expected: the note disappears.
- [ ] Open the Reference lines section on Line, Bubble, Area and
Histogram charts. Expected: Sloped is offered on the first three only.
- [ ] Reload a chart with saved horizontal and vertical lines. Expected:
each line stays at its saved value.

## Changes Made

- `packages/ui` `charts/types.ts`: the reference line type gains a
`sloped` variant with `slope` and `intercept`.
- `packages/ui` `charts/sloped-reference-lines.ts` (new) projects each
sloped line into every panel from the ranges Plotly has already worked
out, in panel coordinates. Plotly counts shapes drawn in data units when
it sizes an axis, and these don't. On a log axis it samples the
equation. `cartesian-chart.tsx` and `plotly-chart.tsx` redraw the lines
after Plotly lays the chart out.
- `png-export.ts` keeps the on-screen ranges in PNG, SVG, JPEG and WebP
downloads, so an exported line matches the screen.
- The Reference lines row and section add the Sloped choice with Slope
and Intercept inputs. A small axis-preview context tells the editor
whether the drawn axes are numeric.
- The four cartesian style shelves pass the chart type through, so only
scatter, line, bubble and area offer Sloped.
- Labels in en-US, de-DE and nl-NL, including the section's empty-state
text, which no longer says lines are only vertical or horizontal. The
guide in `apps/docs/content/guide/data-analysis/viewing-data.mdx`
explains sloped lines.

The create and update schemas reject incomplete sloped lines and require
finite numeric coefficients. The read schema still accepts older saved
charts with incomplete lines so users can open and repair them. No
database migration is needed.

## Checklist

- [x] I have added/updated tests for my changes
- [x] My code follows the project's style guidelines
- [x] I have updated the documentation accordingly
- [x] I have tested these changes locally
- [x] My changes generate no new warnings
- [x] I have reviewed my own code
- [x] My PR title and Linear relation lines follow the release metadata
contract
- [x] The Linear ticket carries `## How it was built` and `## Testing
criteria`

## Screenshots/Recordings

The chart editor on the local stack, with the data served from a fixture
table of made-up field and reference sensor readings. Before this
change, a reference line could only be horizontal or vertical.

| A dashed 1:1 line on a method comparison | The same saved line on a
date X axis |
| --- | --- |
| <img width="560" alt="Scatter of a field sensor against a reference
sensor with a dashed grey line labeled 1:1 running edge to edge, and the
Reference lines section showing Axis Sloped, Slope 1, Intercept 0,
Dashed and the label 1:1"
src="https://public.linear.app/4d34d487-d352-45fe-89b9-feddf0310761/7ff07986-a5bd-4802-9cb7-f2bf869188cd/67f3dfd1-a9f6-4f28-9b0d-50acee9f7c4c"
/> | <img width="560" alt="Scatter of the field sensor over time with no
reference line drawn, and the Reference lines section showing the note
Sloped lines need numeric X and Y axes"
src="https://public.linear.app/4d34d487-d352-45fe-89b9-feddf0310761/66b9c490-5568-46ea-be3d-b0fb3f858a74/bf65f9e5-5cd5-4680-a0c9-d00250840055"
/> |

## Additional Notes

**Known issue.** If the browser cancels a touch pan and the user then
double-taps, Plotly 3.7.0 leaves the traces offset from the axes, so the
line looks misplaced until the next pan or Reset axes. This is a Plotly
bug, reported as plotly/plotly.js#8220 with a fix in
plotly/plotly.js#8221.

Verified: 430 tests in `packages/ui` and 133 in web, covering unchanged
autorange, zoom and reset, panel geometry, mixed log axes, hiding on
category axes, and an SVG download from a resized chart. Typechecks for
web, `@repo/ui`, docs and `@repo/i18n` pass, as do ESLint and Prettier
on the changed files and the docs build with its link and media checks.
A separate review took two rounds. The first round found that the slope
inputs weren't offered for numeric text columns and that a resized
non-PNG export lost alignment. Both are fixed in 0caf8d0.

**Merge order.** This PR and the Export figure PR for OJD-1795 both
change `packages/ui/src/components/charts/png-export.ts` and
`plotly-chart.tsx`, so whichever lands second needs a rebase.

<!-- codesmith:footer -->
---
<a
href="https://dashboardbackend.blacksmith.sh/track/run-proof?expires=1798734069&installation_model_id=427137&pr_number=2571&ref=codesmith_pr_footer&repository=Jan-IngenHousz-Institute%2Fopen-jii&return_to=https%3A%2F%2Fgithub.com%2FJan-IngenHousz-Institute%2Fopen-jii%2Fpull%2F2571&variant=proof_this_pr&signature=90561a186a4cebcd80992cc673f9dd3981d8167b4bf76efdabb2b403ca5a1b0c"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://pr-comments-assets.blacksmith.sh/codesmith/proof-proof-this-pr-v2-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://pr-comments-assets.blacksmith.sh/codesmith/proof-proof-this-pr-v2-light.svg"><img
alt="Proof this PR"
src="https://pr-comments-assets.blacksmith.sh/codesmith/proof-proof-this-pr-v2-dark-3x.png"
width="121" height="28"></picture></a>
<sup><a
href="https://app.blacksmith.sh/Jan-IngenHousz-Institute/codesmith/open-jii/pr/2571?autoLogin=true&ref=codesmith_pr_footer">Open
in [code]smith</a> · Need help on this PR? Tag
<code>@codesmith-bot</code> with what you need.</sup>

<!-- codesmith:autofix:disabled -->
<!-- /codesmith:footer -->

This branch has not been deployed

No deployments
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.

[BUG]: Cartesian pan keeps its preview after touchcancel, so traces sit away from the axes

1 participant