88
99'use strict' ;
1010
11+ var isNumeric = require ( 'fast-isnumeric' ) ;
12+
1113var plotApi = require ( './plot_api' ) ;
1214var Lib = require ( '../lib' ) ;
1315
@@ -27,15 +29,17 @@ var attrs = {
2729 min : 1 ,
2830 description : [
2931 'Sets the exported image width.' ,
30- 'Defaults to the value found in `layout.width`'
32+ 'Defaults to the value found in `layout.width`' ,
33+ 'If set to *null*, the exported image width will match the current graph width.'
3134 ] . join ( ' ' )
3235 } ,
3336 height : {
3437 valType : 'number' ,
3538 min : 1 ,
3639 description : [
3740 'Sets the exported image height.' ,
38- 'Defaults to the value found in `layout.height`'
41+ 'Defaults to the value found in `layout.height`' ,
42+ 'If set to *null*, the exported image height will match the current graph height.'
3943 ] . join ( ' ' )
4044 } ,
4145 scale : {
@@ -87,23 +91,27 @@ function toImage(gd, opts) {
8791 var data ;
8892 var layout ;
8993 var config ;
94+ var fullLayout ;
9095
9196 if ( Lib . isPlainObject ( gd ) ) {
9297 data = gd . data || [ ] ;
9398 layout = gd . layout || { } ;
9499 config = gd . config || { } ;
100+ fullLayout = { } ;
95101 } else {
96102 gd = Lib . getGraphDiv ( gd ) ;
97103 data = Lib . extendDeep ( [ ] , gd . data ) ;
98104 layout = Lib . extendDeep ( { } , gd . layout ) ;
99105 config = gd . _context ;
106+ fullLayout = gd . _fullLayout || { } ;
100107 }
101108
102109 function isImpliedOrValid ( attr ) {
103110 return ! ( attr in opts ) || Lib . validate ( opts [ attr ] , attrs [ attr ] ) ;
104111 }
105112
106- if ( ! isImpliedOrValid ( 'width' ) || ! isImpliedOrValid ( 'height' ) ) {
113+ if ( ( ! isImpliedOrValid ( 'width' ) && opts . width !== null ) ||
114+ ( ! isImpliedOrValid ( 'height' ) && opts . height !== null ) ) {
107115 throw new Error ( 'Height and width should be pixel values.' ) ;
108116 }
109117
@@ -132,8 +140,16 @@ function toImage(gd, opts) {
132140
133141 // extend layout with image options
134142 var layoutImage = Lib . extendFlat ( { } , layout ) ;
135- if ( width ) layoutImage . width = width ;
136- if ( height ) layoutImage . height = height ;
143+ if ( width ) {
144+ layoutImage . width = width ;
145+ } else if ( opts . width === null && isNumeric ( fullLayout . width ) ) {
146+ layoutImage . width = fullLayout . width ;
147+ }
148+ if ( height ) {
149+ layoutImage . height = height ;
150+ } else if ( opts . height === null && isNumeric ( fullLayout . height ) ) {
151+ layoutImage . height = fullLayout . height ;
152+ }
137153
138154 // extend config for static plot
139155 var configImage = Lib . extendFlat ( { } , config , {
0 commit comments