From 9d114c80bc0f82f3a18c10e8b0f096e1be9ed720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Tue, 11 Sep 2018 12:05:58 -0400 Subject: [PATCH] convert typed array to 'regular' arrays before sendToCloud - as JSON.stringify(new Float32Array([1,2,3])) gives: "{"0":1,"1":2,"2":3}" - use Lib.simpleMap as opposed to Array.prototype.slice.call(d) to avoid potential 'exceed stack size' errors --- src/plots/plots.js | 4 +++ test/jasmine/tests/plots_test.js | 45 ++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/plots/plots.js b/src/plots/plots.js index da202ed8005..4cc227fb3cb 100644 --- a/src/plots/plots.js +++ b/src/plots/plots.js @@ -1894,6 +1894,10 @@ plots.graphJson = function(gd, dataonly, mode, output, useDefaults) { return d.map(stripObj); } + if(Lib.isTypedArray(d)) { + return Lib.simpleMap(d, Lib.identity); + } + // convert native dates to date strings... // mostly for external users exporting to plotly if(Lib.isJSDate(d)) return Lib.ms2DateTimeLocal(+d); diff --git a/test/jasmine/tests/plots_test.js b/test/jasmine/tests/plots_test.js index 4b5139bf818..ede60e8b130 100644 --- a/test/jasmine/tests/plots_test.js +++ b/test/jasmine/tests/plots_test.js @@ -500,6 +500,13 @@ describe('Test Plots', function() { }); describe('Plots.graphJson', function() { + var gd; + + beforeEach(function() { + gd = createGraphDiv(); + }); + + afterEach(destroyGraphDiv); it('should serialize data, layout and frames', function(done) { var mock = { @@ -533,7 +540,7 @@ describe('Test Plots', function() { }] }; - Plotly.plot(createGraphDiv(), mock).then(function(gd) { + Plotly.plot(gd, mock).then(function() { var str = Plots.graphJson(gd, false, 'keepdata'); var obj = JSON.parse(str); @@ -547,10 +554,38 @@ describe('Test Plots', function() { name: 'garbage' }); }) - .then(function() { - destroyGraphDiv(); - done(); - }); + .catch(failTest) + .then(done); + }); + + it('should convert typed arrays to regular arrays', function(done) { + var trace = { + x: new Float32Array([1, 2, 3]), + y: new Float32Array([1, 2, 1]), + marker: { + size: new Float32Array([20, 30, 10]), + color: new Float32Array([10, 30, 20]), + cmin: 10, + cmax: 30, + colorscale: [ + [0, 'rgb(255, 0, 0)'], + [0.5, 'rgb(0, 255, 0)'], + [1, 'rgb(0, 0, 255)'] + ] + } + }; + + Plotly.plot(gd, [trace]).then(function() { + var str = Plots.graphJson(gd, false, 'keepdata'); + var obj = JSON.parse(str); + + expect(obj.data[0].x).toEqual([1, 2, 3]); + expect(obj.data[0].y).toEqual([1, 2, 1]); + expect(obj.data[0].marker.size).toEqual([20, 30, 10]); + expect(obj.data[0].marker.color).toEqual([10, 30, 20]); + }) + .catch(failTest) + .then(done); }); });