From c44ff238ca5c92923d0c03ddf68f758e5e44e6b2 Mon Sep 17 00:00:00 2001 From: archmoj Date: Thu, 8 Oct 2020 13:09:59 -0400 Subject: [PATCH 1/2] export computed margins in full-json --- src/plots/plots.js | 15 ++++++++++++++- test/jasmine/tests/toimage_test.js | 17 +++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/plots/plots.js b/src/plots/plots.js index 03ba70426c0..665c6b35784 100644 --- a/src/plots/plots.js +++ b/src/plots/plots.js @@ -2160,7 +2160,20 @@ plots.graphJson = function(gd, dataonly, mode, output, useDefaults, includeConfi return d; }) }; - if(!dataonly) { obj.layout = stripObj(layout); } + if(!dataonly) { + obj.layout = stripObj(layout); + if(useDefaults) { + var gs = layout._size; + obj.layout._computed = { + margin: { + b: gs.b, + l: gs.l, + r: gs.r, + t: gs.t + } + }; + } + } if(gd.framework && gd.framework.isPolar) obj = gd.framework.getConfig(); diff --git a/test/jasmine/tests/toimage_test.js b/test/jasmine/tests/toimage_test.js index 5baac3d2445..a5b852e40f4 100644 --- a/test/jasmine/tests/toimage_test.js +++ b/test/jasmine/tests/toimage_test.js @@ -6,6 +6,7 @@ var createGraphDiv = require('../assets/create_graph_div'); var destroyGraphDiv = require('../assets/destroy_graph_div'); var failTest = require('../assets/fail_test'); var subplotMock = require('@mocks/multiple_subplots.json'); +var pieAutoMargin = require('@mocks/pie_automargin'); var FORMATS = ['png', 'jpeg', 'webp', 'svg']; @@ -301,5 +302,21 @@ describe('Plotly.toImage', function() { .catch(failTest) .then(done); }); + + it('export computed margins', function(done) { + Plotly.toImage(pieAutoMargin, imgOpts) + .then(function(fig) { + fig = JSON.parse(fig); + var computed = fig.layout._computed; + expect(computed).toBeDefined('no computed'); + expect(computed.margin).toBeDefined('no computed margin'); + expect(computed.margin.t).toBeDefined('no top'); + expect(computed.margin.l).toBeDefined('no left'); + expect(computed.margin.r).toBeDefined('no right'); + expect(computed.margin.b).toBeDefined('no bottom'); + }) + .catch(failTest) + .then(done); + }); }); }); From 586f2cc3180d537e204f1b274864cd3170339465 Mon Sep 17 00:00:00 2001 From: archmoj Date: Fri, 9 Oct 2020 08:19:16 -0400 Subject: [PATCH 2/2] define layout.computed in the schema --- src/plots/layout_attributes.js | 9 +++++++++ src/plots/plots.js | 2 +- test/jasmine/tests/toimage_test.js | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/plots/layout_attributes.js b/src/plots/layout_attributes.js index 2a2a2f3a473..e7b9191182f 100644 --- a/src/plots/layout_attributes.js +++ b/src/plots/layout_attributes.js @@ -265,6 +265,15 @@ module.exports = { }, editType: 'plot' }, + computed: { + valType: 'any', + role: 'info', + editType: 'none', + description: [ + 'Placeholder for exporting automargin-impacting values namely', + '`margin.t`, `margin.b`, `margin.l` and `margin.r` in *full-json* mode.', + ].join(' ') + }, paper_bgcolor: { valType: 'color', role: 'style', diff --git a/src/plots/plots.js b/src/plots/plots.js index 665c6b35784..ff1e5f9d232 100644 --- a/src/plots/plots.js +++ b/src/plots/plots.js @@ -2164,7 +2164,7 @@ plots.graphJson = function(gd, dataonly, mode, output, useDefaults, includeConfi obj.layout = stripObj(layout); if(useDefaults) { var gs = layout._size; - obj.layout._computed = { + obj.layout.computed = { margin: { b: gs.b, l: gs.l, diff --git a/test/jasmine/tests/toimage_test.js b/test/jasmine/tests/toimage_test.js index a5b852e40f4..c1d8ac15e46 100644 --- a/test/jasmine/tests/toimage_test.js +++ b/test/jasmine/tests/toimage_test.js @@ -307,7 +307,7 @@ describe('Plotly.toImage', function() { Plotly.toImage(pieAutoMargin, imgOpts) .then(function(fig) { fig = JSON.parse(fig); - var computed = fig.layout._computed; + var computed = fig.layout.computed; expect(computed).toBeDefined('no computed'); expect(computed.margin).toBeDefined('no computed margin'); expect(computed.margin.t).toBeDefined('no top');