forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto_image.js
209 lines (182 loc) · 5.92 KB
/
to_image.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* Copyright 2012-2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var plotApi = require('./plot_api');
var Lib = require('../lib');
var helpers = require('../snapshot/helpers');
var toSVG = require('../snapshot/tosvg');
var svgToImg = require('../snapshot/svgtoimg');
var attrs = {
format: {
valType: 'enumerated',
values: ['png', 'jpeg', 'webp', 'svg'],
dflt: 'png',
description: 'Sets the format of exported image.'
},
width: {
valType: 'number',
min: 1,
description: [
'Sets the exported image width.',
'Defaults to the value found in `layout.width`'
].join(' ')
},
height: {
valType: 'number',
min: 1,
description: [
'Sets the exported image height.',
'Defaults to the value found in `layout.height`'
].join(' ')
},
scale: {
valType: 'number',
min: 0,
dflt: 1,
description: [
'Sets a scaling for the generated image.',
'If set, all features of a graphs (e.g. text, line width)',
'are scaled, unlike simply setting',
'a bigger *width* and *height*.'
].join(' ')
},
setBackground: {
valType: 'any',
dflt: false,
description: [
'Sets the image background mode.',
'By default, the image background is determined by `layout.paper_bgcolor`,',
'the *transparent* mode.',
'One might consider setting `setBackground` to *opaque*',
'when exporting a *jpeg* image as JPEGs do not support opacity.'
].join(' ')
},
imageDataOnly: {
valType: 'boolean',
dflt: false,
description: [
'Determines whether or not the return value is prefixed by',
'the image format\'s corresponding \'data:image;\' spec.'
].join(' ')
}
};
var IMAGE_URL_PREFIX = /^data:image\/\w+;base64,/;
/** Plotly.toImage
*
* @param {object | string | HTML div} gd
* can either be a data/layout/config object
* or an existing graph <div>
* or an id to an existing graph <div>
* @param {object} opts (see above)
* @return {promise}
*/
function toImage(gd, opts) {
opts = opts || {};
var data;
var layout;
var config;
if(Lib.isPlainObject(gd)) {
data = gd.data || [];
layout = gd.layout || {};
config = gd.config || {};
} else {
gd = Lib.getGraphDiv(gd);
data = Lib.extendDeep([], gd.data);
layout = Lib.extendDeep({}, gd.layout);
config = gd._context;
}
function isImpliedOrValid(attr) {
return !(attr in opts) || Lib.validate(opts[attr], attrs[attr]);
}
if(!isImpliedOrValid('width') || !isImpliedOrValid('height')) {
throw new Error('Height and width should be pixel values.');
}
if(!isImpliedOrValid('format')) {
throw new Error('Image format is not jpeg, png, svg or webp.');
}
var fullOpts = {};
function coerce(attr, dflt) {
return Lib.coerce(opts, fullOpts, attrs, attr, dflt);
}
var format = coerce('format');
var width = coerce('width');
var height = coerce('height');
var scale = coerce('scale');
var setBackground = coerce('setBackground');
var imageDataOnly = coerce('imageDataOnly');
// put the cloned div somewhere off screen before attaching to DOM
var clonedGd = document.createElement('div');
clonedGd.style.position = 'absolute';
clonedGd.style.left = '-5000px';
document.body.appendChild(clonedGd);
// extend layout with image options
var layoutImage = Lib.extendFlat({}, layout);
if(width) layoutImage.width = width;
if(height) layoutImage.height = height;
// extend config for static plot
var configImage = Lib.extendFlat({}, config, {
_exportedPlot: true,
staticPlot: true,
setBackground: setBackground
});
var redrawFunc = helpers.getRedrawFunc(clonedGd);
function wait() {
return new Promise(function(resolve) {
setTimeout(resolve, helpers.getDelay(clonedGd._fullLayout));
});
}
function convert() {
return new Promise(function(resolve, reject) {
var svg = toSVG(clonedGd, format, scale);
var width = clonedGd._fullLayout.width;
var height = clonedGd._fullLayout.height;
plotApi.purge(clonedGd);
document.body.removeChild(clonedGd);
if(format === 'svg') {
if(imageDataOnly) {
return resolve(svg);
} else {
return resolve('data:image/svg+xml,' + encodeURIComponent(svg));
}
}
var canvas = document.createElement('canvas');
canvas.id = Lib.randstr();
svgToImg({
format: format,
width: width,
height: height,
scale: scale,
canvas: canvas,
svg: svg,
// ask svgToImg to return a Promise
// rather than EventEmitter
// leave EventEmitter for backward
// compatibility
promise: true
})
.then(resolve)
.catch(reject);
});
}
function urlToImageData(url) {
if(imageDataOnly) {
return url.replace(IMAGE_URL_PREFIX, '');
} else {
return url;
}
}
return new Promise(function(resolve, reject) {
plotApi.plot(clonedGd, data, layoutImage, configImage)
.then(redrawFunc)
.then(wait)
.then(convert)
.then(function(url) { resolve(urlToImageData(url)); })
.catch(function(err) { reject(err); });
});
}
module.exports = toImage;