Skip to content

Filter numbers during calc step #4063

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

Merged
merged 3 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ lib.mergeArray = function(traceAttr, cd, cdAttr, fn) {
lib.mergeArrayCastPositive = function(traceAttr, cd, cdAttr) {
return lib.mergeArray(traceAttr, cd, cdAttr, function(v) {
var w = +v;
return w > 0 ? w : 0;
return isNaN(w) ? NaN : w > 0 ? w : 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you want isNumeric here. Have tried something like

'marker.line.width': [NaN, null, undefined, [], {}, 0]`

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Works OK with those cases. Using isNumeric might be slow.

Copy link
Contributor

Choose a reason for hiding this comment

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

Have you tried running benchmarks here? I doubt that isNumeric is that much slower than isNaN

Copy link
Contributor

Choose a reason for hiding this comment

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

Moreover, could we lock those cases down in a _module.calc test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tests are added in 39b9840.

});
};

Expand Down
14 changes: 7 additions & 7 deletions src/traces/funnel/arrays_to_calcdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@

'use strict';

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

// arrayOk attributes, merge them into calcdata array
module.exports = function arraysToCalcdata(cd, trace) {
for(var i = 0; i < cd.length; i++) cd[i].i = i;

mergeArray(trace.text, cd, 'tx');
mergeArray(trace.hovertext, cd, 'htx');
Lib.mergeArray(trace.text, cd, 'tx');
Lib.mergeArray(trace.hovertext, cd, 'htx');

var marker = trace.marker;
if(marker) {
mergeArray(marker.opacity, cd, 'mo');
mergeArray(marker.color, cd, 'mc');
Lib.mergeArray(marker.opacity, cd, 'mo');
Lib.mergeArray(marker.color, cd, 'mc');

var markerLine = marker.line;
if(markerLine) {
mergeArray(markerLine.color, cd, 'mlc');
mergeArray(markerLine.width, cd, 'mlw');
Lib.mergeArray(markerLine.color, cd, 'mlc');
Lib.mergeArrayCastPositive(markerLine.width, cd, 'mlw');
}
}
};
8 changes: 4 additions & 4 deletions src/traces/scatter/arrays_to_calcdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ module.exports = function arraysToCalcdata(cd, trace) {
Lib.mergeArray(trace.customdata, cd, 'data');
Lib.mergeArray(trace.textposition, cd, 'tp');
if(trace.textfont) {
Lib.mergeArray(trace.textfont.size, cd, 'ts');
Lib.mergeArrayCastPositive(trace.textfont.size, cd, 'ts');
Lib.mergeArray(trace.textfont.color, cd, 'tc');
Lib.mergeArray(trace.textfont.family, cd, 'tf');
}

var marker = trace.marker;
if(marker) {
Lib.mergeArray(marker.size, cd, 'ms');
Lib.mergeArray(marker.opacity, cd, 'mo');
Lib.mergeArrayCastPositive(marker.size, cd, 'ms');
Lib.mergeArrayCastPositive(marker.opacity, cd, 'mo');
Lib.mergeArray(marker.symbol, cd, 'mx');
Lib.mergeArray(marker.color, cd, 'mc');

var markerLine = marker.line;
if(marker.line) {
Lib.mergeArray(markerLine.color, cd, 'mlc');
Lib.mergeArray(markerLine.width, cd, 'mlw');
Lib.mergeArrayCastPositive(markerLine.width, cd, 'mlw');
}

var markerGradient = marker.gradient;
Expand Down
14 changes: 14 additions & 0 deletions test/jasmine/tests/bar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,20 @@ describe('Bar.calc', function() {
assertPointField(cd, 'x', [[1, NaN, NaN, 15]]);
assertPointField(cd, 'y', [[1, 2, 10, 30]]);
});

it('should guard against negative marker.line.width values', function() {
var gd = mockBarPlot([{
marker: {
line: {
width: [2, 1, 0, -1, false, true, null, [], -Infinity, Infinity, NaN, {}]
}
},
y: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
}], {});

var cd = gd.calcdata;
assertPointField(cd, 'mlw', [[2, 1, 0, 0, 0, 1, 0, 0, 0, Infinity, NaN, NaN]]);
Copy link
Contributor

@etpinard etpinard Jul 23, 2019

Choose a reason for hiding this comment

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

Does having NaN in calcdata[i][j].mlw lead to console errors?

Copy link
Contributor

Choose a reason for hiding this comment

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

console errors e.g. when drawing a svg path?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't notice any:
Scatter codepen
Bar codepen

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But that's a very good question.
Now I am wondering if we should patch this test instead of the lib.mergeArrayCastPositive to accept 0 instead of NaN?
shouldCheckForNaN

Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't notice any:

Ok, but looks like marker pts with mlw = NaN render as if they had mlw = 0, so I would prefer making NaN cast to 0 during calc.

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. Done in f9db523 and fixed scattegeo test.

});
});

describe('Bar.crossTraceCalc (formerly known as setPositions)', function() {
Expand Down
28 changes: 28 additions & 0 deletions test/jasmine/tests/funnel_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,34 @@ describe('Funnel.calc', function() {
assertPointField(cd, 'y', [[1, NaN, NaN, 15]]);
assertPointField(cd, 'x', [[0.5, 1, 5, 15]]);
});

it('should guard against negative marker.line.width values', function() {
var gd = mockFunnelPlot([{
marker: {
line: {
width: [2, 1, 0, -1, false, true, null, [], -Infinity, Infinity, NaN, {}]
}
},
y: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
}], {});

var cd = gd.calcdata;
assertPointField(cd, 'mlw', [[2, 1, 0, 0, 0, 1, 0, 0, 0, Infinity, NaN, NaN]]);
});

it('should guard against negative marker.line.width values', function() {
var gd = mockFunnelPlot([{
marker: {
line: {
width: [2, 1, 0, -1, false, true, null, [], -Infinity, Infinity, NaN, {}]
}
},
y: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
}], {});

var cd = gd.calcdata;
assertPointField(cd, 'mlw', [[2, 1, 0, 0, 0, 1, 0, 0, 0, Infinity, NaN, NaN]]);
});
});

describe('Funnel.crossTraceCalc', function() {
Expand Down
52 changes: 52 additions & 0 deletions test/jasmine/tests/scatter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var Scatter = require('@src/traces/scatter');
var makeBubbleSizeFn = require('@src/traces/scatter/make_bubble_size_func');
var linePoints = require('@src/traces/scatter/line_points');
var Lib = require('@src/lib');
var Plots = require('@src/plots/plots');

var Plotly = require('@lib/index');
var createGraphDiv = require('../assets/create_graph_div');
Expand All @@ -18,6 +19,8 @@ var assertMultiNodeOrder = customAssertions.assertMultiNodeOrder;
var checkEventData = require('../assets/check_event_data');
var constants = require('@src/traces/scatter/constants');

var supplyAllDefaults = require('../assets/supply_defaults');

var getOpacity = function(node) { return Number(node.style.opacity); };
var getFillOpacity = function(node) { return Number(node.style['fill-opacity']); };
var getColor = function(node) { return node.style.fill; };
Expand Down Expand Up @@ -273,6 +276,55 @@ describe('Test scatter', function() {
});
});

describe('calc', function() {
function assertPointField(calcData, prop, expectation) {
var values = [];

calcData.forEach(function(calcTrace) {
var vals = calcTrace.map(function(pt) {
return Lib.nestedProperty(pt, prop).get();
});

values.push(vals);
});

expect(values).toBeCloseTo2DArray(expectation, undefined, '(field ' + prop + ')');
}

it('should guard against negative size values', function() {
var gd = {
data: [{
type: 'scatter',
mode: 'markers+text',
marker: {
line: {
width: [2, 1, 0, -1, false, true, null, [], -Infinity, Infinity, NaN, {}]
},
opacity: [2, 1, 0, -1, false, true, null, [], -Infinity, Infinity, NaN, {}],
size: [2, 1, 0, -1, false, true, null, [], -Infinity, Infinity, NaN, {}]
},
textfont: {
size: [2, 1, 0, -1, false, true, null, [], -Infinity, Infinity, NaN, {}]
},
text: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
y: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
}],
layout: {},
calcdata: [],
_context: {locale: 'en', locales: {}}
};

supplyAllDefaults(gd);
Plots.doCalcdata(gd);

var cd = gd.calcdata;
assertPointField(cd, 'mlw', [[2, 1, 0, 0, 0, 1, 0, 0, 0, Infinity, NaN, NaN]]);
assertPointField(cd, 'mo', [[2, 1, 0, 0, 0, 1, 0, 0, 0, Infinity, NaN, NaN]]);
assertPointField(cd, 'ms', [[2, 1, 0, 0, 0, 1, 0, 0, 0, Infinity, NaN, NaN]]);
assertPointField(cd, 'ts', [[2, 1, 0, 0, 0, 1, 0, 0, 0, Infinity, NaN, NaN]]);
});
});

describe('isBubble', function() {
it('should return true when marker.size is an Array', function() {
var trace = {
Expand Down