|
8 | 8 |
|
9 | 9 | 'use strict'; |
10 | 10 |
|
11 | | -var isNumeric = require('fast-isnumeric'); |
12 | | - |
13 | 11 | var Plotly = require('../plotly'); |
14 | 12 | var Lib = require('../lib'); |
15 | 13 |
|
16 | 14 | var helpers = require('../snapshot/helpers'); |
17 | | -var clonePlot = require('../snapshot/cloneplot'); |
18 | 15 | var toSVG = require('../snapshot/tosvg'); |
19 | 16 | var svgToImg = require('../snapshot/svgtoimg'); |
20 | 17 |
|
21 | | -/** |
22 | | - * @param {object} gd figure Object |
23 | | - * @param {object} opts option object |
24 | | - * @param opts.format 'jpeg' | 'png' | 'webp' | 'svg' |
25 | | - * @param opts.width width of snapshot in px |
26 | | - * @param opts.height height of snapshot in px |
| 18 | +var getGraphDiv = require('./helpers').getGraphDiv; |
| 19 | + |
| 20 | +var attrs = { |
| 21 | + format: { |
| 22 | + valType: 'enumerated', |
| 23 | + values: ['png', 'jpeg', 'webp', 'svg'], |
| 24 | + dflt: 'png', |
| 25 | + description: 'Sets the format of exported image.' |
| 26 | + }, |
| 27 | + width: { |
| 28 | + valType: 'number', |
| 29 | + min: 1, |
| 30 | + description: [ |
| 31 | + 'Sets the exported image width.', |
| 32 | + 'Defaults to the value found in `layout.width`' |
| 33 | + ].join(' ') |
| 34 | + }, |
| 35 | + height: { |
| 36 | + valType: 'number', |
| 37 | + min: 1, |
| 38 | + description: [ |
| 39 | + 'Sets the exported image height.', |
| 40 | + 'Defaults to the value found in `layout.height`' |
| 41 | + ].join(' ') |
| 42 | + }, |
| 43 | + setBackground: { |
| 44 | + valType: 'any', |
| 45 | + dflt: false, |
| 46 | + description: [ |
| 47 | + 'Sets the image background mode.', |
| 48 | + 'By default, the image background is determined by `layout.paper_bgcolor`,', |
| 49 | + 'the *transparent* mode.', |
| 50 | + 'One might consider setting `setBackground` to *opaque*', |
| 51 | + 'when exporting a *jpeg* image as JPEGs do not support opacity.' |
| 52 | + ].join(' ') |
| 53 | + }, |
| 54 | + imageDataOnly: { |
| 55 | + valType: 'boolean', |
| 56 | + dflt: false, |
| 57 | + description: [ |
| 58 | + 'Determines whether or not the return value is prefixed by', |
| 59 | + 'the image format\'s corresponding \'data:image;\' spec.' |
| 60 | + ].join(' ') |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +var IMAGE_URL_PREFIX = /^data:image\/\w+;base64,/; |
| 65 | + |
| 66 | +/** Plotly.toImage |
| 67 | + * |
| 68 | + * @param {object | string | HTML div} gd |
| 69 | + * can either be a data/layout/config object |
| 70 | + * or an existing graph <div> |
| 71 | + * or an id to an existing graph <div> |
| 72 | + * @param {object} opts (see above) |
| 73 | + * @return {promise} |
27 | 74 | */ |
28 | 75 | function toImage(gd, opts) { |
| 76 | + opts = opts || {}; |
| 77 | + |
| 78 | + var data; |
| 79 | + var layout; |
| 80 | + var config; |
| 81 | + |
| 82 | + if(Lib.isPlainObject(gd)) { |
| 83 | + data = gd.data || []; |
| 84 | + layout = gd.layout || {}; |
| 85 | + config = gd.config || {}; |
| 86 | + } else { |
| 87 | + gd = getGraphDiv(gd); |
| 88 | + data = Lib.extendDeep([], gd.data); |
| 89 | + layout = Lib.extendDeep({}, gd.layout); |
| 90 | + config = gd._context; |
| 91 | + } |
| 92 | + |
| 93 | + function isImpliedOrValid(attr) { |
| 94 | + return !(attr in opts) || Lib.validate(opts[attr], attrs[attr]); |
| 95 | + } |
| 96 | + |
| 97 | + if(!isImpliedOrValid('width') || !isImpliedOrValid('height')) { |
| 98 | + throw new Error('Height and width should be pixel values.'); |
| 99 | + } |
| 100 | + |
| 101 | + if(!isImpliedOrValid('format')) { |
| 102 | + throw new Error('Image format is not jpeg, png, svg or webp.'); |
| 103 | + } |
| 104 | + |
| 105 | + var fullOpts = {}; |
| 106 | + |
| 107 | + function coerce(attr, dflt) { |
| 108 | + return Lib.coerce(opts, fullOpts, attrs, attr, dflt); |
| 109 | + } |
| 110 | + |
| 111 | + var format = coerce('format'); |
| 112 | + var width = coerce('width'); |
| 113 | + var height = coerce('height'); |
| 114 | + var setBackground = coerce('setBackground'); |
| 115 | + var imageDataOnly = coerce('imageDataOnly'); |
| 116 | + |
| 117 | + // put the cloned div somewhere off screen before attaching to DOM |
| 118 | + var clonedGd = document.createElement('div'); |
| 119 | + clonedGd.style.position = 'absolute'; |
| 120 | + clonedGd.style.left = '-5000px'; |
| 121 | + document.body.appendChild(clonedGd); |
| 122 | + |
| 123 | + // extend layout with image options |
| 124 | + var layoutImage = Lib.extendFlat({}, layout); |
| 125 | + if(width) layoutImage.width = width; |
| 126 | + if(height) layoutImage.height = height; |
| 127 | + |
| 128 | + // extend config for static plot |
| 129 | + var configImage = Lib.extendFlat({}, config, { |
| 130 | + staticPlot: true, |
| 131 | + plotGlPixelRatio: config.plotGlPixelRatio || 2, |
| 132 | + setBackground: setBackground |
| 133 | + }); |
29 | 134 |
|
30 | | - var promise = new Promise(function(resolve, reject) { |
31 | | - // check for undefined opts |
32 | | - opts = opts || {}; |
33 | | - // default to png |
34 | | - opts.format = opts.format || 'png'; |
35 | | - |
36 | | - var isSizeGood = function(size) { |
37 | | - // undefined and null are valid options |
38 | | - if(size === undefined || size === null) { |
39 | | - return true; |
40 | | - } |
41 | | - |
42 | | - if(isNumeric(size) && size > 1) { |
43 | | - return true; |
| 135 | + var redrawFunc = helpers.getRedrawFunc(clonedGd); |
| 136 | + |
| 137 | + function wait() { |
| 138 | + return new Promise(function(resolve) { |
| 139 | + setTimeout(resolve, helpers.getDelay(clonedGd._fullLayout)); |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + function convert() { |
| 144 | + return new Promise(function(resolve, reject) { |
| 145 | + var svg = toSVG(clonedGd); |
| 146 | + var width = clonedGd._fullLayout.width; |
| 147 | + var height = clonedGd._fullLayout.height; |
| 148 | + |
| 149 | + Plotly.purge(clonedGd); |
| 150 | + document.body.removeChild(clonedGd); |
| 151 | + |
| 152 | + if(format === 'svg') { |
| 153 | + if(imageDataOnly) { |
| 154 | + return resolve(svg); |
| 155 | + } else { |
| 156 | + return resolve('data:image/svg+xml,' + encodeURIComponent(svg)); |
| 157 | + } |
44 | 158 | } |
45 | 159 |
|
46 | | - return false; |
47 | | - }; |
48 | | - |
49 | | - if(!isSizeGood(opts.width) || !isSizeGood(opts.height)) { |
50 | | - reject(new Error('Height and width should be pixel values.')); |
51 | | - } |
52 | | - |
53 | | - // first clone the GD so we can operate in a clean environment |
54 | | - var clone = clonePlot(gd, {format: 'png', height: opts.height, width: opts.width}); |
55 | | - var clonedGd = clone.gd; |
56 | | - |
57 | | - // put the cloned div somewhere off screen before attaching to DOM |
58 | | - clonedGd.style.position = 'absolute'; |
59 | | - clonedGd.style.left = '-5000px'; |
60 | | - document.body.appendChild(clonedGd); |
61 | | - |
62 | | - function wait() { |
63 | | - var delay = helpers.getDelay(clonedGd._fullLayout); |
64 | | - |
65 | | - return new Promise(function(resolve, reject) { |
66 | | - setTimeout(function() { |
67 | | - var svg = toSVG(clonedGd); |
68 | | - |
69 | | - var canvas = document.createElement('canvas'); |
70 | | - canvas.id = Lib.randstr(); |
71 | | - |
72 | | - svgToImg({ |
73 | | - format: opts.format, |
74 | | - width: clonedGd._fullLayout.width, |
75 | | - height: clonedGd._fullLayout.height, |
76 | | - canvas: canvas, |
77 | | - svg: svg, |
78 | | - // ask svgToImg to return a Promise |
79 | | - // rather than EventEmitter |
80 | | - // leave EventEmitter for backward |
81 | | - // compatibility |
82 | | - promise: true |
83 | | - }).then(function(url) { |
84 | | - if(clonedGd) document.body.removeChild(clonedGd); |
85 | | - resolve(url); |
86 | | - }).catch(function(err) { |
87 | | - reject(err); |
88 | | - }); |
89 | | - |
90 | | - }, delay); |
91 | | - }); |
| 160 | + var canvas = document.createElement('canvas'); |
| 161 | + canvas.id = Lib.randstr(); |
| 162 | + |
| 163 | + svgToImg({ |
| 164 | + format: format, |
| 165 | + width: width, |
| 166 | + height: height, |
| 167 | + canvas: canvas, |
| 168 | + svg: svg, |
| 169 | + // ask svgToImg to return a Promise |
| 170 | + // rather than EventEmitter |
| 171 | + // leave EventEmitter for backward |
| 172 | + // compatibility |
| 173 | + promise: true |
| 174 | + }) |
| 175 | + .then(resolve) |
| 176 | + .catch(reject); |
| 177 | + }); |
| 178 | + } |
| 179 | + |
| 180 | + function urlToImageData(url) { |
| 181 | + if(imageDataOnly) { |
| 182 | + return url.replace(IMAGE_URL_PREFIX, ''); |
| 183 | + } else { |
| 184 | + return url; |
92 | 185 | } |
| 186 | + } |
93 | 187 |
|
94 | | - var redrawFunc = helpers.getRedrawFunc(clonedGd); |
95 | | - |
96 | | - Plotly.plot(clonedGd, clone.data, clone.layout, clone.config) |
| 188 | + return new Promise(function(resolve, reject) { |
| 189 | + Plotly.plot(clonedGd, data, layoutImage, configImage) |
97 | 190 | .then(redrawFunc) |
98 | 191 | .then(wait) |
99 | | - .then(function(url) { resolve(url); }) |
100 | | - .catch(function(err) { |
101 | | - reject(err); |
102 | | - }); |
| 192 | + .then(convert) |
| 193 | + .then(function(url) { resolve(urlToImageData(url)); }) |
| 194 | + .catch(function(err) { reject(err); }); |
103 | 195 | }); |
104 | | - |
105 | | - return promise; |
106 | 196 | } |
107 | 197 |
|
108 | 198 | module.exports = toImage; |
0 commit comments