Skip to content

Commit 9497ed5

Browse files
committed
Plotly.toImage: format "full-json" now contains config and version key
1 parent 2ca0ae3 commit 9497ed5

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

src/plot_api/to_image.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var Lib = require('../lib');
1717
var helpers = require('../snapshot/helpers');
1818
var toSVG = require('../snapshot/tosvg');
1919
var svgToImg = require('../snapshot/svgtoimg');
20+
var version = require('../core').version;
2021

2122
var attrs = {
2223
format: {
@@ -172,7 +173,9 @@ function toImage(gd, opts) {
172173
var height = clonedGd._fullLayout.height;
173174

174175
if(format === 'full-json') {
175-
var json = plots.graphJson(clonedGd, false, 'keepdata', false, true);
176+
var json = plots.graphJson(clonedGd, false, 'keepdata', 'object', true, true);
177+
json.version = version;
178+
json = JSON.stringify(json);
176179
if(imageDataOnly) {
177180
return resolve(json);
178181
} else {

src/plots/plots.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -2037,9 +2037,10 @@ plots.didMarginChange = function(margin0, margin1) {
20372037
* keepall: keep data and src
20382038
* @param {String} output If you specify 'object', the result will not be stringified
20392039
* @param {Boolean} useDefaults If truthy, use _fullLayout and _fullData
2040+
* @param {Boolean} includeConfig If truthy, include _context
20402041
* @returns {Object|String}
20412042
*/
2042-
plots.graphJson = function(gd, dataonly, mode, output, useDefaults) {
2043+
plots.graphJson = function(gd, dataonly, mode, output, useDefaults, includeConfig) {
20432044
// if the defaults aren't supplied yet, we need to do that...
20442045
if((useDefaults && dataonly && !gd._fullData) ||
20452046
(useDefaults && !dataonly && !gd._fullLayout)) {
@@ -2050,18 +2051,20 @@ plots.graphJson = function(gd, dataonly, mode, output, useDefaults) {
20502051
var layout = (useDefaults) ? gd._fullLayout : gd.layout;
20512052
var frames = (gd._transitionData || {})._frames;
20522053

2053-
function stripObj(d) {
2054+
function stripObj(d, keepFunction) {
20542055
if(typeof d === 'function') {
2055-
return null;
2056+
return keepFunction ? '_function_' : null;
20562057
}
20572058
if(Lib.isPlainObject(d)) {
20582059
var o = {};
20592060
var v, src;
20602061
for(v in d) {
20612062
// remove private elements and functions
20622063
// _ is for private, [ is a mistake ie [object Object]
2063-
if(typeof d[v] === 'function' ||
2064-
['_', '['].indexOf(v.charAt(0)) !== -1) {
2064+
if(['_', '['].indexOf(v.charAt(0)) !== -1) continue;
2065+
2066+
if(keepFunction && typeof d[v] === 'function') {
2067+
o[v] = '_function';
20652068
continue;
20662069
}
20672070

@@ -2091,13 +2094,13 @@ plots.graphJson = function(gd, dataonly, mode, output, useDefaults) {
20912094
}
20922095

20932096
// OK, we're including this... recurse into it
2094-
o[v] = stripObj(d[v]);
2097+
o[v] = stripObj(d[v], keepFunction);
20952098
}
20962099
return o;
20972100
}
20982101

20992102
if(Array.isArray(d)) {
2100-
return d.map(stripObj);
2103+
return d.map(function(x) {return stripObj(x, keepFunction);});
21012104
}
21022105

21032106
if(Lib.isTypedArray(d)) {
@@ -2126,6 +2129,8 @@ plots.graphJson = function(gd, dataonly, mode, output, useDefaults) {
21262129

21272130
if(frames) obj.frames = stripObj(frames);
21282131

2132+
if(includeConfig) obj.config = stripObj(gd._context, true);
2133+
21292134
return (output === 'object') ? obj : JSON.stringify(obj);
21302135
};
21312136

test/jasmine/tests/toimage_test.js

+8
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,11 @@ describe('Plotly.toImage', function() {
271271
.then(function() { return Plotly.toImage('graph', imgOpts);})
272272
.then(function(fig) {
273273
fig = JSON.parse(fig);
274+
['data', 'layout', 'config'].forEach(function(key) {
275+
expect(fig.hasOwnProperty(key)).toBeTruthy('is missing key: ' + key);
276+
});
274277
expect(fig.data[0].mode).toBe('lines+markers', 'contain default mode');
278+
expect(fig.version).toBe(Plotly.version, 'contains Plotly version');
275279
})
276280
.catch(failTest)
277281
.then(done);
@@ -281,7 +285,11 @@ describe('Plotly.toImage', function() {
281285
Plotly.toImage({data: [{y: [1, 2, 3]}]}, imgOpts)
282286
.then(function(fig) {
283287
fig = JSON.parse(fig);
288+
['data', 'layout', 'config'].forEach(function(key) {
289+
expect(fig.hasOwnProperty(key)).toBeTruthy('is missing key: ' + key);
290+
});
284291
expect(fig.data[0].mode).toBe('lines+markers', 'contain default mode');
292+
expect(fig.version).toBe(Plotly.version, 'contains Plotly version');
285293
})
286294
.catch(failTest)
287295
.then(done);

0 commit comments

Comments
 (0)