Skip to content
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

Typed arrays support #2388

Merged
merged 28 commits into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
91b03ff
rename Lib.isArray -> Lib.isArrayOrTypedArray
etpinard Feb 20, 2018
a25ff13
accept typed array when coercing 'data_array' attributes
etpinard Feb 20, 2018
45c2f35
[PoC] bypass ax.d2c for typedArray during ax.makeCalcdata
etpinard Feb 20, 2018
2d81bdc
replace Array.isArray -> Lib.isArrayOrTypedArray in various places
etpinard Feb 20, 2018
bcf59d7
replace Lib.isArray -> Array.isArray in tests w/o typed arrays
etpinard Feb 20, 2018
bc32981
[wip] a few todos
etpinard Feb 20, 2018
2372629
Merge branch 'master' into typed-arrays-support
etpinard Feb 22, 2018
909120e
make scattergl dragmode relayout replot not recalc
etpinard Feb 26, 2018
5316c47
large lint commit in scattergl/index.js
etpinard Feb 26, 2018
0aa0f5e
improvements to Scattergl.calc
etpinard Feb 26, 2018
39ef5a9
update baselines
etpinard Feb 26, 2018
36b4e25
update jasmine tests for new scattergl auto-ranges
etpinard Feb 26, 2018
40f93f7
add svg vs gl scatter autorange :lock:s
etpinard Feb 26, 2018
d98dcc0
improve makeCalcdata typed array handling + some tests + some linting
etpinard Feb 26, 2018
c0e2f73
a few more Array.isArray -> Lib.isArrayOrTypedArray
etpinard Feb 26, 2018
09d37b6
some typed array tests
etpinard Feb 26, 2018
a7ed2c2
:lock: Plotly.restyle support for typed arrays
etpinard Feb 28, 2018
b95e462
add and :lock: typed array support for extendTraces and prependTraces
etpinard Feb 28, 2018
53ea0ec
Merge branch 'master' into typed-arrays-support
etpinard Feb 28, 2018
98d2407
use c2l instead of d2l
etpinard Feb 28, 2018
01a8443
:hocho: TODOs
etpinard Feb 28, 2018
c15722f
add mock :lock:ing down cleanNumber for scattergl
etpinard Feb 28, 2018
a2fb88b
Merge pull request #2404 from plotly/scattergl-autorange
etpinard Feb 28, 2018
f0395b5
improve isTypedArray
etpinard Feb 28, 2018
9b83826
don't require all of Lib when only isArrayOrTypedArray is needed
etpinard Feb 28, 2018
6dd2f69
turn coord typed arrays into plain array for 'date' and 'category axes
etpinard Feb 28, 2018
306986d
check that typed arrays items aren't all NaNs in hasColorscale
etpinard Feb 28, 2018
d4cb0c4
no need to check for typed array in outer array of a 2d array
etpinard Feb 28, 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
6 changes: 3 additions & 3 deletions src/lib/coerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ exports.coerce = function(containerIn, containerOut, attributes, attribute, dflt
* individual form (eg. some array vals can be numbers, even if the
* single values must be color strings)
*/
if(opts.arrayOk && Array.isArray(v)) {
if(opts.arrayOk && isArrayOrTypedArray(v)) {
propOut.set(v);
return v;
}
Expand Down Expand Up @@ -419,7 +419,7 @@ exports.coerceSelectionMarkerOpacity = function(traceOut, coerce) {
//
// Only give [un]selected.marker.opacity a default value if you don't
// set any other [un]selected attributes.
if(!Array.isArray(mo) && !traceOut.selected && !traceOut.unselected) {
if(!isArrayOrTypedArray(mo) && !traceOut.selected && !traceOut.unselected) {
smoDflt = mo;
usmoDflt = DESELECTDIM * mo;
}
Expand All @@ -431,7 +431,7 @@ exports.coerceSelectionMarkerOpacity = function(traceOut, coerce) {
exports.validate = function(value, opts) {
var valObjectDef = exports.valObjectMeta[opts.valType];

if(opts.arrayOk && Array.isArray(value)) return true;
if(opts.arrayOk && isArrayOrTypedArray(value)) return true;

if(valObjectDef.validateFunction) {
return valObjectDef.validateFunction(value, opts);
Expand Down
7 changes: 4 additions & 3 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ lib.noneOrAll = function(containerIn, containerOut, attrList) {
* @param {string} cdAttr : calcdata key
*/
lib.mergeArray = function(traceAttr, cd, cdAttr) {
if(Array.isArray(traceAttr)) {
if(lib.isArrayOrTypedArray(traceAttr)) {
var imax = Math.min(traceAttr.length, cd.length);
for(var i = 0; i < imax; i++) cd[i][cdAttr] = traceAttr[i];
}
Expand All @@ -411,7 +411,7 @@ lib.mergeArray = function(traceAttr, cd, cdAttr) {
lib.fillArray = function(traceAttr, cd, cdAttr, fn) {
fn = fn || lib.identity;

if(Array.isArray(traceAttr)) {
if(lib.isArrayOrTypedArray(traceAttr)) {
for(var i = 0; i < cd.length; i++) {
cd[i][cdAttr] = fn(traceAttr[i]);
}
Expand All @@ -432,7 +432,8 @@ lib.castOption = function(trace, ptNumber, astr, fn) {

var val = lib.nestedProperty(trace, astr).get();

if(Array.isArray(val)) {
if(lib.isArrayOrTypedArray(val)) {
// TODO what to do with 2d arrays?
if(Array.isArray(ptNumber) && Array.isArray(val[ptNumber[0]])) {
return fn(val[ptNumber[0]][ptNumber[1]]);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/plot_api/plot_schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ exports.findArrayAttributes = function(trace) {

var astr = toAttrString(stack);
var val = Lib.nestedProperty(trace, astr).get();
if(!Array.isArray(val)) return;
if(!Lib.isArrayOrTypedArray(val)) return;

arrayAttributes.push(astr);
}
Expand Down
6 changes: 2 additions & 4 deletions src/plot_api/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@
* LICENSE file in the root directory of this source tree.
*/


'use strict';


var Lib = require('../lib');
var Plots = require('../plots/plots');
var PlotSchema = require('./plot_schema');
var dfltConfig = require('./plot_config');

var isPlainObject = Lib.isPlainObject;
var isArray = Array.isArray;

var isArrayOrTypedArray = Lib.isArrayOrTypedArray;

/**
* Validate a data array and layout object.
Expand Down Expand Up @@ -211,7 +209,7 @@ function crawl(objIn, objOut, schema, list, base, path) {
else if(!isPlainObject(valIn) && isPlainObject(valOut)) {
list.push(format('object', base, p, valIn));
}
else if(!isArray(valIn) && isArray(valOut) && !isInfoArray && !isColorscale) {
else if(!isArrayOrTypedArray(valIn) && isArrayOrTypedArray(valOut) && !isInfoArray && !isColorscale) {
list.push(format('array', base, p, valIn));
}
else if(!(k in objOut)) {
Expand Down
4 changes: 2 additions & 2 deletions src/plots/gl3d/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,14 @@ var axisProperties = [ 'xaxis', 'yaxis', 'zaxis' ];

function coordinateBound(axis, coord, len, d, bounds, calendar) {
var x;
if(!Array.isArray(coord)) {
if(!Lib.isArrayOrTypedArray(coord)) {
bounds[0][d] = Math.min(bounds[0][d], 0);
bounds[1][d] = Math.max(bounds[1][d], len - 1);
return;
}

for(var i = 0; i < (len || coord.length); ++i) {
if(Array.isArray(coord[i])) {
if(Lib.isArrayOrTypedArray(coord[i])) {
for(var j = 0; j < coord[i].length; ++j) {
x = axis.d2l(coord[i][j], 0, calendar);
if(!isNaN(x) && isFinite(x)) {
Expand Down
3 changes: 1 addition & 2 deletions src/plots/mapbox/convert_text_opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

var Lib = require('../../lib');


/**
* Convert plotly.js 'textposition' to mapbox-gl 'anchor' and 'offset'
* (with the help of the icon size).
Expand All @@ -29,7 +28,7 @@ module.exports = function convertTextOpts(textposition, iconSize) {
hPos = parts[1];

// ballpack values
var factor = Array.isArray(iconSize) ? Lib.mean(iconSize) : iconSize,
var factor = Lib.isArrayOrTypedArray(iconSize) ? Lib.mean(iconSize) : iconSize,
xInc = 0.5 + (factor / 100),
yInc = 1.5 + (factor / 100);

Expand Down
3 changes: 2 additions & 1 deletion src/traces/bar/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

var isNumeric = require('fast-isnumeric');

var Lib = require('../../lib');
Copy link
Collaborator

Choose a reason for hiding this comment

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

you go back and forth between this and isArrayOrTypedArray = require('../../lib').isArrayOrTypedArray; - not a big deal but the latter seems marginally preferable to me when you don't need anything else from Lib.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cleaned up in 9b83826

var Axes = require('../../plots/cartesian/axes');
var hasColorscale = require('../../components/colorscale/has_colorscale');
var colorscaleCalc = require('../../components/colorscale/calc');
Expand Down Expand Up @@ -63,7 +64,7 @@ module.exports = function calc(gd, trace) {
var base = trace.base,
b;

if(Array.isArray(base)) {
if(Lib.isArrayOrTypedArray(base)) {
for(i = 0; i < Math.min(base.length, cd.length); i++) {
b = sa.d2c(base[i], 0, scalendar);
if(isNumeric(b)) {
Expand Down
5 changes: 3 additions & 2 deletions src/traces/bar/set_positions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
var isNumeric = require('fast-isnumeric');
var BADNUM = require('../../constants/numerical').BADNUM;

var Lib = require('../../lib');
var Registry = require('../../registry');
var Axes = require('../../plots/cartesian/axes');
var Sieve = require('./sieve.js');
Expand Down Expand Up @@ -311,7 +312,7 @@ function applyAttributes(sieve) {
initialPoffset = t.poffset,
newPoffset;

if(Array.isArray(offset)) {
if(Lib.isArrayOrTypedArray(offset)) {
// if offset is an array, then clone it into t.poffset.
newPoffset = offset.slice(0, calcTrace.length);

Expand All @@ -337,7 +338,7 @@ function applyAttributes(sieve) {
var width = fullTrace.width,
initialBarwidth = t.barwidth;

if(Array.isArray(width)) {
if(Lib.isArrayOrTypedArray(width)) {
// if width is an array, then clone it into t.barwidth.
var newBarwidth = width.slice(0, calcTrace.length);

Expand Down
2 changes: 1 addition & 1 deletion src/traces/box/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function arraysToCalcdata(pt, trace, i) {
}

function calcSelection(cd, trace) {
if(Array.isArray(trace.selectedpoints)) {
if(Lib.isArrayOrTypedArray(trace.selectedpoints)) {
for(var i = 0; i < cd.length; i++) {
var pts = cd[i].pts || [];
var ptNumber2cdIndex = {};
Expand Down
6 changes: 4 additions & 2 deletions src/traces/carpet/array_minmax.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

'use strict';

var isArrayOrTypedArray = require('../../lib').isArrayOrTypedArray;

module.exports = function(a) {
return minMax(a, 0);
};
Expand All @@ -16,7 +18,7 @@ function minMax(a, depth) {
// Limit to ten dimensional datasets. This seems *exceedingly* unlikely to
// ever cause problems or even be a concern. It's include strictly so that
// circular arrays could never cause this to loop.
if(!Array.isArray(a) || depth >= 10) {
if(!isArrayOrTypedArray(a) || depth >= 10) {
return null;
}

Expand All @@ -26,7 +28,7 @@ function minMax(a, depth) {
for(var i = 0; i < n; i++) {
var datum = a[i];

if(Array.isArray(datum)) {
if(isArrayOrTypedArray(datum)) {
var result = minMax(datum, depth + 1);

if(result) {
Expand Down
4 changes: 3 additions & 1 deletion src/traces/carpet/axis_aligned_line.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

'use strict';

var isArrayOrTypedArray = require('../../lib').isArrayOrTypedArray;

/* This function retrns a set of control points that define a curve aligned along
* either the a or b axis. Exactly one of a or b must be an array defining the range
* spanned.
Expand All @@ -19,7 +21,7 @@ module.exports = function(carpet, carpetcd, a, b) {
var idx, tangent, tanIsoIdx, tanIsoPar, segment, refidx;
var p0, p1, v0, v1, start, end, range;

var axis = Array.isArray(a) ? 'a' : 'b';
var axis = isArrayOrTypedArray(a) ? 'a' : 'b';
var ax = axis === 'a' ? carpet.aaxis : carpet.baxis;
var smoothing = ax.smoothing;
var toIdx = axis === 'a' ? carpet.a2i : carpet.b2j;
Expand Down
5 changes: 3 additions & 2 deletions src/traces/carpet/has_columns.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var isArrayOrTypedArray = require('../../lib').isArrayOrTypedArray;

module.exports = function(data) {
return Array.isArray(data[0]);
return isArrayOrTypedArray(data[0]);
};
4 changes: 3 additions & 1 deletion src/traces/carpet/map_1d_array.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

'use strict';

var isArrayOrTypedArray = require('../../lib').isArrayOrTypedArray;

/*
* Map an array of x or y coordinates (c) to screen-space pixel coordinates (p).
* The output array is optional, but if provided, it will be reused without
Expand All @@ -16,7 +18,7 @@
module.exports = function mapArray(out, data, func) {
var i;

if(!Array.isArray(out)) {
if(!isArrayOrTypedArray(out)) {
// If not an array, make it an array:
out = [];
} else if(out.length > data.length) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

⚠️ this block has a .slice ⚠️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah great. So this is fine, but I guess 🐎 at some point we could make a helper that uses slice on regular arrays and subarray on typed arrays, for cases like this where we don't need a copy.

Expand Down
6 changes: 4 additions & 2 deletions src/traces/carpet/map_2d_array.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

'use strict';

var isArrayOrTypedArray = require('../../lib').isArrayOrTypedArray;
Copy link
Collaborator

Choose a reason for hiding this comment

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

this file is map_2d_array - and we're not supporting typed 2D arrays, at least not yet, right?

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 eyes. Only the inner arrays should be allowed to be typed arrays.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in d4cb0c4


/*
* Map an array of x or y coordinates (c) to screen-space pixel coordinates (p).
* The output array is optional, but if provided, it will be reused without
Expand All @@ -16,7 +18,7 @@
module.exports = function mapArray(out, data, func) {
var i, j;

if(!Array.isArray(out)) {
if(!isArrayOrTypedArray(out)) {
// If not an array, make it an array:
out = [];
} else if(out.length > data.length) {
Expand All @@ -26,7 +28,7 @@ module.exports = function mapArray(out, data, func) {
}

for(i = 0; i < data.length; i++) {
if(!Array.isArray(out[i])) {
if(!isArrayOrTypedArray(out[i])) {
// If not an array, make it an array:
out[i] = [];
} else if(out[i].length > data.length) {
Expand Down
2 changes: 1 addition & 1 deletion src/traces/choropleth/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
}

var z = coerce('z');
if(!Array.isArray(z)) {
if(!Lib.isArrayOrTypedArray(z)) {
traceOut.visible = false;
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/traces/histogram/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ module.exports = function calc(gd, trace) {
var pr2c = function(v) { return pa.r2c(v, 0, calendar); };
var rawCounterData;

if(Array.isArray(trace[counterData]) && func !== 'count') {
if(Lib.isArrayOrTypedArray(trace[counterData]) && func !== 'count') {
rawCounterData = trace[counterData];
isAvg = func === 'avg';
binFunc = binFunctions[func];
Expand Down Expand Up @@ -199,7 +199,7 @@ module.exports = function calc(gd, trace) {

arraysToCalcdata(cd, trace);

if(Array.isArray(trace.selectedpoints)) {
if(Lib.isArrayOrTypedArray(trace.selectedpoints)) {
Lib.tagSelected(cd, trace, ptNumber2cdIndex);
}

Expand Down
2 changes: 1 addition & 1 deletion src/traces/mesh3d/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
var ret = array.map(function(attr) {
var result = coerce(attr);

if(result && Array.isArray(result)) return result;
if(result && Lib.isArrayOrTypedArray(result)) return result;
return null;
});

Expand Down
3 changes: 2 additions & 1 deletion src/traces/pie/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
var isNumeric = require('fast-isnumeric');
var tinycolor = require('tinycolor2');

var Lib = require('../../lib');
var Color = require('../../components/color');
var helpers = require('./helpers');

module.exports = function calc(gd, trace) {
var vals = trace.values;
var hasVals = Array.isArray(vals) && vals.length;
var hasVals = Lib.isArrayOrTypedArray(vals) && vals.length;
var labels = trace.labels;
var colors = trace.marker.colors || [];
var cd = [];
Expand Down
2 changes: 1 addition & 1 deletion src/traces/pie/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
var vals = coerce('values');
var labels = coerce('labels');
if(!Array.isArray(labels)) {
if(!Array.isArray(vals) || !vals.length) {
if(!Lib.isArrayOrTypedArray(vals) || !vals.length) {
// must have at least one of vals or labels
traceOut.visible = false;
return;
Expand Down
4 changes: 2 additions & 2 deletions src/traces/scatter/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var isNumeric = require('fast-isnumeric');

var Lib = require('../../lib');
var Axes = require('../../plots/cartesian/axes');
var BADNUM = require('../../constants/numerical').BADNUM;

Expand Down Expand Up @@ -111,7 +111,7 @@ function calcMarkerSize(trace, serieslen) {
};
}

if(Array.isArray(marker.size)) {
if(Lib.isArrayOrTypedArray(marker.size)) {
// I tried auto-type but category and dates dont make much sense.
var ax = {type: 'linear'};
Axes.setConvert(ax);
Expand Down
2 changes: 1 addition & 1 deletion src/traces/scatter/calc_selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
var Lib = require('../../lib');

module.exports = function calcSelection(cd, trace) {
if(Array.isArray(trace.selectedpoints)) {
if(Lib.isArrayOrTypedArray(trace.selectedpoints)) {
Lib.tagSelected(cd, trace);
}
};
6 changes: 3 additions & 3 deletions src/traces/scatter/fillcolor_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'use strict';

var Color = require('../../components/color');

var Lib = require('../../lib');

module.exports = function fillColorDefaults(traceIn, traceOut, defaultColor, coerce) {
var inheritColorFromMarker = false;
Expand All @@ -20,10 +20,10 @@ module.exports = function fillColorDefaults(traceIn, traceOut, defaultColor, coe
var markerColor = traceOut.marker.color,
markerLineColor = (traceOut.marker.line || {}).color;

if(markerColor && !Array.isArray(markerColor)) {
if(markerColor && !Lib.isArrayOrTypedArray(markerColor)) {
inheritColorFromMarker = markerColor;
}
else if(markerLineColor && !Array.isArray(markerLineColor)) {
else if(markerLineColor && !Lib.isArrayOrTypedArray(markerLineColor)) {
inheritColorFromMarker = markerLineColor;
}
}
Expand Down
Loading