From 5ecf2f528bc2969001968f0954d94e4c5bdc2377 Mon Sep 17 00:00:00 2001 From: akysil Date: Mon, 30 Nov 2026 22:29:14 +0000 Subject: [PATCH] Reset private keys that react, relayout and restyle carried from the previous figure Plots.supplyDefaults copies private keys of the previous figure into the new one. Keys that the defaults step sets only under a condition then stay in the next figure. Set _hasDayOfWeekBreaks on every date axis. Remove _colorAx when a container has no coloraxis. Clear _insideTickLabelsUpdaterange in supplyDefaults. Relink a trace only when the trace type is the same. --- draftlogs/XXXX_fix.md | 1 + .../colorscale/cross_trace_defaults.js | 3 ++ src/plots/cartesian/axis_defaults.js | 3 ++ src/plots/plots.js | 8 ++- test/jasmine/tests/axes_test.js | 49 +++++++++++++++++++ test/jasmine/tests/carpet_test.js | 29 +++++++++++ test/jasmine/tests/colorscale_test.js | 30 ++++++++++++ 7 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 draftlogs/XXXX_fix.md diff --git a/draftlogs/XXXX_fix.md b/draftlogs/XXXX_fix.md new file mode 100644 index 00000000000..f887839e465 --- /dev/null +++ b/draftlogs/XXXX_fix.md @@ -0,0 +1 @@ +- Fix values of the previous figure that stay in `Plotly.react`, `Plotly.relayout` and `Plotly.restyle` updates [[#XXXX](https://github.com/plotly/plotly.js/pull/XXXX)], with thanks to @akysil for the contribution! diff --git a/src/components/colorscale/cross_trace_defaults.js b/src/components/colorscale/cross_trace_defaults.js index 34124b9b557..c83a94d9049 100644 --- a/src/components/colorscale/cross_trace_defaults.js +++ b/src/components/colorscale/cross_trace_defaults.js @@ -22,6 +22,9 @@ module.exports = function crossTraceDefaults(fullData, fullLayout) { // stash ref to color axis cont._colorAx = fullLayout[cont.coloraxis]; } else { + // remove the color axis that relinkPrivateKeys copied from the previous figure + delete cont._colorAx; + var cOpts = extractOpts(cont); var isAuto = cOpts.auto; diff --git a/src/plots/cartesian/axis_defaults.js b/src/plots/cartesian/axis_defaults.js index 1c390bc948d..9ea7f58ab29 100644 --- a/src/plots/cartesian/axis_defaults.js +++ b/src/plots/cartesian/axis_defaults.js @@ -196,6 +196,9 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce, handleItemDefaults: rangebreaksDefaults }); + // reset the flag here, so that relinkPrivateKeys does not keep the value of the previous figure + containerOut._hasDayOfWeekBreaks = false; + if(!containerOut.rangebreaks.length) { delete containerOut.rangebreaks; } else { diff --git a/src/plots/plots.js b/src/plots/plots.js index 9b1247f8800..203dc1b63b2 100644 --- a/src/plots/plots.js +++ b/src/plots/plots.js @@ -306,6 +306,9 @@ plots.supplyDefaults = function(gd, opts) { // eg set `_requestRangeslider.x2 = true` for xaxis2 newFullLayout._requestRangeslider = {}; + // range updates for inside tick labels, set by Axes.draw for the axes of this figure only + newFullLayout._insideTickLabelsUpdaterange = undefined; + // pull uids from old data to use as new defaults newFullLayout._traceUids = getTraceUids(oldFullData, newData); @@ -335,7 +338,10 @@ plots.supplyDefaults = function(gd, opts) { if(oldFullData.length === newFullData.length) { for(i = 0; i < newFullData.length; i++) { - relinkPrivateKeys(newFullData[i], oldFullData[i]); + // private keys of another trace type do not apply to the new trace + if(newFullData[i].type === oldFullData[i].type) { + relinkPrivateKeys(newFullData[i], oldFullData[i]); + } } } diff --git a/test/jasmine/tests/axes_test.js b/test/jasmine/tests/axes_test.js index e4c85ccd689..8fe7ac9e535 100644 --- a/test/jasmine/tests/axes_test.js +++ b/test/jasmine/tests/axes_test.js @@ -8539,6 +8539,55 @@ describe('more react tests', function() { expect(gd._fullLayout.xaxis.range).toBeCloseToArray([-0.173, 2]); }).then(done, done.fail); }); + + it('should not keep the day-of-week tick steps after rangebreaks are removed', async () => { + const x = []; + const y = []; + let t = Date.UTC(2024, 0, 1); + while(x.length < 15) { + const weekday = new Date(t).getUTCDay(); + if(weekday !== 0 && weekday !== 6) { + x.push(new Date(t).toISOString().slice(0, 10)); + y.push(x.length); + } + t += ONEDAY; + } + const data = () => [{x, y}]; + const layout = () => ({width: 700, height: 350}); + const layoutWithBreaks = () => ({...layout(), xaxis: {rangebreaks: [{bounds: ['sat', 'mon']}]}}); + + await Plotly.newPlot(gd, data(), layout()); + const newPlotDtick = gd._fullLayout.xaxis.dtick; + expect(newPlotDtick).toBeLessThan(ONEWEEK); + + await Plotly.newPlot(gd, data(), layoutWithBreaks()); + await Plotly.relayout(gd, 'xaxis.rangebreaks', null); + expect(gd._fullLayout.xaxis._hasDayOfWeekBreaks).withContext('relayout').toBe(false); + expect(gd._fullLayout.xaxis.dtick).withContext('relayout').toBe(newPlotDtick); + + await Plotly.newPlot(gd, data(), layoutWithBreaks()); + await Plotly.react(gd, data(), layout()); + expect(gd._fullLayout.xaxis._hasDayOfWeekBreaks).withContext('react').toBe(false); + expect(gd._fullLayout.xaxis.dtick).withContext('react').toBe(newPlotDtick); + }); + + it('should not relayout axes of the previous figure for inside tick labels', async () => { + const sales = {y: [1, 3, 2]}; + const costs = {y: [2, 1, 3], xaxis: 'x2', yaxis: 'y2'}; + + await Plotly.react(gd, [sales, costs], { + width: 700, + height: 400, + grid: {rows: 1, columns: 2, pattern: 'independent'}, + xaxis2: {ticklabelposition: 'inside'}, + yaxis2: {ticklabelposition: 'inside'} + }); + await Plotly.react(gd, [sales], {width: 700, height: 400}); + + expect(gd._fullLayout._insideTickLabelsUpdaterange).toBeUndefined(); + expect(gd._fullLayout._subplots.cartesian).toEqual(['xy']); + expect(d3SelectAll('.subplot').size()).toBe(1); + }); }); describe('category preservation tests on gd passed to Plotly.react()', function() { diff --git a/test/jasmine/tests/carpet_test.js b/test/jasmine/tests/carpet_test.js index 8c4bab22c31..696c98ddcd2 100644 --- a/test/jasmine/tests/carpet_test.js +++ b/test/jasmine/tests/carpet_test.js @@ -844,4 +844,33 @@ describe('contourcarpet plotting & editing', function() { }) .then(done, done.fail); }); + + [ + {type: 'contour', withUids: false}, + {type: 'contour', withUids: true}, + {type: 'histogram2dcontour', withUids: false} + ].forEach(({type, withUids}) => { + it(`can replace a contourcarpet trace with a ${type} trace using react${withUids ? ', with distinct uids' : ''}`, async () => { + const carpet = {type: 'carpet', a: [0.1, 0.2], b: [1.56, 1.01], y: [4.8, 5.1], carpet: 'c1'}; + const contourcarpet = {type: 'contourcarpet', z: [0.22, 0.42], carpet: 'c1'}; + const contour = type === 'contour' ? + {type, z: [[1, 2, 3], [2, 3, 4], [3, 4, 5]]} : + {type, x: [1, 2, 2, 3, 3, 3], y: [1, 1, 2, 2, 3, 3]}; + const layout = {width: 500, height: 400}; + if(withUids) { + carpet.uid = 'carpet'; + contourcarpet.uid = 'contourcarpet'; + contour.uid = 'contour'; + } + + await Plotly.newPlot(gd, [carpet, contourcarpet], layout); + await Plotly.react(gd, [carpet, contour], layout); + + const trace = gd._fullData[1]; + expect(trace.type).toBe(type); + expect(trace._carpetTrace).toBeUndefined(); + expect(trace._carpet).toBeUndefined(); + expect(d3SelectAll('.contour').size()).toBe(1); + }); + }); }); diff --git a/test/jasmine/tests/colorscale_test.js b/test/jasmine/tests/colorscale_test.js index 4957690b7c6..a44e9ba7877 100644 --- a/test/jasmine/tests/colorscale_test.js +++ b/test/jasmine/tests/colorscale_test.js @@ -1199,6 +1199,36 @@ describe('Test colorscale restyle calls:', function() { .then(done, done.fail); }); + it('should use the trace colorscale after the coloraxis is removed', async () => { + const left = [[1, 2, 3], [3, 4, 5]]; + const right = [[2, 3, 9], [1, 5, 6]]; + const layout = {width: 700, height: 350, grid: {rows: 1, columns: 2, xgap: 0.25}}; + + const shared = () => Plotly.react(gd, [ + {type: 'heatmap', z: left, coloraxis: 'coloraxis'}, + {type: 'heatmap', z: right, coloraxis: 'coloraxis', xaxis: 'x2'} + ], {...layout, coloraxis: {colorscale: 'Viridis'}}); + + const assertOwnColorscales = (msg) => { + const fullData = gd._fullData; + expect(fullData[0]._colorAx === undefined).withContext(msg).toBe(true); + expect(fullData[1]._colorAx === undefined).withContext(msg).toBe(true); + expect([fullData[0].zmin, fullData[0].zmax]).withContext(msg).toEqual([1, 5]); + expect([fullData[1].zmin, fullData[1].zmax]).withContext(msg).toEqual([1, 9]); + }; + + await shared(); + await Plotly.react(gd, [ + {type: 'heatmap', z: left, colorscale: 'Viridis', colorbar: {x: 0.42}}, + {type: 'heatmap', z: right, colorscale: 'Viridis', xaxis: 'x2'} + ], layout); + assertOwnColorscales('react'); + + await shared(); + await Plotly.restyle(gd, {coloraxis: [null]}); + assertOwnColorscales('restyle'); + }); + it('@gl should work with scatter3d', function(done) { Plotly.newPlot(gd, [{ type: 'scatter3d',