Skip to content

Commit 2ca0ae3

Browse files
committed
add format "full-json" to Plotly.toImage and Plotly.dowloadImage
1 parent 15d75db commit 2ca0ae3

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

src/plot_api/to_image.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
var isNumeric = require('fast-isnumeric');
1212

1313
var plotApi = require('./plot_api');
14+
var plots = require('../plots/plots');
1415
var Lib = require('../lib');
1516

1617
var helpers = require('../snapshot/helpers');
@@ -20,7 +21,7 @@ var svgToImg = require('../snapshot/svgtoimg');
2021
var attrs = {
2122
format: {
2223
valType: 'enumerated',
23-
values: ['png', 'jpeg', 'webp', 'svg'],
24+
values: ['png', 'jpeg', 'webp', 'svg', 'full-json'],
2425
dflt: 'png',
2526
description: 'Sets the format of exported image.'
2627
},
@@ -170,6 +171,15 @@ function toImage(gd, opts) {
170171
var width = clonedGd._fullLayout.width;
171172
var height = clonedGd._fullLayout.height;
172173

174+
if(format === 'full-json') {
175+
var json = plots.graphJson(clonedGd, false, 'keepdata', false, true);
176+
if(imageDataOnly) {
177+
return resolve(json);
178+
} else {
179+
return resolve(helpers.encodeJSON(json));
180+
}
181+
}
182+
173183
plotApi.purge(clonedGd);
174184
document.body.removeChild(clonedGd);
175185

src/snapshot/helpers.js

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ exports.encodeSVG = function(svg) {
3636
return 'data:image/svg+xml,' + encodeURIComponent(svg);
3737
};
3838

39+
exports.encodeJSON = function(json) {
40+
return 'data:application/json,' + encodeURIComponent(json);
41+
};
42+
3943
var DOM_URL = window.URL || window.webkitURL;
4044

4145
exports.createObjectURL = function(blob) {
@@ -49,6 +53,8 @@ exports.revokeObjectURL = function(url) {
4953
exports.createBlob = function(url, format) {
5054
if(format === 'svg') {
5155
return new window.Blob([url], {type: 'image/svg+xml;charset=utf-8'});
56+
} else if(format === 'full-json') {
57+
return new window.Blob([url], {type: 'application/json;charset=utf-8'});
5258
} else {
5359
var binary = fixBinary(window.atob(url));
5460
return new window.Blob([binary], {type: 'image/' + format});

test/jasmine/tests/download_test.js

+6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ describe('Plotly.downloadImage', function() {
5252
.then(done);
5353
}, LONG_TIMEOUT_INTERVAL);
5454

55+
it('should create link, remove link, accept options', function(done) {
56+
downloadTest(gd, 'full-json')
57+
.catch(failTest)
58+
.then(done);
59+
}, LONG_TIMEOUT_INTERVAL);
60+
5561
it('should create link, remove link, accept options', function(done) {
5662
checkWebp(function(supported) {
5763
if(supported) {

test/jasmine/tests/toimage_test.js

+24
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,28 @@ describe('Plotly.toImage', function() {
263263
done();
264264
});
265265
});
266+
267+
describe('with format `full-json`', function() {
268+
var imgOpts = {format: 'full-json', imageDataOnly: true};
269+
it('export a graph div', function(done) {
270+
Plotly.plot(gd, [{y: [1, 2, 3]}])
271+
.then(function() { return Plotly.toImage('graph', imgOpts);})
272+
.then(function(fig) {
273+
fig = JSON.parse(fig);
274+
expect(fig.data[0].mode).toBe('lines+markers', 'contain default mode');
275+
})
276+
.catch(failTest)
277+
.then(done);
278+
});
279+
280+
it('export an object with data/layout/config', function(done) {
281+
Plotly.toImage({data: [{y: [1, 2, 3]}]}, imgOpts)
282+
.then(function(fig) {
283+
fig = JSON.parse(fig);
284+
expect(fig.data[0].mode).toBe('lines+markers', 'contain default mode');
285+
})
286+
.catch(failTest)
287+
.then(done);
288+
});
289+
});
266290
});

0 commit comments

Comments
 (0)