Skip to content

Commit

Permalink
use ax.(_traceIndices, annIndices, shapeIndices)
Browse files Browse the repository at this point in the history
... to keep track of traces, annotations and shapes plotted on
    axis ax. Adapt concatExtremes accordingly!
  • Loading branch information
etpinard committed Jul 27, 2018
1 parent 5becba0 commit 66df51e
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 92 deletions.
5 changes: 5 additions & 0 deletions src/components/annotations/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ function handleAnnotationDefaults(annIn, annOut, fullLayout) {
// xref, yref
var axRef = Axes.coerceRef(annIn, annOut, gdMock, axLetter, '', 'paper');

if(axRef !== 'paper') {
var ax = Axes.getFromId(gdMock, axRef);
ax._annIndices.push(annOut._index);
}

// x, y
Axes.coercePosition(annOut, gdMock, coerce, axRef, axLetter, 0.5);

Expand Down
1 change: 1 addition & 0 deletions src/components/shapes/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function handleShapeDefaults(shapeIn, shapeOut, fullLayout) {

if(axRef !== 'paper') {
ax = Axes.getFromId(gdMock, axRef);
ax._shapeIndices.push(shapeOut._index);
r2pos = helpers.rangeToShapePosition(ax);
pos2r = helpers.shapePositionToRange(ax);
}
Expand Down
84 changes: 29 additions & 55 deletions src/plots/cartesian/autorange.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ var isNumeric = require('fast-isnumeric');
var Lib = require('../../lib');
var FP_SAFE = require('../../constants/numerical').FP_SAFE;

var Registry = require('../../registry');

module.exports = {
getAutoRange: getAutoRange,
makePadFn: makePadFn,
Expand All @@ -31,16 +29,17 @@ module.exports = {
*
* getAutoRange uses return values from findExtremes where:
*
* {
* val: calcdata value,
* pad: extra pixels beyond this value,
* extrapad: bool, does this point want 5% extra padding
* }
*
* @param {object} gd:
* graph div object with filled in fullData and fullLayout,
* graph div object with filled-in fullData and fullLayout, in particular
* with filled-in '_extremes' containers:
* {
* val: calcdata value,
* pad: extra pixels beyond this value,
* extrapad: bool, does this point want 5% extra padding
* }
* @param {object} ax:
* full axis object
* full axis object, in particular with filled-in '_traceIndices'
* and '_annIndices' / '_shapeIndices' if applicable
* @return {array}
* an array of [min, max]. These are calcdata for log and category axes
* and data for linear and date axes.
Expand All @@ -55,8 +54,9 @@ function getAutoRange(gd, ax) {
var newRange = [];

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

if(minArray.length === 0 || maxArray.length === 0) {
return Lib.simpleMap(ax.range, ax.r2l);
Expand Down Expand Up @@ -189,57 +189,31 @@ function makePadFn(ax) {
return function getPad(pt) { return pt.pad + (pt.extrapad ? extrappad : 0); };
}

function concatExtremes(gd, ax, ext) {
var i;
var out = [];

function concatExtremes(gd, ax) {
var axId = ax._id;
var fullData = gd._fullData;
var fullLayout = gd._fullLayout;
var minArray = [];
var maxArray = [];

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

if(trace.visible === true) {
if(Registry.traceIs(trace, 'cartesian') || Registry.traceIs(trace, 'gl2d')) {
var axId = ax._id;
if(extremes[axId]) {
out = out.concat(extremes[axId][ext]);
}
} else if(Registry.traceIs(trace, 'polar')) {
if(trace.subplot === ax._subplot) {
out = out.concat(extremes[ax._name][ext]);
}
function _concat(cont, indices) {
for(var i = 0; i < indices.length; i++) {
var item = cont[indices[i]];
var extremes = item._extremes[axId];
if(item.visible === true && extremes) {
minArray = minArray.concat(extremes.min);
maxArray = maxArray.concat(extremes.max);
}
}
}

var fullLayout = gd._fullLayout;
var annotations = fullLayout.annotations;
var shapes = fullLayout.shapes;

if(Array.isArray(annotations)) {
out = out.concat(concatComponentExtremes(annotations, ax, ext));
}
if(Array.isArray(shapes)) {
out = out.concat(concatComponentExtremes(shapes, ax, ext));
}

return out;
}
_concat(fullData, ax._traceIndices);
_concat(fullLayout.annotations || [], ax._annIndices || []);
_concat(fullLayout.shapes || [], ax._shapeIndices || []);

function concatComponentExtremes(items, ax, ext) {
var out = [];
var axId = ax._id;
var letter = axId.charAt(0);
// TODO collapse more!

for(var i = 0; i < items.length; i++) {
var d = items[i];
var extremes = d._extremes;
if(d.visible && d[letter + 'ref'] === axId && extremes[axId]) {
out = out.concat(extremes[axId][ext]);
}
}
return out;
return {min: minArray, max: maxArray};
}

function doAutoRange(gd, ax) {
Expand Down
5 changes: 3 additions & 2 deletions src/plots/cartesian/constraints.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,19 @@ exports.enforce = function enforceAxisConstraints(gd) {
updateDomain(ax, factor);
ax.setScale();
var m = Math.abs(ax._m);
var extremes = concatExtremes(gd, ax);
var minArray = extremes.min;
var maxArray = extremes.max;
var newVal;
var k;

var minArray = concatExtremes(gd, ax, 'min');
for(k = 0; k < minArray.length; k++) {
newVal = minArray[k].val - getPad(minArray[k]) / m;
if(newVal > outerMin && newVal < rangeMin) {
rangeMin = newVal;
}
}

var maxArray = concatExtremes(gd, ax, 'max');
for(k = 0; k < maxArray.length; k++) {
newVal = maxArray[k].val + getPad(maxArray[k]) / m;
if(newVal < outerMax && newVal > rangeMax) {
Expand Down
72 changes: 51 additions & 21 deletions src/plots/cartesian/layout_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

'use strict';

var Registry = require('../../registry');
var Lib = require('../../lib');
var Color = require('../../components/color');
var Template = require('../../plot_api/plot_template');
Expand All @@ -20,31 +19,57 @@ var handleTypeDefaults = require('./type_defaults');
var handleAxisDefaults = require('./axis_defaults');
var handleConstraintDefaults = require('./constraint_defaults');
var handlePositionDefaults = require('./position_defaults');

var axisIds = require('./axis_ids');
var id2name = axisIds.id2name;
var name2id = axisIds.name2id;

var Registry = require('../../registry');
var traceIs = Registry.traceIs;
var getComponentMethod = Registry.getComponentMethod;

function appendList(cont, k, item) {
if(Array.isArray(cont[k])) cont[k].push(item);
else cont[k] = [item];
}

module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
var ax2traces = {};
var xaCheater = {};
var xaNonCheater = {};
var outerTicks = {};
var noGrids = {};
var i;
var i, j;

// look for axes in the data
for(i = 0; i < fullData.length; i++) {
var trace = fullData[i];

if(!Registry.traceIs(trace, 'cartesian') && !Registry.traceIs(trace, 'gl2d')) {
continue;
if(!traceIs(trace, 'cartesian') && !traceIs(trace, 'gl2d')) continue;

var xaName;
if(trace.xaxis) {
xaName = id2name(trace.xaxis);
appendList(ax2traces, xaName, trace);
} else if(trace.xaxes) {
for(j = 0; j < trace.xaxes.length; j++) {
appendList(ax2traces, id2name(trace.xaxes[j]), trace);
}
}

var xaName = axisIds.id2name(trace.xaxis);
var yaName = axisIds.id2name(trace.yaxis);
var yaName;
if(trace.yaxis) {
yaName = id2name(trace.yaxis);
appendList(ax2traces, yaName, trace);
} else if(trace.yaxes) {
for(j = 0; j < trace.yaxes.length; j++) {
appendList(ax2traces, id2name(trace.yaxes[j]), trace);
}
}

// Two things trigger axis visibility:
// 1. is not carpet
// 2. carpet that's not cheater
if(!Registry.traceIs(trace, 'carpet') || (trace.type === 'carpet' && !trace._cheater)) {
if(!traceIs(trace, 'carpet') || (trace.type === 'carpet' && !trace._cheater)) {
if(xaName) xaNonCheater[xaName] = 1;
}

Expand All @@ -57,22 +82,22 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
}

// check for default formatting tweaks
if(Registry.traceIs(trace, '2dMap')) {
outerTicks[xaName] = true;
outerTicks[yaName] = true;
if(traceIs(trace, '2dMap')) {
outerTicks[xaName] = 1;
outerTicks[yaName] = 1;
}

if(Registry.traceIs(trace, 'oriented')) {
if(traceIs(trace, 'oriented')) {
var positionAxis = trace.orientation === 'h' ? yaName : xaName;
noGrids[positionAxis] = true;
noGrids[positionAxis] = 1;
}
}

var subplots = layoutOut._subplots;
var xIds = subplots.xaxis;
var yIds = subplots.yaxis;
var xNames = Lib.simpleMap(xIds, axisIds.id2name);
var yNames = Lib.simpleMap(yIds, axisIds.id2name);
var xNames = Lib.simpleMap(xIds, id2name);
var yNames = Lib.simpleMap(yIds, id2name);
var axNames = xNames.concat(yNames);

// plot_bgcolor only makes sense if there's a (2D) plot!
Expand Down Expand Up @@ -108,7 +133,7 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
var axName2 = list[j];

if(axName2 !== axName && !(layoutIn[axName2] || {}).overlaying) {
out.push(axisIds.name2id(axName2));
out.push(name2id(axName2));
}
}

Expand All @@ -127,7 +152,12 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
axLayoutIn = layoutIn[axName];
axLayoutOut = Template.newContainer(layoutOut, axName, axLetter + 'axis');

handleTypeDefaults(axLayoutIn, axLayoutOut, coerce, fullData, axName);
var traces = ax2traces[axName] || [];
axLayoutOut._traceIndices = traces.map(function(t) { return t._expandedIndex; });
axLayoutOut._annIndices = [];
axLayoutOut._shapeIndices = [];

handleTypeDefaults(axLayoutIn, axLayoutOut, coerce, traces, axName);

var overlayableAxes = getOverlayableAxes(axLetter, axName);

Expand All @@ -136,7 +166,7 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
font: layoutOut.font,
outerTicks: outerTicks[axName],
showGrid: !noGrids[axName],
data: fullData,
data: traces,
bgColor: bgColor,
calendar: layoutOut.calendar,
automargin: true,
Expand Down Expand Up @@ -173,8 +203,8 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
}

// quick second pass for range slider and selector defaults
var rangeSliderDefaults = Registry.getComponentMethod('rangeslider', 'handleDefaults');
var rangeSelectorDefaults = Registry.getComponentMethod('rangeselector', 'handleDefaults');
var rangeSliderDefaults = getComponentMethod('rangeslider', 'handleDefaults');
var rangeSelectorDefaults = getComponentMethod('rangeselector', 'handleDefaults');

for(i = 0; i < xNames.length; i++) {
axName = xNames[i];
Expand All @@ -201,7 +231,7 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
axLayoutIn = layoutIn[axName];
axLayoutOut = layoutOut[axName];

var anchoredAxis = layoutOut[axisIds.id2name(axLayoutOut.anchor)];
var anchoredAxis = layoutOut[id2name(axLayoutOut.anchor)];

var fixedRangeDflt = (
anchoredAxis &&
Expand Down
1 change: 1 addition & 0 deletions src/plots/polar/layout_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function handleDefaults(contIn, contOut, coerce, opts) {
// propagate the template.
var axOut = contOut[axName] = {};
axOut._id = axOut._name = axName;
axOut._traceIndices = subplotData.map(function(t) { return t.index; });

var dataAttr = constants.axisName2dataArray[axName];
var axType = handleAxisTypeDefaults(axIn, axOut, coerceAxis, subplotData, dataAttr);
Expand Down
1 change: 0 additions & 1 deletion src/plots/polar/polar.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ proto.updateLayout = function(fullLayout, polarLayout) {
proto.doAutoRange = function(fullLayout, polarLayout) {
var radialLayout = polarLayout.radialaxis;
var ax = this.radialAxis;
ax._subplot = this.id;

setScale(ax, radialLayout, fullLayout);
doAutoRange(this.gd, ax);
Expand Down
2 changes: 1 addition & 1 deletion src/traces/scatterpolar/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports = function calc(gd, trace) {
}

var ppad = calcMarkerSize(trace, len);
trace._extremes.radialaxis = Axes.findExtremes(radialAxis, rArray, {ppad: ppad});
trace._extremes.x = Axes.findExtremes(radialAxis, rArray, {ppad: ppad});

calcColorscale(trace);
arraysToCalcdata(cd, trace);
Expand Down
2 changes: 1 addition & 1 deletion src/traces/scatterpolargl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function calc(container, trace) {
stash.r = rArray;
stash.theta = thetaArray;

trace._extremes.radialaxis = Axes.findExtremes(radialAxis, rArray, {tozero: true});
trace._extremes.x = Axes.findExtremes(radialAxis, rArray, {tozero: true});

return [{x: false, y: false, t: stash, trace: trace}];
}
Expand Down
22 changes: 16 additions & 6 deletions test/jasmine/tests/annotations_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ describe('Test annotations', function() {
layoutOut._has = Plots._hasPlotType.bind(layoutOut);
layoutOut._subplots = {xaxis: ['x', 'x2'], yaxis: ['y', 'y2']};
['xaxis', 'yaxis', 'xaxis2', 'yaxis2'].forEach(function(axName) {
if(!layoutOut[axName]) layoutOut[axName] = {type: 'linear', range: [0, 1]};
if(!layoutOut[axName]) {
layoutOut[axName] = {
type: 'linear',
range: [0, 1],
_annIndices: []
};
}
Axes.setConvert(layoutOut[axName]);
});

Expand Down Expand Up @@ -90,7 +96,11 @@ describe('Test annotations', function() {
};

var layoutOut = {
xaxis: { type: 'date', range: ['2000-01-01', '2016-01-01'] }
xaxis: {
type: 'date',
range: ['2000-01-01', '2016-01-01'],
_annIndices: []
}
};

_supply(layoutIn, layoutOut);
Expand Down Expand Up @@ -128,10 +138,10 @@ describe('Test annotations', function() {
};

var layoutOut = {
xaxis: {type: 'linear', range: [0, 1]},
yaxis: {type: 'date', range: ['2000-01-01', '2018-01-01']},
xaxis2: {type: 'log', range: [1, 2]},
yaxis2: {type: 'category', range: [0, 1]}
xaxis: {type: 'linear', range: [0, 1], _annIndices: []},
yaxis: {type: 'date', range: ['2000-01-01', '2018-01-01'], _annIndices: []},
xaxis2: {type: 'log', range: [1, 2], _annIndices: []},
yaxis2: {type: 'category', range: [0, 1], _annIndices: []}
};

_supply(layoutIn, layoutOut);
Expand Down
Loading

0 comments on commit 66df51e

Please sign in to comment.