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 4 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
2 changes: 1 addition & 1 deletion src/components/rangeslider/calc_autorange.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = function calcAutorange(gd) {

if(opts && opts.visible && opts.autorange && ax._min.length && ax._max.length) {
opts._input.autorange = true;
opts._input.range = opts.range = getAutoRange(ax);
opts._input.range = opts.range = getAutoRange(gd, ax);
}
}
};
2 changes: 1 addition & 1 deletion src/plot_api/subroutines.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ exports.doAutoRangeAndConstraints = function(gd) {
for(var i = 0; i < axList.length; i++) {
var ax = axList[i];
cleanAxisConstraints(gd, ax);
doAutoRange(ax);
doAutoRange(gd, ax);
}

enforceAxisConstraints(gd);
Expand Down
262 changes: 232 additions & 30 deletions src/plots/cartesian/autorange.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var isNumeric = require('fast-isnumeric');
Expand All @@ -18,11 +17,13 @@ module.exports = {
getAutoRange: getAutoRange,
makePadFn: makePadFn,
doAutoRange: doAutoRange,
expand: expand
expand: expand,
findExtremes: findExtremes
};

// Find the autorange for this axis
//
// TODO update!!
// assumes ax._min and ax._max have already been set by calling axes.expand
// using calcdata from all traces. These are arrays of objects:
// {
Expand All @@ -38,42 +39,50 @@ module.exports = {
// maintaining backward compatibility. category will always have to use calcdata
// though, because otherwise values between categories (or outside all categories)
// would be impossible.
function getAutoRange(ax) {
function getAutoRange(gd, ax) {
var i, j;
var newRange = [];
var minmin = ax._min[0].val;
var maxmax = ax._max[0].val;
var mbest = 0;
var axReverse = false;

var getPad = makePadFn(ax);
var minArray = concatExtremes(gd, ax, 'min');
var maxArray = concatExtremes(gd, ax, 'max');

var i, j, minpt, maxpt, minbest, maxbest, dp, dv;
if(minArray.length === 0 || maxArray.length === 0) {
return Lib.simpleMap(ax.range, ax.r2l);
}

var minmin = minArray[0].val;
var maxmax = maxArray[0].val;

for(i = 1; i < ax._min.length; i++) {
for(i = 1; i < minArray.length; i++) {
if(minmin !== maxmax) break;
minmin = Math.min(minmin, ax._min[i].val);
minmin = Math.min(minmin, minArray[i].val);
}
for(i = 1; i < ax._max.length; i++) {
for(i = 1; i < maxArray.length; i++) {
if(minmin !== maxmax) break;
maxmax = Math.max(maxmax, ax._max[i].val);
maxmax = Math.max(maxmax, maxArray[i].val);
}

var axReverse = false;

if(ax.range) {
var rng = Lib.simpleMap(ax.range, ax.r2l);
axReverse = rng[1] < rng[0];
}

// one-time setting to easily reverse the axis
// when plotting from code
if(ax.autorange === 'reversed') {
axReverse = true;
ax.autorange = true;
}

for(i = 0; i < ax._min.length; i++) {
minpt = ax._min[i];
for(j = 0; j < ax._max.length; j++) {
maxpt = ax._max[j];
var mbest = 0;
var minpt, maxpt, minbest, maxbest, dp, dv;

for(i = 0; i < minArray.length; i++) {
minpt = minArray[i];
for(j = 0; j < maxArray.length; j++) {
maxpt = maxArray[j];
dv = maxpt.val - minpt.val;
dp = ax._length - getPad(minpt) - getPad(maxpt);
if(dv > 0 && dp > 0 && dv / dp > mbest) {
Expand All @@ -89,11 +98,9 @@ function getAutoRange(ax) {
var upper = minmin + 1;
if(ax.rangemode === 'tozero') {
newRange = minmin < 0 ? [lower, 0] : [0, upper];
}
else if(ax.rangemode === 'nonnegative') {
} else if(ax.rangemode === 'nonnegative') {
newRange = [Math.max(0, lower), Math.max(0, upper)];
}
else {
} else {
newRange = [lower, upper];
}
}
Expand Down Expand Up @@ -133,11 +140,9 @@ function getAutoRange(ax) {
if(ax.rangemode === 'tozero') {
if(newRange[0] < 0) {
newRange = [newRange[0], 0];
}
else if(newRange[0] > 0) {
} else if(newRange[0] > 0) {
newRange = [0, newRange[0]];
}
else {
} else {
newRange = [0, 1];
}
}
Expand Down Expand Up @@ -173,15 +178,40 @@ function makePadFn(ax) {
return function getPad(pt) { return pt.pad + (pt.extrapad ? extrappad : 0); };
}

function doAutoRange(ax) {
function concatExtremes(gd, ax, k) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm. Come to think of it, this thing will be a drag. Looping over all traces, annotations and shapes for every axis, doesn't scale very well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Improvements in -> 66df51e

var i;
var out = [];

var fullData = gd._fullData;
var attr = ax._id.charAt(0) + 'axis';

// should be general enough for 3d, polar etc.

for(i = 0; i < fullData.length; i++) {
var trace = fullData[i];

if(trace.visible === true && (
trace[attr] === ax._id
)) {
out = out.concat(trace._extremes[attr][k]);
}
}

// - annotation
// - shapes
// - annotation3d
// - errorbars?

return out;
}

function doAutoRange(gd, ax) {
if(!ax._length) ax.setScale();

// TODO do we really need this?
var hasDeps = (ax._min && ax._max && ax._min.length && ax._max.length);
var axIn;

if(ax.autorange && hasDeps) {
ax.range = getAutoRange(ax);
if(ax.autorange) {
ax.range = getAutoRange(gd, ax);

ax._r = ax.range.slice();
ax._rl = Lib.simpleMap(ax._r, ax.r2l);
Expand Down Expand Up @@ -364,6 +394,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
Loading