Skip to content

Commit d0ee187

Browse files
authored
Merge pull request #2995 from plotly/graphJson-for-typed-arrays
Convert typed array to 'regular' arrays before sendToCloud
2 parents cbbba35 + 9d114c8 commit d0ee187

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

src/plots/plots.js

+4
Original file line numberDiff line numberDiff line change
@@ -1894,6 +1894,10 @@ plots.graphJson = function(gd, dataonly, mode, output, useDefaults) {
18941894
return d.map(stripObj);
18951895
}
18961896

1897+
if(Lib.isTypedArray(d)) {
1898+
return Lib.simpleMap(d, Lib.identity);
1899+
}
1900+
18971901
// convert native dates to date strings...
18981902
// mostly for external users exporting to plotly
18991903
if(Lib.isJSDate(d)) return Lib.ms2DateTimeLocal(+d);

test/jasmine/tests/plots_test.js

+40-5
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,13 @@ describe('Test Plots', function() {
500500
});
501501

502502
describe('Plots.graphJson', function() {
503+
var gd;
504+
505+
beforeEach(function() {
506+
gd = createGraphDiv();
507+
});
508+
509+
afterEach(destroyGraphDiv);
503510

504511
it('should serialize data, layout and frames', function(done) {
505512
var mock = {
@@ -533,7 +540,7 @@ describe('Test Plots', function() {
533540
}]
534541
};
535542

536-
Plotly.plot(createGraphDiv(), mock).then(function(gd) {
543+
Plotly.plot(gd, mock).then(function() {
537544
var str = Plots.graphJson(gd, false, 'keepdata');
538545
var obj = JSON.parse(str);
539546

@@ -547,10 +554,38 @@ describe('Test Plots', function() {
547554
name: 'garbage'
548555
});
549556
})
550-
.then(function() {
551-
destroyGraphDiv();
552-
done();
553-
});
557+
.catch(failTest)
558+
.then(done);
559+
});
560+
561+
it('should convert typed arrays to regular arrays', function(done) {
562+
var trace = {
563+
x: new Float32Array([1, 2, 3]),
564+
y: new Float32Array([1, 2, 1]),
565+
marker: {
566+
size: new Float32Array([20, 30, 10]),
567+
color: new Float32Array([10, 30, 20]),
568+
cmin: 10,
569+
cmax: 30,
570+
colorscale: [
571+
[0, 'rgb(255, 0, 0)'],
572+
[0.5, 'rgb(0, 255, 0)'],
573+
[1, 'rgb(0, 0, 255)']
574+
]
575+
}
576+
};
577+
578+
Plotly.plot(gd, [trace]).then(function() {
579+
var str = Plots.graphJson(gd, false, 'keepdata');
580+
var obj = JSON.parse(str);
581+
582+
expect(obj.data[0].x).toEqual([1, 2, 3]);
583+
expect(obj.data[0].y).toEqual([1, 2, 1]);
584+
expect(obj.data[0].marker.size).toEqual([20, 30, 10]);
585+
expect(obj.data[0].marker.color).toEqual([10, 30, 20]);
586+
})
587+
.catch(failTest)
588+
.then(done);
554589
});
555590
});
556591

0 commit comments

Comments
 (0)