Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions draftlogs/XXXX_change.md
Original file line number Diff line number Diff line change
@@ -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)]
32 changes: 31 additions & 1 deletion src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions test/jasmine/tests/gl2d_plot_interact_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
49 changes: 49 additions & 0 deletions test/jasmine/tests/plot_api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
Loading