diff --git a/draftlogs/XXXX_change.md b/draftlogs/XXXX_change.md new file mode 100644 index 00000000000..83d0f64addf --- /dev/null +++ b/draftlogs/XXXX_change.md @@ -0,0 +1 @@ +- Skip the recalculation after a range change of axes with `scaleanchor`, if every trace is a regl trace and the ranges of the constrained axes are fixed [[#XXXX](https://github.com/plotly/plotly.js/pull/XXXX)] diff --git a/src/plot_api/plot_api.js b/src/plot_api/plot_api.js index 5eea5fc4806..ddaa2eab7aa 100644 --- a/src/plot_api/plot_api.js +++ b/src/plot_api/plot_api.js @@ -2205,7 +2205,8 @@ function _relayout(gd, aobj) { // specifying arbitrary ranges for all axes in the group. // this way some ranges may expand beyond what's specified, // as they do at first draw, to satisfy the constraints. - flags.calc = true; + // The replot applies the constraints from the axes. A recalc uploads every regl trace again. + flags[constraintsNeedCalc(gd, group) ? 'calc' : 'plot'] = true; for (var groupAxId in group) { if (!rangesAltered[groupAxId]) { Axes.getFromId(gd, groupAxId)._constraintShrinkable = true; @@ -2245,6 +2246,35 @@ function _relayout(gd, aobj) { }; } +/** + * Decide if a relayout that alters the ranges of a constraint group needs a recalc. + * + * @param gd - the graph div + * @param group - the constraint group, keyed by axis id + * @returns false if every trace is a regl trace and every axis of the group has a fixed range, else true + */ +function constraintsNeedCalc(gd, group) { + const fullData = gd._fullData || []; + for (let i = 0; i < fullData.length; i++) { + if (!Registry.traceIs(fullData[i], 'regl')) return true; + } + for (const axId in group) { + const axIn = Axes.getFromId(gd, axId)?._input; + if (!axIn) return true; + const range = axIn.range; + const fixed = + axIn.autorange === false || + (axIn.autorange === undefined && + Array.isArray(range) && + range.length === 2 && + range[0] !== null && + range[1] !== null); + // An autorange needs the extremes of a new calc + if (!fixed) return true; + } + return false; +} + /* * updateAutosize: we made a change, does it change the autosize result? * puts the new size into fullLayout diff --git a/test/jasmine/tests/gl2d_plot_interact_test.js b/test/jasmine/tests/gl2d_plot_interact_test.js index 1ee3b711a77..35d33ef0a49 100644 --- a/test/jasmine/tests/gl2d_plot_interact_test.js +++ b/test/jasmine/tests/gl2d_plot_interact_test.js @@ -622,6 +622,62 @@ describe('Test gl2d plot interactions:', function() { .then(done, done.fail); }); + it('@gl keeps axis constraints on zoom and pan without a recalc', (done) => { + let centerX; + let centerY; + let calcdata; + + function assertScale(msg) { + let xa = gd._fullLayout.xaxis; + let ya = gd._fullLayout.yaxis; + // With scaleratio 2, y has two times the pixels per unit of x + expect(-ya._m).toBeCloseTo(2 * xa._m, 6, msg); + expect(gd.calcdata).toBe(calcdata, 'no recalc - ' + msg); + } + + Plotly.newPlot(gd, [{ + type: 'scattergl', x: [1, 15], y: [1, 15] + }], { + width: 400, + height: 400, + margin: {t: 100, b: 100, l: 100, r: 100}, + xaxis: {range: [-8, 24]}, + yaxis: {range: [0, 16], scaleanchor: 'x', scaleratio: 2} + }) + .then(() => { + let bBox = gd.getBoundingClientRect(); + centerX = bBox.left + 200; + centerY = bBox.top + 200; + calcdata = gd.calcdata; + + return mouseTo([centerX, centerY], [centerX - 1, centerY - 50]); + }) + .then(() => { + expect(gd.layout.xaxis.range).toBeCloseToArray([0, 8], 3); + expect(gd.layout.yaxis.range).toBeCloseToArray([8, 12], 3); + assertScale('zoom'); + + return Plotly.relayout(gd, 'dragmode', 'pan'); + }) + .then(() => { + return mouseTo([centerX, centerY], [centerX + 50, centerY]); + }) + .then(() => { + expect(gd.layout.xaxis.range).toBeCloseToArray([-2, 6], 3); + expect(gd.layout.yaxis.range).toBeCloseToArray([8, 12], 3); + assertScale('pan'); + + return Plotly.relayout(gd, 'xaxis.range', [0, 4]); + }) + .then(() => { + // The y range changes to keep the ratio, about the same center + expect(gd.layout.xaxis.range).toBeCloseToArray([0, 4], 3); + expect(gd.layout.yaxis.range).toBeCloseToArray([9, 11], 3); + assertScale('relayout of one axis'); + }) + .then(done, done.fail); + }); + it('@gl data-referenced annotations should update on drag', function(done) { function assertAnnotation(xy) { var ann = d3Select('g.annotation-text-g').select('g'); diff --git a/test/jasmine/tests/plot_api_test.js b/test/jasmine/tests/plot_api_test.js index 40c75212cda..8b2431109b4 100644 --- a/test/jasmine/tests/plot_api_test.js +++ b/test/jasmine/tests/plot_api_test.js @@ -754,6 +754,55 @@ describe('Test plot api', function () { }) .then(done, done.fail); }); + + it('should trigger replot (but not recalc) on constrained axis range updates when every trace is regl', (done) => { + function regl(layout) { + return mock({ + data: [{type: 'scattergl', x: [1, 2, 3], y: [1, 2, 1]}], + layout: layout + }); + } + function fixed() { + return {xaxis: {range: [0, 4]}, yaxis: {range: [0, 3], scaleanchor: 'x'}}; + } + + gd = regl(fixed()); + Plotly.relayout(gd, 'xaxis.range', [1, 3]) + .then(() => { + expectReplot('range'); + + gd = regl(fixed()); + return Plotly.relayout(gd, {'xaxis.range': [1, 3], 'yaxis.range': [0, 2]}); + }) + .then(() => { + expectReplot('both ranges'); + + gd = regl({xaxis: {}, yaxis: {range: [0, 3], scaleanchor: 'x'}}); + return Plotly.relayout(gd, 'yaxis.range', [0, 2]); + }) + .then(() => { + expect(gd.calcdata).toBeUndefined('an axis of the group autoranges'); + + gd = regl(fixed()); + return Plotly.relayout(gd, 'xaxis.autorange', true); + }) + .then(() => { + expect(gd.calcdata).toBeUndefined('autorange'); + + gd = mock({ + data: [ + {type: 'scattergl', x: [1, 2, 3], y: [1, 2, 1]}, + {x: [1, 2, 3], y: [2, 1, 2]} + ], + layout: fixed() + }); + return Plotly.relayout(gd, 'xaxis.range', [1, 3]); + }) + .then(() => { + expect(gd.calcdata).toBeUndefined('a trace that is not regl'); + }) + .then(done, done.fail); + }); }); describe('Plotly.restyle subroutines switchboard', function () {