8
8
9
9
'use strict' ;
10
10
11
+ var isNumeric = require ( 'fast-isnumeric' ) ;
12
+
11
13
var plotApi = require ( './plot_api' ) ;
12
14
var Lib = require ( '../lib' ) ;
13
15
@@ -27,15 +29,17 @@ var attrs = {
27
29
min : 1 ,
28
30
description : [
29
31
'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.'
31
34
] . join ( ' ' )
32
35
} ,
33
36
height : {
34
37
valType : 'number' ,
35
38
min : 1 ,
36
39
description : [
37
40
'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.'
39
43
] . join ( ' ' )
40
44
} ,
41
45
scale : {
@@ -87,23 +91,27 @@ function toImage(gd, opts) {
87
91
var data ;
88
92
var layout ;
89
93
var config ;
94
+ var fullLayout ;
90
95
91
96
if ( Lib . isPlainObject ( gd ) ) {
92
97
data = gd . data || [ ] ;
93
98
layout = gd . layout || { } ;
94
99
config = gd . config || { } ;
100
+ fullLayout = { } ;
95
101
} else {
96
102
gd = Lib . getGraphDiv ( gd ) ;
97
103
data = Lib . extendDeep ( [ ] , gd . data ) ;
98
104
layout = Lib . extendDeep ( { } , gd . layout ) ;
99
105
config = gd . _context ;
106
+ fullLayout = gd . _fullLayout || { } ;
100
107
}
101
108
102
109
function isImpliedOrValid ( attr ) {
103
110
return ! ( attr in opts ) || Lib . validate ( opts [ attr ] , attrs [ attr ] ) ;
104
111
}
105
112
106
- if ( ! isImpliedOrValid ( 'width' ) || ! isImpliedOrValid ( 'height' ) ) {
113
+ if ( ( ! isImpliedOrValid ( 'width' ) && opts . width !== null ) ||
114
+ ( ! isImpliedOrValid ( 'height' ) && opts . height !== null ) ) {
107
115
throw new Error ( 'Height and width should be pixel values.' ) ;
108
116
}
109
117
@@ -132,8 +140,16 @@ function toImage(gd, opts) {
132
140
133
141
// extend layout with image options
134
142
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
+ }
137
153
138
154
// extend config for static plot
139
155
var configImage = Lib . extendFlat ( { } , config , {
0 commit comments