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

fix support for typed arrays in bar 'width' and 'offset' #3169

Merged
merged 2 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions src/traces/bar/cross_trace_calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ function applyAttributes(sieve) {

if(isArrayOrTypedArray(offset)) {
// if offset is an array, then clone it into t.poffset.
newPoffset = offset.slice(0, calcTrace.length);
newPoffset = Array.prototype.slice.call(offset, 0, calcTrace.length);
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved

// guard against non-numeric items
for(j = 0; j < newPoffset.length; j++) {
Expand All @@ -377,12 +377,12 @@ function applyAttributes(sieve) {
t.poffset = offset;
}

var width = fullTrace._width || fullTrace.width,
initialBarwidth = t.barwidth;
var width = fullTrace._width || fullTrace.width;
var initialBarwidth = t.barwidth;

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

// guard against non-numeric items
for(j = 0; j < newBarwidth.length; j++) {
Expand Down
40 changes: 40 additions & 0 deletions test/jasmine/tests/bar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,46 @@ describe('Bar.crossTraceCalc (formerly known as setPositions)', function() {
assertArrayField(cd[2][0], 't.poffset', [-0.4]);
});

it('should guard against invalid width items', function() {
var w = [0.1, 0.4, 0.7];

var gd = mockBarPlot([{
width: w,
y: [1, 2, 3]
}, {
width: new Float32Array(w),
y: [1, 2, 3]
}]);

var cd = gd.calcdata;
assertArrayField(cd[0][0], 't.barwidth', w);
assertArrayField(cd[1][0], 't.barwidth', w);
assertPointField(cd, 'x', [
[-0.2, 0.8, 1.8],
[0.2, 1.2, 2.2]
]);
});

it('should work with width typed arrays', function() {
var o = [0.1, 0.4, 0.7];

var gd = mockBarPlot([{
offset: o,
y: [1, 2, 3]
}, {
offset: new Float32Array(o),
y: [1, 2, 3]
}]);

var cd = gd.calcdata;
assertArrayField(cd[0][0], 't.poffset', o);
assertArrayField(cd[1][0], 't.poffset', o);
assertPointField(cd, 'x', [
[0.5, 1.8, 3.1],
[0.5, 1.8, 3.099]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Huh, took me a while to figure out where these numbers came from... surprised I had never noticed this before but it seems weird that offset sets the left edge of each bar rather than its center. Oh well, a little late to change that. Why does the second one get 3.099 instead of 3.1?

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'm not sure.

With a regular array, we get:

[0.5, 1.7999999999999998, 3.1]

with a typed array, we get:

[0.5000000014901161, 1.8000000059604644, 3.099999988079071]

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah ok, that's just because it's a Float32Array ie single precision, not double. But it's still closer to 3.1 than to 3.099 😏

]);
});

it('should guard against invalid width items', function() {
var gd = mockBarPlot([{
width: [null, 1, 0.8],
Expand Down