Skip to content

Per-trace axis extremes #2849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
67db333
add axis.range under visible edits test
etpinard Jul 25, 2018
3e817f3
add findExtremes
etpinard Jul 25, 2018
1cd8482
add _extremes to all traces in calc
etpinard Jul 25, 2018
e9d3ca0
adapt getAutoRange and doAutoRange to trace _extremes
etpinard Jul 25, 2018
2966cf3
generalize concatExtremes for polar, splom and layout components
etpinard Jul 25, 2018
a9cf2e1
replace Axes.expand -> findExtremes in traces/
etpinard Jul 25, 2018
13cb0f0
replace Axex.expand -> findExtremes in annotations and shapes
etpinard Jul 25, 2018
d24eaef
replace Axes.expand -> findExtremes for ErrorBars
etpinard Jul 25, 2018
d9bb617
:hocho: ax._min / ax._max logic for rangeslider
etpinard Jul 25, 2018
9b02ac4
adapt enforceConstraints to new per trace/item _extremes
etpinard Jul 25, 2018
d407c90
adapt polar to new per trace/item _extremes
etpinard Jul 25, 2018
9ab0c2f
:hocho: obsolete comments about ax._min / ax._max
etpinard Jul 25, 2018
6e866c2
adapt gl2d to findExtremes
etpinard Jul 26, 2018
c45427b
:hocho: Axes.expand
etpinard Jul 26, 2018
e77cacc
adapt test for Axex.expand -> findExtremes change
etpinard Jul 26, 2018
ba3c903
adapt test to new getAutoRange API
etpinard Jul 26, 2018
424f4a6
sub fail -> failTest
etpinard Jul 26, 2018
4c250c3
setPositions even when recalc===false
etpinard Jul 26, 2018
5becba0
udpdate jsdoc for Axes.getAutoRange
etpinard Jul 26, 2018
66df51e
use ax.(_traceIndices, annIndices, shapeIndices)
etpinard Jul 27, 2018
d0699c0
fixups (from AJ's review)
etpinard Jul 27, 2018
f1cda60
add bar stack visible -> autorange test & move 'b' init to setPositions
etpinard Jul 27, 2018
9a78013
make annotations & shapes 'visible' -> 'plot' edit type
etpinard Jul 27, 2018
5cfee13
Revert "make annotations & shapes 'visible' -> 'plot' edit type"
etpinard Jul 30, 2018
62f1549
add fallback for _extremes during concat
etpinard Jul 30, 2018
aac11a2
collapse trace extremes before getAutorange
etpinard Jul 30, 2018
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
175 changes: 174 additions & 1 deletion src/plots/cartesian/autorange.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module.exports = {
getAutoRange: getAutoRange,
makePadFn: makePadFn,
doAutoRange: doAutoRange,
expand: expand
expand: expand,
findExtremes: findExtremes
};

// Find the autorange for this axis
Expand Down Expand Up @@ -364,6 +365,178 @@ function expand(ax, data, options) {
for(i = len - 1; i >= iMax; i--) addItem(i);
}

/**
* findExtremes
*
* Find min/max extremes of an array of coordinates on a given axis.
*
* Note that findExtremes is called during `calc`, when we don't yet know the axis
* length; all the inputs should be based solely on the trace data, nothing
* about the axis layout.
*
* Note that `ppad` and `vpad` as well as their asymmetric variants refer to
* the before and after padding of the passed `data` array, not to the whole axis.
*
* @param {object} ax: full axis object
* relies on
* - ax.type
* - ax._m (just its sign)
* - ax.d2l
* @param {array} data:
* array of numbers (i.e. already run though ax.d2c)
* @param {object} options:
* available keys are:
* vpad: (number or number array) pad values (data value +-vpad)
* ppad: (number or number array) pad pixels (pixel location +-ppad)
* ppadplus, ppadminus, vpadplus, vpadminus:
* separate padding for each side, overrides symmetric
* padded: (boolean) add 5% padding to both ends
* (unless one end is overridden by tozero)
* tozero: (boolean) make sure to include zero if axis is linear,
* and make it a tight bound if possible
*
* @return {object}
* - min {array of objects}
* - max {array of objects}
* each object item has fields:
* - val {number}
* - pad {number}
* - extrappad {number}
*/
function findExtremes(ax, data, options) {
if(!options) options = {};
if(!ax._m) ax.setScale();

var minArray = [];
var maxArray = [];

var len = data.length;
var extrapad = options.padded || false;
var tozero = options.tozero && (ax.type === 'linear' || ax.type === '-');
var isLog = (ax.type === 'log');

var i, j, k, v, di, dmin, dmax, ppadiplus, ppadiminus, includeThis, vmin, vmax;

var hasArrayOption = false;

function makePadAccessor(item) {
if(Array.isArray(item)) {
hasArrayOption = true;
return function(i) { return Math.max(Number(item[i]||0), 0); };
}
else {
var v = Math.max(Number(item||0), 0);
return function() { return v; };
}
}

var ppadplus = makePadAccessor((ax._m > 0 ?
options.ppadplus : options.ppadminus) || options.ppad || 0);
var ppadminus = makePadAccessor((ax._m > 0 ?
options.ppadminus : options.ppadplus) || options.ppad || 0);
var vpadplus = makePadAccessor(options.vpadplus || options.vpad);
var vpadminus = makePadAccessor(options.vpadminus || options.vpad);

if(!hasArrayOption) {
// with no arrays other than `data` we don't need to consider
// every point, only the extreme data points
vmin = Infinity;
vmax = -Infinity;

if(isLog) {
for(i = 0; i < len; i++) {
v = data[i];
// data is not linearized yet so we still have to filter out negative logs
if(v < vmin && v > 0) vmin = v;
if(v > vmax && v < FP_SAFE) vmax = v;
}
} else {
for(i = 0; i < len; i++) {
v = data[i];
if(v < vmin && v > -FP_SAFE) vmin = v;
if(v > vmax && v < FP_SAFE) vmax = v;
}
}

data = [vmin, vmax];
len = 2;
}

function addItem(i) {
di = data[i];
if(!isNumeric(di)) return;
ppadiplus = ppadplus(i);
ppadiminus = ppadminus(i);
vmin = di - vpadminus(i);
vmax = di + vpadplus(i);
// special case for log axes: if vpad makes this object span
// more than an order of mag, clip it to one order. This is so
// we don't have non-positive errors or absurdly large lower
// range due to rounding errors
if(isLog && vmin < vmax / 10) vmin = vmax / 10;

dmin = ax.c2l(vmin);
dmax = ax.c2l(vmax);

if(tozero) {
dmin = Math.min(0, dmin);
dmax = Math.max(0, dmax);
}

for(k = 0; k < 2; k++) {
var newVal = k ? dmax : dmin;
if(goodNumber(newVal)) {
var extremes = k ? maxArray : minArray;
var newPad = k ? ppadiplus : ppadiminus;
var atLeastAsExtreme = k ? greaterOrEqual : lessOrEqual;

includeThis = true;
/*
* Take items v from ax._min/_max and compare them to the presently active point:
* - Since we don't yet know the relationship between pixels and values
* (that's what we're trying to figure out!) AND we don't yet know how
* many pixels `extrapad` represents (it's going to be 5% of the length,
* but we don't want to have to redo _min and _max just because length changed)
* two point must satisfy three criteria simultaneously for one to supersede the other:
* - at least as extreme a `val`
* - at least as big a `pad`
* - an unpadded point cannot supersede a padded point, but any other combination can
*
* - If the item supersedes the new point, set includethis false
* - If the new pt supersedes the item, delete it from ax._min/_max
*/
for(j = 0; j < extremes.length && includeThis; j++) {
v = extremes[j];
if(atLeastAsExtreme(v.val, newVal) && v.pad >= newPad && (v.extrapad || !extrapad)) {
includeThis = false;
break;
} else if(atLeastAsExtreme(newVal, v.val) && v.pad <= newPad && (extrapad || !v.extrapad)) {
extremes.splice(j, 1);
j--;
}
}
if(includeThis) {
var clipAtZero = (tozero && newVal === 0);
extremes.push({
val: newVal,
pad: clipAtZero ? 0 : newPad,
extrapad: clipAtZero ? false : extrapad
});
}
}
}
}

// For efficiency covering monotonic or near-monotonic data,
// check a few points at both ends first and then sweep
// through the middle
var iMax = Math.min(6, len);
for(i = 0; i < iMax; i++) addItem(i);
for(i = len - 1; i >= iMax; i--) addItem(i);

return {min: minArray, max: maxArray};
}

// In order to stop overflow errors, don't consider points
// too close to the limits of js floating point
function goodNumber(v) {
Expand Down
6 changes: 5 additions & 1 deletion src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -2455,10 +2455,14 @@ plots.doCalcdata = function(gd, traces) {
}
}

// find array attributes in trace
for(i = 0; i < fullData.length; i++) {
trace = fullData[i];

// find array attributes in trace
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 isn't PlotSchema.findArrayAttributes(trace) self-documenting enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call -> d0699c0

trace._arrayAttrs = PlotSchema.findArrayAttributes(trace);

// keep track of trace extremes (for autorange) in here
trace._extremes = {};
}

// add polar axes to axis list
Expand Down
56 changes: 44 additions & 12 deletions test/jasmine/tests/scatter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -879,45 +879,77 @@ describe('end-to-end scatter tests', function() {
.then(done);
});

it('should update axis range accordingly on marker.size edits', function(done) {
function _assert(msg, xrng, yrng) {
var fullLayout = gd._fullLayout;
expect(fullLayout.xaxis.range).toBeCloseToArray(xrng, 2, msg + ' xrng');
expect(fullLayout.yaxis.range).toBeCloseToArray(yrng, 2, msg + ' yrng');
}
function assertAxisRanges(msg, xrng, yrng) {
var fullLayout = gd._fullLayout;
expect(fullLayout.xaxis.range).toBeCloseToArray(xrng, 2, msg + ' xrng');
expect(fullLayout.yaxis.range).toBeCloseToArray(yrng, 2, msg + ' yrng');
}

var schema = Plotly.PlotSchema.get();

it('should update axis range accordingly on marker.size edits', function(done) {
// edit types are important to this test
var schema = Plotly.PlotSchema.get();
expect(schema.traces.scatter.attributes.marker.size.editType)
.toBe('calc', 'marker.size editType');
expect(schema.layout.layoutAttributes.xaxis.autorange.editType)
.toBe('axrange', 'ax autorange editType');

Plotly.plot(gd, [{ y: [1, 2, 1] }])
.then(function() {
_assert('auto rng / base marker.size', [-0.13, 2.13], [0.93, 2.07]);
assertAxisRanges('auto rng / base marker.size', [-0.13, 2.13], [0.93, 2.07]);
return Plotly.relayout(gd, {
'xaxis.range': [0, 2],
'yaxis.range': [0, 2]
});
})
.then(function() {
_assert('set rng / base marker.size', [0, 2], [0, 2]);
assertAxisRanges('set rng / base marker.size', [0, 2], [0, 2]);
return Plotly.restyle(gd, 'marker.size', 50);
})
.then(function() {
_assert('set rng / big marker.size', [0, 2], [0, 2]);
assertAxisRanges('set rng / big marker.size', [0, 2], [0, 2]);
return Plotly.relayout(gd, {
'xaxis.autorange': true,
'yaxis.autorange': true
});
})
.then(function() {
_assert('auto rng / big marker.size', [-0.28, 2.28], [0.75, 2.25]);
assertAxisRanges('auto rng / big marker.size', [-0.28, 2.28], [0.75, 2.25]);
return Plotly.restyle(gd, 'marker.size', null);
})
.then(function() {
_assert('auto rng / base marker.size', [-0.13, 2.13], [0.93, 2.07]);
assertAxisRanges('auto rng / base marker.size', [-0.13, 2.13], [0.93, 2.07]);
})
.catch(failTest)
.then(done);
});

it('should update axis range according to visible edits', function(done) {
expect(schema.traces.scatter.attributes.visible.editType)
.toBe('plot', 'visible editType');

Plotly.plot(gd, [
{x: [1, 2, 3], y: [1, 2, 1]},
{x: [4, 5, 6], y: [-1, -2, -1]}
])
.then(function() {
assertAxisRanges('both visible', [0.676, 6.323], [-2.29, 2.29]);
return Plotly.restyle(gd, 'visible', false, [1]);
})
.then(function() {
assertAxisRanges('visible [true,false]', [0.87, 3.128], [0.926, 2.07]);
return Plotly.restyle(gd, 'visible', false, [0]);
})
.then(function() {
assertAxisRanges('both invisible', [0.87, 3.128], [0.926, 2.07]);
return Plotly.restyle(gd, 'visible', true, [1]);
})
.then(function() {
assertAxisRanges('visible [false,true]', [3.871, 6.128], [-2.07, -0.926]);
return Plotly.restyle(gd, 'visible', true);
})
.then(function() {
assertAxisRanges('back to both visible', [0.676, 6.323], [-2.29, 2.29]);
})
.catch(failTest)
.then(done);
Expand Down