Skip to content

Commit 2a41f9e

Browse files
committed
react+transforms PR review edits
- is1D -> isArray1D - improved comments - opts object instead of confusing boolean to Plots.supplyDefaults - merge & simplify remapTransformedArrays - hasX
1 parent b436d52 commit 2a41f9e

File tree

15 files changed

+71
-63
lines changed

15 files changed

+71
-63
lines changed

src/lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ lib.ensureArray = require('./ensure_array');
3030
var isArrayModule = require('./is_array');
3131
lib.isTypedArray = isArrayModule.isTypedArray;
3232
lib.isArrayOrTypedArray = isArrayModule.isArrayOrTypedArray;
33-
lib.is1D = isArrayModule.is1D;
33+
lib.isArray1D = isArrayModule.isArray1D;
3434

3535
var coerceModule = require('./coerce');
3636
lib.valObjectMeta = coerceModule.valObjectMeta;

src/lib/is_array.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,20 @@ function isArrayOrTypedArray(a) {
2626
return Array.isArray(a) || isTypedArray(a);
2727
}
2828

29-
function is1D(a) {
29+
/*
30+
* Test whether an input object is 1D.
31+
*
32+
* Assumes we already know the object is an array.
33+
*
34+
* Looks only at the first element, if the dimensionality is
35+
* not consistent we won't figure that out here.
36+
*/
37+
function isArray1D(a) {
3038
return !isArrayOrTypedArray(a[0]);
3139
}
3240

3341
module.exports = {
3442
isTypedArray: isTypedArray,
3543
isArrayOrTypedArray: isArrayOrTypedArray,
36-
is1D: is1D
44+
isArray1D: isArray1D
3745
};

src/plot_api/plot_api.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2270,7 +2270,7 @@ exports.react = function(gd, data, layout, config) {
22702270
// "true" skips updating calcdata and remapping arrays from calcTransforms,
22712271
// which supplyDefaults usually does at the end, but we may need to NOT do
22722272
// if the diff (which we haven't determined yet) says we'll recalc
2273-
Plots.supplyDefaults(gd, true);
2273+
Plots.supplyDefaults(gd, {skipUpdateCalc: true});
22742274

22752275
var newFullData = gd._fullData;
22762276
var newFullLayout = gd._fullLayout;

src/plots/plots.js

+43-45
Original file line numberDiff line numberDiff line change
@@ -254,27 +254,37 @@ var extraFormatKeys = [
254254
'year', 'month', 'dayMonth', 'dayMonthYear'
255255
];
256256

257-
// Fill in default values:
258-
//
259-
// gd.data, gd.layout:
260-
// are precisely what the user specified,
261-
// these fields shouldn't be modified nor used directly
262-
// after the supply defaults step.
263-
//
264-
// gd._fullData, gd._fullLayout:
265-
// are complete descriptions of how to draw the plot,
266-
// use these fields in all required computations.
267-
//
268-
// gd._fullLayout._modules
269-
// is a list of all the trace modules required to draw the plot.
270-
//
271-
// gd._fullLayout._basePlotModules
272-
// is a list of all the plot modules required to draw the plot.
273-
//
274-
// gd._fullLayout._transformModules
275-
// is a list of all the transform modules invoked.
276-
//
277-
plots.supplyDefaults = function(gd, skipCalcUpdate) {
257+
/*
258+
* Fill in default values
259+
* @param {DOM element} gd
260+
* @param {object} opts
261+
* @param {boolean} opts.skipUpdateCalc: normally if the existing gd.calcdata looks
262+
* compatible with the new gd._fullData we finish by linking the new _fullData traces
263+
* to the old gd.calcdata, so it's correctly set if we're not going to recalc. But also,
264+
* if there are calcTransforms on the trace, we first remap data arrays from the old full
265+
* trace into the new one. Use skipUpdateCalc to defer this (needed by Plotly.react)
266+
*
267+
* gd.data, gd.layout:
268+
* are precisely what the user specified,
269+
* these fields shouldn't be modified nor used directly
270+
* after the supply defaults step.
271+
*
272+
* gd._fullData, gd._fullLayout:
273+
* are complete descriptions of how to draw the plot,
274+
* use these fields in all required computations.
275+
*
276+
* gd._fullLayout._modules
277+
* is a list of all the trace modules required to draw the plot.
278+
*
279+
* gd._fullLayout._basePlotModules
280+
* is a list of all the plot modules required to draw the plot.
281+
*
282+
* gd._fullLayout._transformModules
283+
* is a list of all the transform modules invoked.
284+
*
285+
*/
286+
plots.supplyDefaults = function(gd, opts) {
287+
var skipUpdateCalc = opts && opts.skipUpdateCalc;
278288
var oldFullLayout = gd._fullLayout || {};
279289

280290
if(oldFullLayout._skipDefaults) {
@@ -458,7 +468,7 @@ plots.supplyDefaults = function(gd, skipCalcUpdate) {
458468
}
459469

460470
// update object references in calcdata
461-
if(!skipCalcUpdate && oldCalcdata.length === newFullData.length) {
471+
if(!skipUpdateCalc && oldCalcdata.length === newFullData.length) {
462472
plots.supplyDefaultsUpdateCalc(oldCalcdata, newFullData);
463473
}
464474

@@ -471,11 +481,18 @@ plots.supplyDefaultsUpdateCalc = function(oldCalcdata, newFullData) {
471481
var newTrace = newFullData[i];
472482
var cd0 = oldCalcdata[i][0];
473483
if(cd0 && cd0.trace) {
474-
if(cd0.trace._hasCalcTransform) {
475-
remapTransformedArrays(cd0, newTrace);
476-
} else {
477-
cd0.trace = newTrace;
484+
var oldTrace = cd0.trace;
485+
if(oldTrace._hasCalcTransform) {
486+
var arrayAttrs = oldTrace._arrayAttrs;
487+
var j, astr, oldArrayVal;
488+
489+
for(j = 0; j < arrayAttrs.length; j++) {
490+
astr = arrayAttrs[j];
491+
oldArrayVal = Lib.nestedProperty(oldTrace, astr).get().slice();
492+
Lib.nestedProperty(newTrace, astr).set(oldArrayVal);
493+
}
478494
}
495+
cd0.trace = newTrace;
479496
}
480497
}
481498
};
@@ -522,25 +539,6 @@ function emptySubplotLists() {
522539
return out;
523540
}
524541

525-
function remapTransformedArrays(cd0, newTrace) {
526-
var oldTrace = cd0.trace;
527-
var arrayAttrs = oldTrace._arrayAttrs;
528-
var transformedArrayHash = {};
529-
var i, astr;
530-
531-
for(i = 0; i < arrayAttrs.length; i++) {
532-
astr = arrayAttrs[i];
533-
transformedArrayHash[astr] = Lib.nestedProperty(oldTrace, astr).get().slice();
534-
}
535-
536-
cd0.trace = newTrace;
537-
538-
for(i = 0; i < arrayAttrs.length; i++) {
539-
astr = arrayAttrs[i];
540-
Lib.nestedProperty(cd0.trace, astr).set(transformedArrayHash[astr]);
541-
}
542-
}
543-
544542
/**
545543
* getFormatObj: use _context to get the format object from locale.
546544
* Used to get d3.locale argument object and extraFormat argument object

src/traces/box/defaults.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function handleSampleDefaults(traceIn, traceOut, coerce, layout) {
5151
coerce('x0');
5252
len = y.length;
5353
}
54-
} else if(x && x.length) {
54+
} else if(hasX) {
5555
defaultOrientation = 'h';
5656
coerce('y0');
5757
len = x.length;

src/traces/carpet/calc.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'use strict';
1010

1111
var Axes = require('../../plots/cartesian/axes');
12-
var is1D = require('../../lib').is1D;
12+
var isArray1D = require('../../lib').isArray1D;
1313
var cheaterBasis = require('./cheater_basis');
1414
var arrayMinmax = require('./array_minmax');
1515
var calcGridlines = require('./calc_gridlines');
@@ -29,8 +29,8 @@ module.exports = function calc(gd, trace) {
2929
var x = trace.x;
3030
var y = trace.y;
3131
var cols = [];
32-
if(x && is1D(x)) cols.push('x');
33-
if(y && is1D(y)) cols.push('y');
32+
if(x && isArray1D(x)) cols.push('x');
33+
if(y && isArray1D(y)) cols.push('y');
3434

3535
if(cols.length) {
3636
convertColumnData(trace, aax, bax, 'a', 'b', cols);

src/traces/carpet/xy_defaults.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
'use strict';
1111

12-
var is1D = require('../../lib').is1D;
12+
var isArray1D = require('../../lib').isArray1D;
1313

1414
module.exports = function handleXYDefaults(traceIn, traceOut, coerce) {
1515
var x = coerce('x');
@@ -20,7 +20,7 @@ module.exports = function handleXYDefaults(traceIn, traceOut, coerce) {
2020

2121
traceOut._cheater = !x;
2222

23-
if((!hasX || is1D(x)) && (!hasY || is1D(y))) {
23+
if((!hasX || isArray1D(x)) && (!hasY || isArray1D(y))) {
2424
var len = hasX ? x.length : Infinity;
2525
if(hasY) len = Math.min(len, y.length);
2626
if(traceOut.a && traceOut.a.length) len = Math.min(len, traceOut.a.length);

src/traces/contour/defaults.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3535

3636
coerce('text');
3737
var isConstraint = (coerce('contours.type') === 'constraint');
38-
coerce('connectgaps', Lib.is1D(traceOut.z));
38+
coerce('connectgaps', Lib.isArray1D(traceOut.z));
3939

4040
// trace-level showlegend has already been set, but is only allowed if this is a constraint
4141
if(!isConstraint) delete traceOut.showlegend;

src/traces/contourcarpet/calc.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'use strict';
1010

1111
var colorscaleCalc = require('../../components/colorscale/calc');
12-
var is1D = require('../../lib').is1D;
12+
var isArray1D = require('../../lib').isArray1D;
1313

1414
var convertColumnData = require('../heatmap/convert_column_xyz');
1515
var clean2dArray = require('../heatmap/clean_2d_array');
@@ -70,7 +70,7 @@ function heatmappishCalc(gd, trace) {
7070
aax._minDtick = 0;
7171
bax._minDtick = 0;
7272

73-
if(is1D(trace.z)) convertColumnData(trace, aax, bax, 'a', 'b', ['z']);
73+
if(isArray1D(trace.z)) convertColumnData(trace, aax, bax, 'a', 'b', ['z']);
7474
a = trace._a = trace._a || trace.a;
7575
b = trace._b = trace._b || trace.b;
7676

src/traces/contourcarpet/defaults.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
5656
var isConstraint = (coerce('contours.type') === 'constraint');
5757

5858
// Unimplemented:
59-
// coerce('connectgaps', Lib.is1D(traceOut.z));
59+
// coerce('connectgaps', Lib.isArray1D(traceOut.z));
6060

6161
// trace-level showlegend has already been set, but is only allowed if this is a constraint
6262
if(!isConstraint) delete traceOut.showlegend;

src/traces/heatmap/calc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ module.exports = function calc(gd, trace) {
5858
}
5959
else {
6060
var zIn = trace.z;
61-
if(Lib.is1D(zIn)) {
61+
if(Lib.isArray1D(zIn)) {
6262
convertColumnData(trace, xa, ya, 'x', 'y', ['z']);
6363
x = trace._x;
6464
y = trace._y;

src/traces/heatmap/convert_column_xyz.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = function convertColumnData(trace, ax1, ax2, var1Name, var2Name,
1717
var col1 = trace[var1Name].slice(0, colLen);
1818
var col2 = trace[var2Name].slice(0, colLen);
1919
var textCol = trace.text;
20-
var hasColumnText = (textCol !== undefined && Lib.is1D(textCol));
20+
var hasColumnText = (textCol !== undefined && Lib.isArray1D(textCol));
2121
var col1Calendar = trace[var1Name + 'calendar'];
2222
var col2Calendar = trace[var2Name + 'calendar'];
2323

src/traces/heatmap/defaults.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3232

3333
handleStyleDefaults(traceIn, traceOut, coerce, layout);
3434

35-
coerce('connectgaps', Lib.is1D(traceOut.z) && (traceOut.zsmooth !== false));
35+
coerce('connectgaps', Lib.isArray1D(traceOut.z) && (traceOut.zsmooth !== false));
3636

3737
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: '', cLetter: 'z'});
3838
};

src/traces/heatmap/xyz_defaults.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = function handleXYZDefaults(traceIn, traceOut, coerce, layout, x
2222

2323
if(z === undefined || !z.length) return 0;
2424

25-
if(Lib.is1D(traceIn.z)) {
25+
if(Lib.isArray1D(traceIn.z)) {
2626
x = coerce(xName);
2727
y = coerce(yName);
2828

src/traces/mesh3d/defaults.js

+2
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
8888
coerce('text');
8989

9090
// disable 1D transforms
91+
// x/y/z should match lengths, and i/j/k should match as well, but
92+
// the two sets have different lengths so transforms wouldn't work.
9193
traceOut._length = null;
9294
};

0 commit comments

Comments
 (0)