fix: preserve z-indexed subplots during relayout for correct pan/zoom#7659
Open
ryan-williams wants to merge 2 commits intoplotly:masterfrom
Open
fix: preserve z-indexed subplots during relayout for correct pan/zoom#7659ryan-williams wants to merge 2 commits intoplotly:masterfrom
relayout for correct pan/zoom#7659ryan-williams wants to merge 2 commits intoplotly:masterfrom
Conversation
When traces have different `zorder` values, plotly.js creates z-indexed subplots (e.g., `xyz2`, `xyz3`) in `drawFramework`. However, when `relayout` is called (e.g., during resize), `supplyDefaults` resets `_plots` via `linkSubplots`, losing these z-indexed subplots. Since `relayout` doesn't trigger `drawFramework`, they aren't recreated. This caused pan/zoom to fail on the first attempt in react-plotly.js, because `updateSubplots` in `dragbox.js` couldn't find the z-indexed subplots to transform. Fix: 1. In `linkSubplots`, preserve z-indexed subplots from `oldSubplots` 2. In `updateSubplots`, include z-indexed subplots from `_plots` 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
eefb961 to
18f5dc7
Compare
camdecoster
requested changes
Apr 15, 2026
Comment on lines
+899
to
+910
| // Preserve z-indexed subplots (e.g. 'xyz2', 'xyz3') from oldSubplots to _plots only. | ||
| // These are created by drawFramework when traces have different zorder values. | ||
| // We preserve them to _plots (not _subplots.cartesian) so that drag/pan operations | ||
| // in dragbox.js can still transform them during relayout operations that don't | ||
| // trigger a full redraw (e.g., resize). If drawFramework runs later, it will | ||
| // properly update/remove these subplots as needed. | ||
| var zindexSeparator = cartesianConstants.zindexSeparator; | ||
| for(var oldId in oldSubplots) { | ||
| if(oldId.indexOf(zindexSeparator) !== -1 && !newSubplots[oldId]) { | ||
| newSubplots[oldId] = oldSubplots[oldId]; | ||
| } | ||
| } |
Contributor
There was a problem hiding this comment.
I prodded Claude to dig into this and this is the resulting solution. Updating this one file should take care of the issue without updating dragbox.js. Could you give this change a shot and let me know if it fixes your problem?
Suggested change
| // Preserve z-indexed subplots (e.g. 'xyz2', 'xyz3') from oldSubplots to _plots only. | |
| // These are created by drawFramework when traces have different zorder values. | |
| // We preserve them to _plots (not _subplots.cartesian) so that drag/pan operations | |
| // in dragbox.js can still transform them during relayout operations that don't | |
| // trigger a full redraw (e.g., resize). If drawFramework runs later, it will | |
| // properly update/remove these subplots as needed. | |
| var zindexSeparator = cartesianConstants.zindexSeparator; | |
| for(var oldId in oldSubplots) { | |
| if(oldId.indexOf(zindexSeparator) !== -1 && !newSubplots[oldId]) { | |
| newSubplots[oldId] = oldSubplots[oldId]; | |
| } | |
| } | |
| // Preserve z-indexed subplots (e.g. 'xyz2', 'xyz3') created by drawFramework. | |
| // These aren't in newSubplotList.cartesian because supplyTraceDefaults only | |
| // registers base subplot IDs. We restore them from oldSubplots so they | |
| // survive relayout cycles that don't trigger drawFramework. | |
| for (const [oldId, oldPlot] of Object.entries(oldSubplots)) { | |
| if (oldId.includes(zindexSeparator) && !newSubplots[oldId]) { | |
| const baseId = oldId.split(zindexSeparator)[0]; | |
| const basePlotinfo = newSubplots[baseId]; | |
| if (basePlotinfo) { | |
| // Update axis refs to avoid stale references | |
| oldPlot.xaxis = basePlotinfo.xaxis; | |
| oldPlot.yaxis = basePlotinfo.yaxis; | |
| oldPlot._hasClipOnAxisFalse = basePlotinfo._hasClipOnAxisFalse; | |
| newSubplots[oldId] = oldPlot; | |
| newSubplotList.cartesian.push(oldId); | |
| } | |
| } | |
| } |
| var BADNUM = require('../constants/numerical').BADNUM; | ||
|
|
||
| var axisIDs = require('./cartesian/axis_ids'); | ||
| var cartesianConstants = require('./cartesian/constants'); |
Contributor
There was a problem hiding this comment.
Suggested change
| var cartesianConstants = require('./cartesian/constants'); | |
| const { zindexSeparator } = require('./cartesian/constants'); |
Contributor
There was a problem hiding this comment.
The file can remain unchanged if you follow the suggestion in plots.js.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a bug where pan/zoom fails on the first interaction when traces have different
zordervalues. On first drag, one trace moves with the grid while the other stays frozen in place. The second pan (and all subsequent pans) work correctly — both traces move together as expected. This is particularly noticeable in react-plotly.js, which callsPlotly.Plots.resize()after mount.Root cause: When
relayoutis called (e.g., during resize),supplyDefaultsresets_plotsvialinkSubplots, losing z-indexed subplots (e.g.,xyz2,xyz3). Sincerelayoutdoesn't triggerdrawFramework, they aren't recreated, causingupdateSubplotsindragbox.jsto miss them during pan/zoom.Fix:
linkSubplots(plots.js): preserve z-indexed subplots fromoldSubplotsupdateSubplots(dragbox.js): include z-indexed subplots from_plotsDemo
bug.mp4
Live before/after comparison: https://runsascoded.github.io/plotly.js/
To reproduce the bug:
Test plan
cartesian_interacttests pass🤖 Generated with Claude Code