Skip to content

Commit a04b13d

Browse files
authored
Merge pull request #3169 from plotly/bar-width-offset-typed-arrays
fix support for typed arrays in bar 'width' and 'offset'
2 parents 9e5c70c + 5f4672c commit a04b13d

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

Diff for: src/traces/bar/cross_trace_calc.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ function applyAttributes(sieve) {
356356

357357
if(isArrayOrTypedArray(offset)) {
358358
// if offset is an array, then clone it into t.poffset.
359-
newPoffset = offset.slice(0, calcTrace.length);
359+
newPoffset = Array.prototype.slice.call(offset, 0, calcTrace.length);
360360

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

380-
var width = fullTrace._width || fullTrace.width,
381-
initialBarwidth = t.barwidth;
380+
var width = fullTrace._width || fullTrace.width;
381+
var initialBarwidth = t.barwidth;
382382

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

387387
// guard against non-numeric items
388388
for(j = 0; j < newBarwidth.length; j++) {

Diff for: test/jasmine/tests/bar_test.js

+40
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,46 @@ describe('Bar.crossTraceCalc (formerly known as setPositions)', function() {
397397
assertArrayField(cd[2][0], 't.poffset', [-0.4]);
398398
});
399399

400+
it('should work with *width* typed arrays', function() {
401+
var w = [0.1, 0.4, 0.7];
402+
403+
var gd = mockBarPlot([{
404+
width: w,
405+
y: [1, 2, 3]
406+
}, {
407+
width: new Float32Array(w),
408+
y: [1, 2, 3]
409+
}]);
410+
411+
var cd = gd.calcdata;
412+
assertArrayField(cd[0][0], 't.barwidth', w);
413+
assertArrayField(cd[1][0], 't.barwidth', w);
414+
assertPointField(cd, 'x', [
415+
[-0.2, 0.8, 1.8],
416+
[0.2, 1.2, 2.2]
417+
]);
418+
});
419+
420+
it('should work with *offset* typed arrays', function() {
421+
var o = [0.1, 0.4, 0.7];
422+
423+
var gd = mockBarPlot([{
424+
offset: o,
425+
y: [1, 2, 3]
426+
}, {
427+
offset: new Float32Array(o),
428+
y: [1, 2, 3]
429+
}]);
430+
431+
var cd = gd.calcdata;
432+
assertArrayField(cd[0][0], 't.poffset', o);
433+
assertArrayField(cd[1][0], 't.poffset', o);
434+
assertPointField(cd, 'x', [
435+
[0.5, 1.8, 3.1],
436+
[0.5, 1.8, 3.099]
437+
]);
438+
});
439+
400440
it('should guard against invalid width items', function() {
401441
var gd = mockBarPlot([{
402442
width: [null, 1, 0.8],

0 commit comments

Comments
 (0)